2011-09-04 146 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