2016-04-24 290 views
1

我無法連接到我的數據庫。我認爲這是一個我不瞭解的需要改變的地方。我在這裏有一些非常基本的代碼:MySQLNonTransientConnectionException:無法創建與數據庫服務器的連接。嘗試重新連接3次。放棄

public static void main(String[] args) 
{ 
    try 
    { 
     Connection con = DriverManager.getConnection("jdbc:mysql://IP:3306/TABLENAME?autoReconnect=true","USERNAME", "PASSWORD"); 
     con.close(); 
    } 

    catch(Exception e) 
    { 
     e.printStackTrace(); 
    } 
} 

顯然與登錄信息。它會導致這個錯誤:

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up. 

Caused by: 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. 

Caused by: java.net.ConnectException: Connection refused 

我已經通過大約10個不同的崗位看上去差不多的問題,並試圖解決他們仍然無法得到它的工作。

+0

http://stackoverflow.com/questions/2983248/com-mysql-jdbc-exceptions-jdbc4-communicationsexception-communications-link-fai –

+0

@FastSnail我嘗試了所有的BalusC技巧,但仍然遇到同樣的問題。 –

+0

也閱讀這篇文章http://stackoverflow.com/questions/6865538/solving-a-communications-link-failure-with-jdbc-and-mysql –

回答

0

當MySQL服務沒有運行並且您正嘗試連接到mysql數據庫時,會出現此問題。

要在Windows中解決這個問題,

  • 開始>>鍵入 '服務',並打開它。

  • 找到MySQL服務。可能與名稱MySQLxx。 xx是版本。
    例如。 MySQL56,MySQL57等

  • 啓動服務,現在嘗試連接到數據庫。

0

你必須使用數據庫名稱,而不是表名

0

增加mysql的最大連接,

SET GLOBAL max_connections = 1500; 

如果仍然收到錯誤,然後查詢可能要花費太長迴應。

在這種情況下,試圖增加在創建JDBC連接,

&maxReconnects=XX 
相關問題