4

我想连接到 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=""
4

8 回答 8

4

using password: NO - this means the program is not passing any password, in your case that is correct.

Since you mention that you are reading the values from the properties file, I don't see you doing that in the code you have posted. If you are really reading the values from the properties file in your actual code, and the MySQL server is a remote server, then make sure that you grant relevant permissions on the remote MySQL server with the below statement

grant all privileges on easylibdb1.* to 'root'@'192.168.1.51' to allow connections originating from 192.168.1.51

or

grant all privileges on easylibdb1.* to 'root'@'%' to allow connections originating from anywhere

于 2013-06-20T09:37:22.663 回答
3

The password argument should be set to null because even an empty String "" implies that there is a password.

DriverManager.getConnection("jdbc:mysql://localhost:3306/easylibdb1","root",null)
于 2013-06-20T08:02:03.100 回答
2

null作为密码而不是空字符串传递。那应该使它起作用。

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

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

于 2013-06-20T07:59:06.460 回答
2

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

于 2013-06-20T08:00:23.083 回答
0

如果它确实password=""是您的属性文件中的内容,那么""将作为密码发送。不是空字符串,而是两个引号。

于 2013-06-20T07:57:49.273 回答
0
URL=jdbc\:mysql\://192.168.1.51:3306/easylibdb1
USER=root
PASSWD=
DRIVER=com.mysql.jdbc.Driver

Please set ur PASSWORD Field as blank.. Don't put quote.

于 2013-06-20T08:04:43.167 回答
0
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/easylibdb1","root",null);
于 2014-06-26T14:49:10.803 回答
0

According to you, you are passing username as root and password as empty string.And your connection is established through

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

So change your function as

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

Then it will work. Or change the password in the application.properties file as

password=
于 2020-02-16T15:50:06.383 回答