2011-09-04 145 views
1

我想使用Java連接到我的MySQL數據庫。使用Java連接到MySQL數據庫

我正在使用JDBC,並且我安裝了驅動程序。 (com.mysql.jdbc.Driver)

唯一的問題就是,我不斷收到錯誤:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException The last packet sent successfully to the server was 0 milliseconds ago.

這裏是我的代碼:

Class.forName("com.mysql.jdbc.Driver"); 
Connection connection = DriverManager.getConnection("jdbc:mysql:/mydomain.com/mydatabase", "username", "password"); 

我不積極如何組合URL(以及我的用戶名和密碼),但我做了大量的研究。

我是唯一一個訪問我的數據庫和域的人,所以沒有必要詢問管理員。

我使用phpMyAdmin來創建數據庫並對其進行管理。我使用我的phpMyAdmin用戶名和密碼或什麼?

順便說一下,我的網站託管在Yahoo!小本生意。

所以我的問題是:

  • 如何進行連接網址是什麼?
  • 我的用戶名和密碼是什麼?
+1

http://dev.mysql.com/doc/refman/5.0/en/connector-j-reference-configuration-properties.html –

+0

到底在回答你的問題? – speedRS

+0

[Java與MySQL的連接]可能的重複(http://stackoverflow.com/questions/2839321/java-connectivity-with-mysql) – Nekto

回答

0

How do I make the connection URL?

你缺少你的URL正斜槓?我會一直以爲它會是這樣的:

jdbc:mysql://server/database 
1

我會說你缺少你的URL正斜槓。

Connection connection = DriverManager.getConnection("jdbc:mysql://mydomain.com/mydatabase", "username", "password"); 

或者我覺得您的訪問權限有問題。同樣的事情也發生在我身上,這是防火牆阻塞服務器端口的問題。所以確認情況並非如此。

0
Load the drivers for mysql 
Class.forName("com.mysql.jdbc.Driver"); 
connect with the data base 
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/widget_corp","dbuser","dbpassword");    
.....jdbc:mysql://this is use as it is 
.....127.0.0.1 this is the address of localhost You can use the word "localhost" insted of  "127.0.0.1" 
.....dbuser is the data base user 
.....dbpassword is the password of the dbuser 
.....3306 is the default port used for database