gpt4 book ai didi

java - 使用 Java 访问在线数据库

转载 作者:行者123 更新时间:2023-11-30 23:51:05 28 4
gpt4 key购买 nike

<分区>

我制作了一个访问数据库的 Java 程序。在开发阶段,我使用了一个本地数据库(XAMPP 和 MySQL),但是当我尝试访问 2 个不同的在线 数据库(一个商业数据库和一个免费数据库)时,我在这两种情况下都收到了以下异常:

SQLException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

我用 PHP 访问它们没有任何问题。

这是我的代码:

private String user = "user1";
private String pass = "pass1";
private String dbClass = "com.mysql.jdbc.Driver";
private String dbDriver = "jdbc:mysql://db4free.net:3306/DBNAME";
private Connection conn = null;

public boolean connect() {
boolean done = false;
//load driver
try {
Class.forName(dbClass).newInstance();
System.out.println("driver loaded"); // THIS IS BEING RETURNED
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
System.err.println(ex);
}
// Connection
try {
conn = DriverManager.getConnection(dbDriver, user, pass);
System.out.println("connected"); // THIS IS NOT BEING RETURNED
done = true;
} catch (SQLException ex) {
System.out.println("SQLException: " + ex.getMessage());
}
return done;
}

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