2014-11-05 84 views
0

--CODE UPDATED--連接Mysql數據庫和Java

我試圖連接使用JDBC MySQL數據庫,但我不能夠得到的連接工作。我不確定我構建連接字符串的方式是否正確。下面的代碼和錯誤:

代碼:

Connection conn; 
    String userName = "root"; 
    String serverName = "127.0.0.1"; 
    String portNumber = "3306"; 
    String password = "password"; 
    String dbName = "myDB"; 

    /** 
    * Establishes connection with the sql database 
    * @throws SQLException 
    */ 
    public SQLConnector() throws SQLException { 
     setConnection(); 
    } 

    private void setConnection() throws SQLException { 
     Properties connectionProps = new Properties(); 
     connectionProps.put("user", this.userName); 
     connectionProps.put("password", this.password); 
     String connectionString = "jdbc:mysql://" + this.serverName 
       + ":" + this.portNumber + "/" + this.dbName; 
     System.out.println(connectionString); 
     this.conn = DriverManager.getConnection(connectionString, connectionProps); 
    } 

錯誤:

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(Unknown Source) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) 
    at java.lang.reflect.Constructor.newInstance(Unknown Source) 
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:377) 
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1036) 
    at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:627) 
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1013) 
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2234) 
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2265) 
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2064) 
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:790) 
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) 
    at java.lang.reflect.Constructor.newInstance(Unknown Source) 
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:377) 
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:395) 
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:325) 
    at java.sql.DriverManager.getConnection(Unknown Source) 
    at java.sql.DriverManager.getConnection(Unknown Source) 
    at com.source.database.SQLConnector.setConnection(SQLConnector.java:69) 
    at com.source.database.SQLConnector.<init>(SQLConnector.java:25) 
    at com.source.main.Start.main(Start.java:13) 
Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost. 
    at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:2914) 
    at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:559) 
    ... 18 more 
+2

請參閱http://stackoverflow.com/questions/2983248/com-mysql-jdbc-exceptions-jdbc4-communicationsexception-communications-link-fai – 2014-11-05 21:06:08

+0

而不要使用一個字段爲您的連接,它的獲取,使用和處置,永遠不會得到並保持;) – 2014-11-05 21:12:06

+0

@RC你爲什麼這麼認爲? – EternallyCurious 2014-11-06 18:48:32

回答

-1

我也試過用java連接mysql的某個時候回來。這是我做到的。

private Connection getConnection() throws SQLException 
    { 
     Connection conn = null; 
     Properties connectionProps = new Properties(); 
     connectionProps.put("user", this.userName); 
     connectionProps.put("password", this.password); 

     conn = DriverManager.getConnection("jdbc:mysql://" 
       + this.serverName + ":" + this.portNumber + "/" + this.dbName, 
       connectionProps); 

     return conn; 
    } 

public boolean executeUpdate(Connection connection, String command) throws SQLException 
    { 
     try 
     { 
      Statement st = connection.createStatement(); 

      st.executeUpdate(command); 

      connection.close(); 
     } 
     catch (Exception e) 
     { 
      System.err.println("Got an exception!"); 
      e.printStackTrace(); 
     } 

     return false; 
    } 

executeUpdate被用來更新,插入和從表中刪除。 希望有所幫助..

哦,而且JDBC驅動程序也必須在程序路徑中。

+0

我用你的代碼替換了我的代碼。仍然得到相同的錯誤。你能否確保你的連接字符串是正確的? – EternallyCurious 2014-11-06 18:37:58

+0

這裏是我使用的參考資料: [This](http://www.ccs.neu.edu/home/kathleen/classes/cs3200/JDBCtutorial.pdf)是教程 和[this](http: /www.ccs.neu.edu/home/kathleen/classes/cs3200/DBDemo.java)是用來運行它的.java文件。如果一切都正確,它應該運行,並且您的輸出將是您連接到數據庫並創建並刪除表。 讓我知道這是否有效:) – Tsar 2014-11-07 16:34:16

+0

此外,請檢查[this](http://stackoverflow.com/questions/2983248/com-mysql-jdbc-exceptions-jdbc4-communicationsexception-communications-link-fai)。 。相同的異常錯誤 – Tsar 2014-11-07 16:41:18