简体   繁体   中英

java jdbc mysql: how to store credentials in properties file with special characters?

I am trying to connect to MySQL using JDBC with a java program. The program reads from a properties file to get credentials (url,username,password). We use special characters such as ^,&,*,@ in the password.

a section of the properties file look something like this:

mysql.username=bob
mysql.password=th1$is^my@pa$$w*rd   (not the real password, but for the sake of discussion)

the error from the stack trace looks like this: ERROR: unable to connect to db: [43008] Access denied: for user 'bob'@'' (using password: YES)

I confirmed that the program is able to read the password (by printing it to the console). It looks like it's some sort of escape sequence that is required that I'm missing?

have search google and asked some coworkers but no one seem to know the answer.

any help is appreciated. thanks in advance.

the error from the stack trace looks like this: ERROR: unable to connect to db: [43008] Access denied: for user 'bob'@''

There's your answer, more specifically the 'bob'@'' snippet. Which tells you that the use bob at no host (or empty host) is trying to connect to your MySQL database, which I'm almost sure it's not what you are trying to do.

The solution lies in making sure you got the right url to connect to your database:

String url = "jdbc:mysql://"+HOST+"/" + DB; //Double check your HOST constant

Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection(url, userName, password);

I hope it helped. Cheers

EDIT:

It seems by the way that you are storing the pass the right way, the only characters you should scape in a property file are:

\t \n \r \\ \" \' \ (space) \= \:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM