Twiip
Twiip

Reputation: 11

Connecting to a MySQL Database with Java

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 the 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:

Upvotes: 1

Views: 850

Answers (3)

NomanJaved
NomanJaved

Reputation: 1380

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

Upvotes: 0

Roshnal
Roshnal

Reputation: 1314

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.

Upvotes: 1

speedRS
speedRS

Reputation: 1240

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

Upvotes: 0

Related Questions