java - How to Use HikariCP with MySql JDBC -


i'm trying use hikaricp jdbc connection pool in java application. i'm not using frameworks spring or hibernate in application. i'm able connect mysql db using simple jdbc driver when try using hiraki code not working. can't understand i'm going wrong after initializing data source .

initial jdbc working code..

public class connect extends errorcat{      protected connection connection = null;      //database user name , password     private string name = "root";     private string pass = "";      //database url , jdbc driver     private string url = "jdbc:mysql://127.0.0.1:3306/fls";     private string driver = "com.mysql.jdbc.driver";      protected /*static connection*/void getconnection(){          if (connection == null){             system.out.println("registering driver....");              try {                 //driver registration                 class.forname(driver).newinstance();                 system.out.println("driver registered successfully!!.");                  //initiate connection                 system.out.println("connecting database...");                 connection = drivermanager.getconnection(url, name, pass);                 system.out.println("connected database!!!");              } catch (instantiationexception | illegalaccessexception | classnotfoundexception e) {                 e.printstacktrace();                 system.out.println("couldnt register driver...");             } catch (sqlexception e) {                 e.printstacktrace();                 system.out.println("couldnt connect database...");             }         }          //return connection;     } } 

updated code(not working)..

public class connect extends errorcat{      protected connection connection = null;      protected connection connection = null;     protected hikaridatasource ds = null;      protected static connection instance = null;       protected /*static connection*/void getconnection() {          if (connection == null){             system.out.println("registering driver....");              connect ct = new connect();              ct.hikarigfxdpool();         }          //return connection;     }      protected void hikarigfxdpool(){          hikariconfig config = new hikariconfig();         config.setjdbcurl("jdbc:mysql://localhost:3306/simpsons");         config.setusername("bart");         config.setpassword("51mp50n");           config.adddatasourceproperty("cacheprepstmts", "true");         config.adddatasourceproperty("prepstmtcachesize", "250");         config.adddatasourceproperty("prepstmtcachesqllimit", "2048");          hikaridatasource ds = new hikaridatasource(config);     } } 

i think real problem in method hikarigfxdpool create local variable , class variable protected hikaridatasource ds = null; remain null. cannot connection.

the best way use separate class make , connections

something this:

public class dbhandler{     private static hikaridatasource ds;     static{          hikariconfig config = new hikariconfig();         config.setjdbcurl("jdbc:mysql://localhost:3306/simpsons");         config.setusername("bart");         config.setpassword("51mp50n");           config.setdriverclassname("com.mysql.jdbc.driver");         config.adddatasourceproperty("cacheprepstmts", "true");         config.adddatasourceproperty("prepstmtcachesize", "250");         config.adddatasourceproperty("prepstmtcachesqllimit", "2048");          ds = new hikaridatasource(config);     }      public static connection getconn() throws sqlexception {         return ds.getconnection();     }  } 

then in yours other class connection using:

connection conn = dbhandler.getconn(); // query conn.close(); 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -