4

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=""
Jonathan
  • 20,053
  • 6
  • 63
  • 70
Rajesh Hatwar
  • 1,843
  • 6
  • 39
  • 58

9 Answers9

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

Rakesh
  • 4,264
  • 4
  • 32
  • 58
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)
blackpanther
  • 10,998
  • 11
  • 48
  • 78
2

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.

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
2

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

JREN
  • 3,572
  • 3
  • 27
  • 45
  • sorry i am stil geting this error : java.sql.SQLException: Access denied for user 'root'@'192.168.1.37' (using password: NO) – Rajesh Hatwar Jun 20 '13 at 09:09
0

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.

Joshua
  • 2,932
  • 2
  • 24
  • 40
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.

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
sachin
  • 1,447
  • 4
  • 14
  • 22
  • ya i did that but still i am getting like java.sql.SQLException: Access denied for user 'root'@'192.168.1.37' (using password: NO) – Rajesh Hatwar Jun 20 '13 at 09:18
  • So, you won't be access of the that particular IP address. Check the grant of the mysql. http://stackoverflow.com/questions/5016505/mysql-grant-all-privileges-on-database – sachin Jun 20 '13 at 09:35
0
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/easylibdb1","root",null);
Tilak Raj
  • 472
  • 4
  • 12
  • 2
    This answer turned up in the low quality review queue, presumably because you didn't explain the code. If you do explain it (in your answer), you are far more likely to get more upvotes—and the questioner actually learns something! – The Guy with The Hat Jun 26 '14 at 15:07
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=
Chameera Dulanga
  • 218
  • 5
  • 17
0

You have left the password field empty. However, for connecting to a MySQL database, you typically need to provide a valid password.

In your case, you can pass "null" as the password and it should do the trick.

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

But generally, having a password improves security. Hope this helps!

Sujata R
  • 51
  • 4