gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 05:52:12 27 4
gpt4 key购买 nike

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

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com