添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm trying to use HikariCP JDBC connection pool in my Java application. I'm not using any frameworks like Spring or Hibernate in my application. Currently I'm able to connect to MySQL DB using simple JDBC driver but when I try using Hiraki the code is not working. Can't understand where I'm going wrong even after initializing the data source .

Initial JDBC Working Code..

public class Connect extends ErrorCat{
    protected Connection connection = null;
    //Database user name and password
    private String name = "root";
    private String pass = "";
    //Database URL and 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 a connection
                System.out.println("Connecting to database...");
                connection = DriverManager.getConnection(url, name, pass);
                System.out.println("Connected to database!!!");
            } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
                e.printStackTrace();
                System.out.println("Couldnt register driver...");
            } catch (SQLException e) {
                e.printStackTrace();
                System.out.println("Couldnt connect to 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);
                Even after adding the driver and printing the stack trace in try catch I'm not getting an error. The Application is running fine but the Mysql Data is not being displayed..
– Lucy
                Jan 27, 2016 at 9:16
                The last line HikariDataSource ds = new HikariDataSource(config); creates a local variable. Maybe you want to use the protected ds variable already declared?
– Kayaman
                Jan 27, 2016 at 9:24
                You are never creating a connection. You are only creating a connection pool to immediately throw it away. The connection is never anything other than null.
– Mark Rotteveel
                Jan 27, 2016 at 10:27

I Think that the real problem is that in the method HikariGFXDPool you create a local variable and the class variable protected HikariDataSource ds = null; remain null. So you cannot get the connection.

The best way is to use a separate class to make and get the connections

something like 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 you get the connection using:

Connection conn = DBHandler.getConn();
// query
conn.close();
                This answer address the question because I Think that the real problem is that in the method HikariGFXDPool he create a local variable and the class variable protected HikariDataSource ds = null; remain null. So he cannot get the connection. I can Update My answer to ensure this concept
– Andrea Catania
                Jan 27, 2016 at 10:31
                Yes, I think it will be very helpful to point out the problem and explain how your code solves that problem.
– Mark Rotteveel
                Jan 27, 2016 at 10:33
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.