0

I am trying to setup a boneCP connection and I am getting the following error message:

Exception in thread "main" java.lang.ClassCastException: com.jolbox.bonecp.StatementHandle cannot be cast to com.mysql.jdbc.Statement

The connection seems to work fine but I get stopped out at the query.

Here is my code:

        BoneCP connectionPool = null;
    Connection connection = null;

    try {
        // load the database driver (make sure this is in your classpath!)
        Class.forName("com.mysql.jdbc.Driver");
    } catch (Exception e) {
        e.printStackTrace();
        return;
    }

    try {
        // setup the connection pool
        BoneCPConfig config = new BoneCPConfig();
        config.setJdbcUrl("jdbc:mysql://192.126.0.0:3306/"); // jdbc url specific to your database, eg jdbc:mysql://127.0.0.1/yourdb
        config.setUsername("root"); 
        config.setPassword("");
        config.setMinConnectionsPerPartition(5);
        config.setMaxConnectionsPerPartition(10);
        config.setPartitionCount(1);
        connectionPool = new BoneCP(config); // setup the connection pool

        connection = connectionPool.getConnection(); // fetch a connection

        if (connection != null){
            System.out.println("Connection successful!");
            Statement stmt = (Statement) connection.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT 1 FROM table"); // do something with the connection.
            while(rs.next()){
                System.out.println(rs.getString(1)); // should print out "1"'
            }
        }
        connectionPool.shutdown(); // shutdown connection pool.
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
scriptdiddy
  • 1,207
  • 3
  • 17
  • 31

1 Answers1

1

This is quite simple: If you use BoneCP, you do not have direct access to the objects of the underlying driver. This applies to connection pools in general, because they usually object proxies to handle resource management (eg close statements, resultsets etc when the connection is returned to the pool). This especially applies to statements as connection pools can (and usually do) also provide statement caching.

Specifically for BoneCP you should be able to get to the wrapped statement using StatementHandle.getInternalStatement() (although I am not 100% sure about that).

Although the big question is: why do you need to cast to com.mysql.jdbc.Statement, isn't the java.sql.Statement interface sufficient for you?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • I'm not sure how to answer your question? How should I structure my code to do a simple query? – scriptdiddy Jul 07 '12 at 19:39
  • You should normally talk to the JDBC interfaces only, and not to classes of a specific driver. In the code of you question there is nothing which cannot be done by simply using `java.sql.Statement`. So replace (what I expect) the import for `com.mysql.jdbc.Statement` with `java.sql.Statement` (this will also remove the need for the cast you apply). – Mark Rotteveel Jul 07 '12 at 19:44
  • Thank you for your help ^ !! Would you know how I can pass the connection to a action listener (thread) so I can query the database every iteration? – scriptdiddy Jul 07 '12 at 19:53
  • A separate question is probably better. Although needing a database connection in your action listener doesn't sound like a good design. – Mark Rotteveel Jul 07 '12 at 19:58