简体   繁体   中英

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

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

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

你确定它是在端口3306上运行的mysql,并且你的连接器/ j支持它的版本吗?

我认为你没有启动你的PC中的MySql服务器。在运行你的应用程序之前

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM