The new version of mysql connection problem java

java connect mysql

An old version of mysql can be connected with the following code without problems.

Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
Wed Apr 04 10:00:51 CST 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:127)
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:95)
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:87)
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:61)
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:71)
	at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:76)
	at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:874)
	at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456)
	at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:234)
	at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:227)
	at java.sql.DriverManager.getConnection(DriverManager.java:664)
	at java.sql.DriverManager.getConnection(DriverManager.java:247)
	at JDBC.Jdbc.JdbcMysql(Jdbc.java:24)
	at JDBC.Jdbc.main(Jdbc.java:12)
Caused by: com.mysql.cj.core.exceptions.InvalidConnectionAttributeException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at com.mysql.cj.core.exceptions.ExceptionFactory.createException(ExceptionFactory.java:60)
	at com.mysql.cj.core.exceptions.ExceptionFactory.createException(ExceptionFactory.java:79)
	at com.mysql.cj.core.util.TimeUtil.getCanonicalTimezone(TimeUtil.java:126)
	at com.mysql.cj.mysqla.MysqlaSession.configureTimezone(MysqlaSession.java:385)
	at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:1406)
	at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:1005)
	at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:864)
	... 7 more

Then we will solve it one by one

Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.sh

When we first saw this prompt, we knew that the driver was in the wrong place, and we made a mistake.

Some students said that they have been using this all the time. Yes, it was used correctly before, but the new version has a new driver, so the new driver should be used here instead of the old one.

The solution is:

String driverName="com.mysql.jdbc.Driver";    改为   String driverName="com.mysql.cj.jdbc.Driver";

Let's look at the next one

Wed Apr 04 09:49:35 CST 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.

There is a truststore problem here.

Solution

String url="jdbc:mysql://localhost:3306/bk";  变为  String url="jdbc:mysql://localhost:3306/bk?useSSL=true";

We have solved all the information prompted by ok. Check out the exception below


java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.

This is caused by the library time not matching the system time.

Solution

String url="jdbc:mysql://localhost:3306/bk?useSSL=true"; 变为  String url="jdbc:mysql://localhost:3306/bk?serverTimezone=GMT&useSSL=true";

Well, after we have done the above operations, let's try the effect.





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326017064&siteId=291194637