stumped
stumped

Reputation: 5

Cannot connect to Mysql database using Java

I am running the following code on a Windows Server box using Java on Eclipse.

Connection conn = null; // connection object  
Statement stmt = null; // statement object  
ResultSet rs = null; // result set object  
try{  
  Class.forName("com.mysql.jdbc.Driver").newInstance();  
  conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/errorcodes", "myusername", "mypassword");   
  System.out.println ("Database connection established");  
 }catch (Exception e){  
  System.err.println ("Cannot connect to database server");  
}

And i keep seeing the "Cannot connect to database server error". Any ideas what i might be doing wrong?
I have tried netstat -an and i see : TCP 127.0.0.1:4464 127.0.0.1:3306 Established

Upvotes: 0

Views: 11654

Answers (5)

waleed_Alrasheed
waleed_Alrasheed

Reputation: 1

It is simple .. you need .jar file called mysql-connector-java-5.1.16-bin.jar ,,, download it and add it to your libs ...

good luck !!!

Upvotes: 0

John
John

Reputation: 2782

Try the followings:

  1. Clean and rebuild the project in eclipse.
  2. Try to access the mysql database using the username and password in command prompt to ensure the username and password are correct.

If you want to ensure the username and password , you have to query the user table in mysql table again to need another mysql admin account to query.

Upvotes: 0

prasad.gai
prasad.gai

Reputation: 2987

i think you did not start the MySql server in your PC.before running your application

Upvotes: 0

Askar Kalykov
Askar Kalykov

Reputation: 2591

Do you sure that it is mysql running on port 3306 and that it's version is supported by your connector/j?

Upvotes: 0

Bohemian
Bohemian

Reputation: 425318

My guess? You haven't got the mysql jdbc connector jar in your classpath. It should be called something like mysql-connector-java-5.1.16-bin.jar, depending on your version of mysql

If you don't have that jar, visit here

Upvotes: 2

Related Questions