简体   繁体   English

无法使用Java连接到Mysql数据库

[英]Cannot connect to Mysql database using Java

I am running the following code on a Windows Server box using Java on Eclipse. 我在Eclipse上使用Java在Windows Server机器上运行以下代码。

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 我试过netstat -an,我看到了:TCP 127.0.0.1:4464 127.0.0.1:3306成立

My guess? 我猜? You haven't got the mysql jdbc connector jar in your classpath. 您没有在类路径中获得mysql jdbc连接器jar。 It should be called something like mysql-connector-java-5.1.16-bin.jar, depending on your version of mysql 它应该被称为mysql-connector-java-5.1.16-bin.jar,具体取决于你的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 ... 这很简单..你需要.jar文件名为mysql-connector-java-5.1.16-bin.jar ,,,下载并将其添加到你的libs ...

good luck !!! 祝好运 !!!

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

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

Try the followings: 请尝试以下方法:

  1. Clean and rebuild the project in eclipse. 在eclipse中清理并重建项目。
  2. Try to access the mysql database using the username and password in command prompt to ensure the username and password are correct. 尝试在命令提示符下使用用户名和密码访问mysql数据库,以确保用户名和密码正确。

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. 如果你想确保用户名和密码,你必须再次在mysql表中查询用户表,需要另一个mysql管理员帐户来查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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