1

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

}

Abdi Osman
  • 43
  • 1
  • 7
  • What's the stack trace? Instead of catching `catch(SQLException e)` print the exception `{e.printStackTrace();}` You really need that information. "cannot" does not tell you the why, simply that an exception (by the way a very abstract exception) happened. – Daniel B. Chapman Nov 19 '12 at 19:17

1 Answers1

3

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

ChuyAMS
  • 480
  • 2
  • 9
  • Thanks, I did what you said and @DanielChapman said (use e.printStackTrace()), and I get the error message below. My username and password is correct i dont understand why access is denied. Does this error message tell you anything? java.sql.SQLException: Unable to open a test connection to the given database. JDBC url = jdbc:mysql://mysql.lms.shawnathon.com/chocoLMS, username = chocolms. Terminating connection pool. Original Exception: ------ com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Access denied for user 'chocolms'@'%.%.%.%' to database 'chocoLMS' – Abdi Osman Nov 19 '12 at 20:15
  • bad username/password can also mean a simple connection issue to the database. Is your instance up and running? I'd use a tool like `http://squirrel-sql.sourceforge.net/` to check your connection string. Once you have it right (and you're connecting) you can rule out a network issue. – Daniel B. Chapman Nov 19 '12 at 20:27
  • Like Daniel said, it seems is a bad password or username or the server is down. Also could be that your user don't have permission to the database. Can you access the database with other programs or with a web page?. Maybe the remote access is not allowed in the MySql server – ChuyAMS Nov 20 '12 at 14:17