java - 为什么 BoneCP 不建立连接?

标签 java bonecp

我正在尝试使用他们网站上的测试代码来测试 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) {
            }
        }
    }
}

最佳答案

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

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

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

关于java - 为什么 BoneCP 不建立连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13460491/

相关文章:

Java Play 2.2.2 BoneCp 0.8.0.RELEASE MySql 超时连接

java - 将 ADT 从 21.0 更新到 21.1

java - 从 JDBC ResultSet 中以字符串形式读取日期时指定的日期格式在哪里

mysql - Tomcat启动时如何避免分钟延迟

java - 如何修复 BoneCP 中的 : Exception in thread "main" java. lang.NoClassDefFoundError

jdbc - 当连接返回到池时,BoneCP(或任何其他池)是否关闭连接的语句?

java - 嵌入 YouTube 视频不起作用

java - 如何使用 Play! 实现预定的一次性流程v1.2.4 和 Heroku Scheduler 插件

java - JComboBox 未在 jTable 中打开