我使用 Ubuntu 和 Net-beans。我从 Netbeans 向导创建了一个 Java 桌面数据库应用程序。我使用 MySQL 数据库,我可以从它的服务 → 数据库打开并运行查询。当我运行它时,我得到了下面的异常。当我清理并构建项目运行 jar 文件时,同样的异常。我在同一台机器双启动方式下安装了 Windows,也有相同的 MySQL 数据库和 Java。当我运行使用 Ubuntu Netbeans 制作的 jar 文件时,它在 Windows 中完美运行并显示数据库数据。我也采用了另一种方式,我使用相同的数据库在 Windows Netbeans 中创建了相同的 Java 应用程序,并且它在 Netbeans 和 jar 文件中都运行得很好。但是当我尝试在 Ubuntu 中运行它时,Netbeans 和 jar 文件中都显示以下异常。
[TopLink Info]: 2012.01.29 11:16:58.898--ServerSession(285416048)--TopLink, version: Oracle TopLink Essentials - 2.0.1 (Build b09d-fcs (12/06/2007)) Jan 29, 2012 11:16:59 AM org.jdesktop.application.Application$1 run SEVERE: Application class customerrecordsu.CustomerRecordsUApp failed to launch Local Exception Stack: Exception [TOPLINK-4002] (Oracle TopLink Essentials - 2.0.1 (Build b09d-fcs (12/06/2007))): oracle.toplink.essentials.exceptions.
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. Error Code: 0 at oracle.toplink.essentials.exceptions.DatabaseException.sqlException(DatabaseException.java:305)
... lot of more
Caused by: java.net.SocketException: Can't connect to SOCKS proxy:Connection refused at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:427)
... lot of more
下面是 'my.cnf' 文件删除评论的一部分。
[client]
port = 3306
socket = /var/run/mysqld/mysqld.sock
[mysqld_safe]
socket = /var/run/mysqld/mysqld.sock
nice = 0
[mysqld]
user = mysql
socket = /var/run/mysqld/mysqld.sock
port = 3306
skip-external-locking
bind-address = 127.0.0.1
下面是persistance.xml文件的一部分
<property name="toplink.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="toplink.jdbc.url" value="jdbc:mysql://localhost:3306/MyBusinessRecords"/>
<property name="toplink.jdbc.user" value="root"/>
<property name="toplink.jdbc.password" value="password"/>
Netbeans 向导使用 JPA 和 Top Link。我用纯 Java 创建了一个没有向导的程序,它在 Ubuntu 中按预期完美运行并显示了数据库数据。该程序如下。上下都使用相同的 Java MySQL connector.jar。
public static void main(String[] args) {
Connection con = null;
Statement st = null;
ResultSet rs = null;
int id = 0;
String name = null;
try {
String url = "jdbc:mysql://localhost:3306/MyBusinessRecords";
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection(url, "root", "5843");
if (con != null) {
System.out.println("A database connection has been establised!");
st = con.createStatement();
rs = st.executeQuery("select * from COUNTRIES");
while(rs.next()){
id = rs.getInt(1);
name = rs.getString(2);
System.out.println("id = " + id + " Name = " + name);
}
}
} catch (Exception e) {
System.out.println("Problem" + e.toString());
} finally {
if (con != null) {
try {
rs.close();
st.close();
con.close();
} catch (Exception e) {
System.out.println(e.toString());
}
con = null;
}
}
}
这是我的系统
Java 版本“1.7.0_01” Java HotSpot(TM) 64 位服务器 VM(构建 21.1-b02,混合模式) Ubuntu 11.10。操作系统类型 64 位 MySQL 版本- MySQL 5.1.58-1ubuntu1 MySQL 客户端版本 5.1.58 套接字:/var/run/mysqld/mysqld.sock
我在这个网站上阅读了 15 个以上的问答,但现在有一个帮助了我。请帮助我。
最佳答案
String url = "jdbc:mysql://localhost:3306/MyBusinessRecords";
Caused by: java.net.SocketException: Can't connect to SOCKS proxy: a non-existant SOCKS proxy bind-address = 127.0.0.1 You can try bind-address = 0.0.0.0 or jdbc:mysql://127.0.0.1:3306/MyBusinessRecords"
关于java - 数据库异常通信异常 : Communications link failure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9051517/