Question

I'm trying to test BoneCP using the test code on their website. However, I don't seem to be able to actually establish a connection to the server hosted online. My code is below maybe I'm making rookie errors since I'm a noob. THanks for your help!

 package org.chocolms.db;

 import java.sql.*; 
 import com.jolbox.bonecp.BoneCP;
 import com.jolbox.bonecp.BoneCPConfig;
 import com.jolbox.bonecp.BoneCPDataSource;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.Properties;

 public class ConnPoolTest
 {
public static void main(String[] args) throws Exception{

    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) {
    }

    try {
        // setup the connection pool

        BoneCPConfig config = new BoneCPConfig();
        config.setJdbcUrl("jdbc:mysql:mysql.lms.shawnathon.com");   //could the problem be here?
        // jdbc url specific to your database, eg jdbc:mysql://127.0.0.1/yourdb
        config.setUsername("removedforSecurity"); 
        config.setPassword("removedforSecurity");
        config.setMinConnectionsPerPartition(5);
        config.setMaxConnectionsPerPartition(10);
        config.setPartitionCount(1);

        System.out.print("code reaches here");
         connectionPool = new BoneCP(config);

          System.out.print("code doesn't reache here");     // where I'm stuck

        connection = connectionPool.getConnection(); // fetch a connection
   System.out.print("Or here");
        if (connection != null){
            System.out.println("Connection successful!");
            Statement stmt = connection.createStatement(); // do something with the connection.
            ResultSet rs = stmt.executeQuery("SELECT resource_name, BOOK.resource_id"
                                            + "FROM BOOK, RESOURCE "
                                            + "WHERE BOOK.resource_id = RESOURCE.resource_id;"); 

            while(rs.next()){
                System.out.println(rs.getString(1)); // printing everyting in column 1
            }
        }
        connectionPool.shutdown(); // shutdown connection pool.
    } catch (SQLException e) { System.out.print("cannot");
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
            }
        }
    }
}

}

Was it helpful?

Solution

As you put in the comments, the JDBC URL must have the host and database to connect. So you are missing the database that you want to connect. So the JDBC URL should be like this:

config.setJdbcUrl("jdbc:mysql://mysql.lms.shawnathon.com/database");

NOTE: I'm supposing that the host of the database is mysql.lms.shawnathon.com

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top