0

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

6
  • 3
    Can you print out a stack trace of the exception? Replace "System.err.println(...)" with e.printStackTrace(); Commented Jun 18, 2011 at 4:44
  • Are you sure the password and login are correct and can see that DB? Commented Jun 18, 2011 at 4:47
  • add this to your catch `e.printStackTrace();' Commented Jun 18, 2011 at 4:47
  • Try putting the Class.forName statement and the DriverManager.getConnection statement in different try clauses, to see if you're able to load the Driver. Commented Jun 18, 2011 at 4:50
  • I added the e.printStackTrace. And added the connector jar. Now I see: Cannot connect to database server java.sql.SQLException: Access denied for user 'myusername'@'localhost' (using password: NO) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073) and so on. How do i cross check my sql username? I know my password is correct. And why does it say "using password: NO) ? Commented Jun 18, 2011 at 5:05

5 Answers 5

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I just did this and added to jar to my project. Now I see: Cannot connect to database server java.sql.SQLException: Access denied for user 'myusername'@'localhost' (using password: NO) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073) and so on.
Then your database credentials or access privileges are incorrect if database server is running. Don't catch all exceptions, or elaborate them with more than one catch blocks.
0

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

1 Comment

I am running MySQL 5.5 and my connector is 5.1.16. Both are the latest.
0

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

Comments

0

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.

Comments

0

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 !!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.