2014-03-13 103 views
0

我嘗試使用下面的代碼從Java連接到MySQL數據庫:從Java連接到MySQL 7

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: 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. 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:525) 
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) 
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1121) 
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:355) 
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2479) 
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2516) 
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2301) 
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:834) 
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:525) 
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) 
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:416) 
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:317) 
    at java.sql.DriverManager.getConnection(DriverManager.java:579) 
    at java.sql.DriverManager.getConnection(DriverManager.java:221) 
    at jobimtext.util.db.DatabaseConnection.openConnection(DatabaseConnection.java:44) 
    at jobimtext.util.db.DatabaseResource.connect(DatabaseResource.java:154) 
    at jobimtext.thesaurus.distributional.DatabaseThesaurusDatastructure.connect(DatabaseThesaurusDatastructure.java:83) 
    at jobimtext.example.demo.IThesaurusDatastructureSimpleDistSimProb.<init>(IThesaurusDatastructureSimpleDistSimProb.java:36) 
    at jobimtext.example.demo.DistThresaurus.inizializeDT(DistThresaurus.java:49) 
    at jobimtext.example.demo.HolingInput$4.actionPerformed(HolingInput.java:544) 
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018) 
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341) 
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) 
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) 
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) 
    at java.awt.Component.processMouseEvent(Component.java:6505) 
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321) 
    at java.awt.Component.processEvent(Component.java:6270) 
    at java.awt.Container.processEvent(Container.java:2229) 
    at java.awt.Component.dispatchEventImpl(Component.java:4861) 
    at java.awt.Container.dispatchEventImpl(Container.java:2287) 
    at java.awt.Component.dispatchEvent(Component.java:4687) 
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832) 
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492) 
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422) 
    at java.awt.Container.dispatchEventImpl(Container.java:2273) 
    at java.awt.Window.dispatchEventImpl(Window.java:2719) 
    at java.awt.Component.dispatchEvent(Component.java:4687) 
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723) 
    at java.awt.EventQueue.access$200(EventQueue.java:103) 
    at java.awt.EventQueue$3.run(EventQueue.java:682) 
    at java.awt.EventQueue$3.run(EventQueue.java:680) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87) 
    at java.awt.EventQueue$4.run(EventQueue.java:696) 
    at java.awt.EventQueue$4.run(EventQueue.java:694) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) 
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:693) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151) 
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147) 
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139) 
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:97) 
Caused by: java.net.SocketException: Malformed reply from SOCKS server 
    at java.net.SocksSocketImpl.readSocksReply(SocksSocketImpl.java:128) 
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:459) 
    at java.net.Socket.connect(Socket.java:579) 
    at java.net.Socket.connect(Socket.java:528) 
    at java.net.Socket.<init>(Socket.java:425) 
    at java.net.Socket.<init>(Socket.java:241) 
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:259) 
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:305) 
    ... 56 more 

Connection connection = null; 
try { 
    connection = DriverManager 
    .getConnection("jdbc:mysql://localhost:3306/database","user", "password"); 
} catch (SQLException e) { 
    System.out.println("Connection Failed! Check output console"); 
    e.printStackTrace(); 
    return; 
} 

如果我用Java 7中,我得到以下錯誤編譯

如果我將編譯環境更改爲Java 6,則一切正常。 有人知道Java 7和JDBC之間是否存在不兼容? 我使用JDBC連接器的最新版本:使用mysql-connector-java的5.1.29-bin.jar

謝謝先進, 西爾維婭

+0

是你的databaseName '數據庫'? –

+1

我使用的Java 7和連接器5.1.22沒有問題 – Leo

+0

您的堆棧跟蹤顯示來自SOCKS server'你使用SOCKS服務器連接到本地主機'畸形的答覆?看起來像運行時環境問題,與編譯器無關 – 2014-03-13 18:51:35

回答

0

加載JDBC驅動程序,然後再試一次。

Connection connection = null; 
try { 
    Class.forName("com.mysql.jdbc.Driver"); 
    connection = DriverManager 
    .getConnection("jdbc:mysql://localhost:3306/database","user", "password"); 
} catch (SQLException e) { 
    System.out.println("Connection Failed! Check output console"); 
    e.printStackTrace(); 
    return; 
} catch (ClassNotFoundException e) { 
    System.out.println("Drive Class Not Found."); 
    e.printStackTrace(); 
    return; 
} 
0

使用此:

Connection conn = null; 
Statement stmt = null; 
try { 
    Class.forName("com.mysql.jdbc.Driver"); 
    conn = DriverManager.getConnection("DB_URL", "USER", "PASSWORD"); 
    stmt = conn.createStatement(); 
    String sql = "ANY_QUERY"; 
    stmt.executeUpdate(sql); 
} catch (SQLException se) { 
    se.printStackTrace(); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 
+0

感謝您的回覆。使用你的代碼我得到了java.sql.SQLException:找不到適合的驅動程序 – Silviutza

+0

@Silviutza:好的。讓我們試着分析問題,縮小潛在的問題來源! 1.您想使用哪個數據庫?哪個版本? 2.您想要使用哪種版本的連接器? – mig8