jdbc连接mysql8.0 报错:
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");
改成:
conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/nq123?useSSL=false&serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf-8&user=root&password=941125");
还有:jdbc 驱动 driverclassname 改变了原先的不加cj的已经被标记为过时
url连接信息后边加入了zone信息
示例:
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
}
}
}