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条回答
Bombasti
2楼-- · 2020-02-02 01:20

Pass null as password instead of an empty String. That should make it work.

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

From what I see right now, you're actually not using the values from the properties file.

查看更多
登录 后发表回答