JDBC连接MySQL8中出现的问题

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.
Exception in thread "main" 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 [email protected]/com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
    at [email protected]/com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
    at [email protected]/com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
    at [email protected]/com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
    at [email protected]/com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73)
    at [email protected]/com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:76)
    at [email protected]/com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:835)
    at [email protected]/com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:455)
    at [email protected]/com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)
    at [email protected]/com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
    at JDBCproject/jdbcPackage.Main.update(Main.java:13)
    at JDBCproject/jdbcPackage.Main.main(Main.java:24)
Caused by: com.mysql.cj.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 java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
    at [email protected]/com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
    at [email protected]/com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:85)
    at [email protected]/com.mysql.cj.util.TimeUtil.getCanonicalTimezone(TimeUtil.java:132)
    at [email protected]/com.mysql.cj.protocol.a.NativeProtocol.configureTimezone(NativeProtocol.java:2243)
    at [email protected]/com.mysql.cj.protocol.a.NativeProtocol.initServerSession(NativeProtocol.java:2267)
    at [email protected]/com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:1319)
    at [email protected]/com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:966)
    at [email protected]/com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:825)
    ... 7 more

两处错误

mysql8版本中更新了驱动

新的连接字
"jdbc:mysql://localhost:3306/xxx?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT"
xxx是数据库实例名

加载具体的驱动类 Class.forName("com.mysql.jdbc.Driver"); 改为 Class.forName("com.mysql.cj.jdbc.Driver");

public static void update() throws Exception {
		// 1.导入驱动,加载具体的驱动类
		Class.forName("com.mysql.cj.jdbc.Driver");
		// 2.与数据库建立连接
		Connection connection = DriverManager.getConnection(MYSQLURL, "root", "admin");
		// 3.发送sql语句,执行命令(增删改,查)
		Statement stmt = connection.createStatement();
		String sql = "insert into emp(empno, ename, salary) "
				+ "values(2,'lisi', 8000); ";
		stmt.executeUpdate(sql);
		
		stmt.close();
		connection.close();
	}
发布了238 篇原创文章 · 获赞 104 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/hpu2022/article/details/104128037