开发者

Connecting to a MySQL Database with Java

开发者 https://www.devze.com 2023-04-02 04:28 出处:网络
I want to connect to my MySQL database with Java. I am using JDBC and I have the driver installed. (com.mysql.jdbc.Driver)

I want to connect to my MySQL database with Java.

I am using JDBC and I have the driver installed. (com.mysql.jdbc.Driver)

The only problem is that I keep getting an error:

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

Here is my code:

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

I am not positive how to compose the URL (and where I get my username and password) but I have done A LOT of research.

I am the only person with acess to my database and domain, so there's no use asking t开发者_开发技巧he admin.

I use phpMyAdmin to create the database(s) and manage them. Do I use my phpMyAdmin username and password or what?

By the way, my site is hosted on Yahoo! Small Business.

So my questions are:

  • How do I make the connection URL?
  • What is my username and password?


I would say you are missing a forward slash on your URL.

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

Or I have a feeling that there is something wrong with your access privileges. This same thing happened to me also and it was a problem of Firewall blocking the port on the server. So verify this is not the case.


How do I make the connection URL?

Are you missing a forward slash in your URL? I would've assumed it would be something like:

jdbc:mysql://server/database


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号