简体   繁体   中英

DatabaseException CommunicationsException: Communications link failure

I use Ubuntu & Net-beans. I make a java desktop database application from Netbeans wizard. I use MySQL database which I can open & run query from its Services → Databases. When I run it I got the Exception below. When I clean & Build the Project run jar file, same Exception. I have installed Windows in the same machine dual boot way, and also have the same MySQL database & Java. When I run the jar file I make using Ubuntu Netbeans, it perfectly run in Windows and show the database data. I did the other way also, I make the same java application in Windows Netbeans using the same database and it run perfectly both withing the Netbeans and jar file. But when I try to run it in Ubuntu, both in Netbeans & jar file show the below Exception.

[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

Below is part of 'my.cnf' file removing comments.

[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

below is part of persistance.xml file

  <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 wizard use JPA, & Top Link. I make a program without wizard by pure Java, it worked perfectly in Ubuntu as expected and show the database data. That program is below. Both above and below use same 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;
        }
    }
}

This is my system

java version "1.7.0_01" Java HotSpot(TM) 64-Bit Server VM (build 21.1-b02, mixed mode) Ubuntu 11.10. OS type 64 bit MySQL Version- MySQL 5.1.58-1ubuntu1 MySQL Client Version 5.1.58 Socket: /var/run/mysqld/mysqld.sock

I read the Q & A more then 15 in this site with same question, but that now one helped me. Please help me.

Try Java 1.6. I had a similar issue and changed back to Java 1.6 (I had upgraded to 1_7) and the issue went away. Probably need a different toplink or OracleDriver release for 1_7.

  String url = "jdbc:mysql://localhost:3306/MyBusinessRecords"; 

由以下原因引起:java.net.SocketException:无法连接到SOCKS代理:不存在的SOCKS代理bind-address = 127.0.0.1您可以尝试bind-address = 0.0.0.0或jdbc:mysql://127.0.0.1 :3306 / MyBusinessRecords“

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM