Mysql使用corejava程序连接输出密码

我想连接到MySQL数据库。 在安装MySQL的时候,我没有给出任何密码,所以在我的程序中我做了同样的事情,但我在连接上遇到错误。 我正在使用属性文件来获取驱动程序,URL,用户名和密码。 请帮我。

这是我的代码:

try { Class.forName("com.mysql.jdbc.Driver"); con=DriverManager.getConnection("jdbc:mysql://localhost:3306/easylibdb1","root",""); } catch (Exception e) { System.out.println("Got Error While Connecting To Database...!"); e.printStackTrace(); } 

这是我的属性文件内容:

 driver=com.mysql.jdbc.Driver url=jdbc:mysql://192.168.1.51:3306/easylibdb1 user=root password="" 

using password: NO – 这意味着程序没有传递任何密码,在您的情况下是正确的。

由于您提到您正在从属性文件中读取值,因此我不会在您发布的代码中看到您这样做。 如果您确实在实际代码中读取属性文件中的值 ,并且MySQL服务器是远程服务器 ,那么请确保使用以下语句在远程MySQL服务器上授予相关权限

grant all privileges on easylibdb1.* to 'root'@'192.168.1.51'以允许来自192.168.1.51的连接

要么

grant all privileges on easylibdb1.* to 'root'@'%'以允许来自任何地方的连接

密码参数应设置为null因为即使空字符串""表示存在密码。

 DriverManager.getConnection("jdbc:mysql://localhost:3306/easylibdb1","root",null) 

在属性文件中删除密码后的2个引号。 所以password=""应该是password=

传递null作为密码而不是空字符串。 这应该使它工作。

 con = DriverManager.getConnection("jdbc:mysql://localhost:3306/easylibdb1","root",null); 

从我现在看到的,你实际上并没有使用属性文件中的值。

如果它真的是password=""你的属性文件中有什么,那么""将作为密码发送。 不是空字符串,而是两个引号。

 URL=jdbc\:mysql\://192.168.1.51:3306/easylibdb1 USER=root PASSWD= DRIVER=com.mysql.jdbc.Driver 

请将您的PASSWORD字段设置为空白..不要放引号。

 con = DriverManager.getConnection("jdbc:mysql://localhost:3306/easylibdb1","root",null);