Mysql Connecting without a password using corejava

I want to connect to a MySQL database. When installing MySQL, I did not enter any password, so in my program I did the same, but I get an error message when connecting. I use the 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 property file contents:

 driver=com.mysql.jdbc.Driver url=jdbc:mysql://192.168.1.51:3306/easylibdb1 user=root password="" 
+4
source share
7 answers

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

Since you mention that you are reading values ​​from a properties file, I don’t see you doing this in the code you published. If you are really reading the values ​​from the properties file in your actual code , and the MySQL server is a remote server , make sure that you provide the appropriate permissions on the remote MySQL server with the instruction below

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

+3
source

The password parameter must be set to null , because even an empty string "" implies a password.

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

Pass null as a password instead of an empty string. This should make it work.

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

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

+2
source

Extract 2 quotes after the password in your properties file. Therefore password="" must be password=

+2
source

If this is really password="" that is in your properties file, then "" will be sent as a password. Not an empty string, but two characters.

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

Please set ur PASSWORD field to empty. Do not put a quote.

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

Source: https://habr.com/ru/post/1487229/


All Articles