我要连接到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=""发布于 2013-06-20 17:37:23
using password: NO -这意味着程序是而不是传递任何密码,在你的情况下是正确的。
由于您提到您正在从属性文件中读取值,因此在您发布的代码中我看不到您这样做。如果您在实际的代码中确实要从属性文件中读取值,并且MySQL服务器是remote服务器,请确保使用以下语句授予远程MySQL服务器上的相关权限
允许来自192.168.1.51的连接的grant all privileges on easylibdb1.* to 'root'@'192.168.1.51'
或
允许从任何地方发起连接的grant all privileges on easylibdb1.* to 'root'@'%'
发布于 2013-06-20 16:02:03
密码参数应设置为null,因为即使是空字符串""也表示有密码。
DriverManager.getConnection("jdbc:mysql://localhost:3306/easylibdb1","root",null)发布于 2013-06-20 15:59:06
将null作为密码传递,而不是空字符串。这应该能让它工作起来。
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/easylibdb1","root",null);根据我现在看到的情况,您实际上没有使用属性文件中的值。
https://stackoverflow.com/questions/17208146
复制相似问题