0

I use database.properties to save my database information. However, it pop up this error, every time I run the code. I don't know which part of the database is wrong. Please help!

host: 127.0.0.1
port: 3306
database: spider
username: root
password: !QAZxsw2
driver: com.mysql.cj.jdbc.Driver
drivertype: MYSQL

ERROR Database:40 - java.sql.SQLNonTransientConnectionException: Cannot load connection class because of underlying exception: com.mysql.cj.exceptions.WrongArgumentException: Failed to parse the host:port pair '127.0.0.1:3306;databaseName=spider;user=root;password=!QAZxsw2;serverTimezone=UTC&amp'.

4
  • I use this string String connString = String.format("jdbc:%s://%s:%s;databaseName=%s;user=%s;password=%s;serverTimezone=UTC&amp", drivertype, host, port, database, username, password); as my connection string. Commented Aug 14, 2019 at 6:38
  • @user7294900 I googled it and the meaning of adding &amp is that "There are multiple params that need to be separated by &, but & is changed to &" Commented Aug 14, 2019 at 6:45
  • &amp isn't & and is irrelevant if no other parameter after. You should add more info to question Commented Aug 14, 2019 at 6:50
  • @user7294900 So, there's nothing wrong in my database.properties file? I thought the error is because of the file. Commented Aug 14, 2019 at 6:52

2 Answers 2

1

You seem to be using SQL Server JDBC driver syntax for the URL, while you are using the MySQL JDBC driver.

See MySQL Connector/J 8, Connection URL Syntax, the URL to connect to MySQL would be:

jdbc:mysql://127.0.0.1:3306/spider?user=root&password=!QAZxsw2&serverTimezone=UTC

Note that I also removed the & you had, which has no place in a URL, unless you are putting it in XML and want to escape the & between key-value pairs.

Given the syntax of JDBC URLs is undefined, except for the jdbc:<sub-protocol>: prefix, you cannot try to dynamically construct it like you are doing and expect it to work on different drivers. Each driver has their own syntax, and although there is considerable overlap in syntax, each has their own idiosyncrasies (if not outright outlandish syntax). Instead, use a single property for the entire URL, or use a driver-specific strategy.

Sign up to request clarification or add additional context in comments.

Comments

0

After I post this question, I found out that my connection string was wrong. jdbc:mysql://localhost:%s/%s?serverTimezone=UTC&"+"user=%s&password=%s It should be like that url pattern of mysql.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.