Mysql Connection with out password using corejava

2020-02-02 00:27发布

I want to connect to a MySQL database. While installing MySQL I did not give any password, so in my program I did the same but I am getting error on connection. I am using properties file to get the driver, URL, username and password. Help me pleas.

This is my code:

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();
}

This is my properties file content:

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

标签: java mysql
7条回答
\"骚年 ilove
2楼-- · 2020-02-02 00:57

If it's really password="" what stands in your properties file, then "" will be sent as password. Not an empty string but the two quote signs.

查看更多
地球回转人心会变
3楼-- · 2020-02-02 01:05
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/easylibdb1","root",null);
查看更多
够拽才男人
4楼-- · 2020-02-02 01:08

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)
查看更多
成全新的幸福
5楼-- · 2020-02-02 01:10

Remove the 2 quotes after password in your properties file. So password=""should be password=

查看更多
ら.Afraid
6楼-- · 2020-02-02 01:13
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.

查看更多
唯我独甜
7楼-- · 2020-02-02 01:14

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

查看更多
登录 后发表回答