1

我正在尝试使用他们网站上的测试代码来测试 BoneCP。但是,我似乎无法真正建立与在线托管服务器的连接。我的代码在下面,因为我是菜鸟,所以我可能会犯新手错误。感谢您的帮助!

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

}

4

1 回答 1

3

正如您在评论中所说,JDBC URL 必须具有要连接的主机和数据库。因此,您缺少要连接的数据库。所以 JDBC URL 应该是这样的:

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

注意:我假设数据库的主机是 mysql.lms.shawnathon.com

于 2012-11-19T19:58:07.870 回答