jdbc connects to mysql8.0

jdbc connection mysql8.0 error:

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.

conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/nq123?&user=root&password=941125");

Change it to:

conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/nq123?useSSL=false&serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf-8&user=root&password=941125");

Also: jdbc driver driverclassname changed the original without cj has been marked as obsolete

The zone information is added after the url connection information  

Example:

package zju_001.maven_test_1;

import java.sql.DriverManager;
import java.sql.SQLException;
//import com.mysql.jdbc.Connection;
import java.sql.Connection;
//import com.mysql.cj.jdbc.Driver
import java.sql.DriverManager;
import java.sql.SQLException;

public class JDBC_test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String dbUrl = "jdbc:mysql://localhost:3306/mybatisDemo?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT";
		String dbUser = "root";
		String dbPwd = "941125";
		Connection conn = null;
		
		try {
			//Class.forName("com.mysql.jdbc.Driver");
			Class.forName("com.mysql.cj.jdbc.Driver");
			conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/nq123?useSSL=false&serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf-8&user=root&password=941125");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace ();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace ();
		}
	}
}
//Notice, do not import com.mysql.cj.jdbc.*
//or you will have problems!

class LoadDriver {
 public static void main(String[] args) {
     try {
         // The newInstance() call is a work around for some
         // broken Java implementations

         Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
     } catch (Exception ex) {
         // handle the error
     }
 }
}







Guess you like

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