package com.ljy.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class JDBCDemo {
public static void main(String[] args) throws Exception {
//1. 注册驱动
Class.forName("com.mysql.jdbc.Driver");
// 2.获取连接
// 这里的db1是我自己创建的数据库,localhost是我的数据库主机号或者是ip地址(127.0.0.1)
// 3306是我数据库的端口号
String url="jdbc:mysql://localhost:3306/db1";
String username="root";
String password="1111";
Connection conn = DriverManager.getConnection(url,username,password);
// 3.定义sql
String sql = "update account set money = 2000 where id = 1";
// 4.获取执行sql的对象 Statement
Statement stmt = conn.createStatement();
// 5.执行sql
int count = stmt.executeUpdate(sql); // 受影响的行数
// 6.处理结果
System.out.println(count);
// 7.释放资源
stmt.close();
conn.close();
}
}
遇到以下报错:
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 com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:76)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:835)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:455)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:207)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:683)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:230)
at com.ljy.jdbc.JDBCDemo.main(JDBCDemo.java:21)
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.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:67)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:483)
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:85)
at com.mysql.cj.util.TimeUtil.getCanonicalTimezone(TimeUtil.java:132)
at com.mysql.cj.protocol.a.NativeProtocol.configureTimezone(NativeProtocol.java:2234)
at com.mysql.cj.protocol.a.NativeProtocol.initServerSession(NativeProtocol.java:2258)
at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:1319)
at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:966)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:825)
... 6 more
Process finished with exit code 1
第一次尝试修改代码:
将 com.mysql.jdbc.Driver 改为 com.mysql.cj.jdbc.Driver
如下:
package com.ljy.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class JDBCDemo {
public static void main(String[] args) throws Exception {
//1. 注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// 2.获取连接
String url="jdbc:mysql://localhost:3306/db1";
//String url= "jdbc:mysql://localhost:3306/db1?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8";
String username="root";
String password="1111";
Connection conn = DriverManager.getConnection(url,username,password);
// 3.定义sql
String sql = "update account set money = 2000 where id = 1";
// 4.获取执行sql的对象 Statement
Statement stmt = conn.createStatement();
// 5.执行sql
int count = stmt.executeUpdate(sql); // 受影响的行数
// 6.处理结果
System.out.println(count);
// 7.释放资源
stmt.close();
conn.close();
}
}
出现的错误如下:
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 com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:76)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:835)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:455)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:207)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:683)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:230)
at com.ljy.jdbc.JDBCDemo.main(JDBCDemo.java:21)
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.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:67)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:483)
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:85)
at com.mysql.cj.util.TimeUtil.getCanonicalTimezone(TimeUtil.java:132)
at com.mysql.cj.protocol.a.NativeProtocol.configureTimezone(NativeProtocol.java:2234)
at com.mysql.cj.protocol.a.NativeProtocol.initServerSession(NativeProtocol.java:2258)
at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:1319)
at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:966)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:825)
... 6 more
Process finished with exit code 1
第二次修改
将 jdbc:mysql://localhost:3306/db1
改为 jdbc:mysql://localhost:3306/db1?serverTimezone=GMT%2B8
package com.ljy.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class JDBCDemo {
public static void main(String[] args) throws Exception {
//1. 注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// 2.获取连接
String url="jdbc:mysql://localhost:3306/db1?serverTimezone=GMT%2B8";
String username="root";
String password="1111";
Connection conn = DriverManager.getConnection(url,username,password);
// 3.定义sql
String sql = "update account set money = 2000 where id = 1";
// 4.获取执行sql的对象 Statement
Statement stmt = conn.createStatement();
// 5.执行sql
int count = stmt.executeUpdate(sql); // 受影响的行数
// 6.处理结果
System.out.println(count);
// 7.释放资源
stmt.close();
conn.close();
}
}
出现的错误如下:
Thu Apr 21 10:50:32 CST 2022 WARN: Caught while disconnecting...
EXCEPTION STACK TRACE:
** BEGIN NESTED EXCEPTION **
javax.net.ssl.SSLException
MESSAGE: closing inbound before receiving peer's close_notify
STACKTRACE:
javax.net.ssl.SSLException: closing inbound before receiving peer's close_notify
at java.base/sun.security.ssl.SSLSocketImpl.shutdownInput(SSLSocketImpl.java:837)
at java.base/sun.security.ssl.SSLSocketImpl.shutdownInput(SSLSocketImpl.java:816)
at com.mysql.cj.protocol.a.NativeProtocol.quit(NativeProtocol.java:1312)
at com.mysql.cj.NativeSession.quit(NativeSession.java:182)
at com.mysql.cj.jdbc.ConnectionImpl.realClose(ConnectionImpl.java:1750)
at com.mysql.cj.jdbc.ConnectionImpl.close(ConnectionImpl.java:720)
at com.ljy.jdbc.JDBCDemo.main(JDBCDemo.java:37)
** END NESTED EXCEPTION **
Process finished with exit code 0
第三次修改:
将 jdbc:mysql://localhost:3306/db1?serverTimezone=GMT%2B8
改为: jdbc:mysql://localhost:3306/db1?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8
package com.ljy.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class JDBCDemo {
public static void main(String[] args) throws Exception {
//1. 注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// 2.获取连接
//String url="jdbc:mysql://localhost:3306/db1?serverTimezone=GMT%2B8";
String url= "jdbc:mysql://localhost:3306/db1?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8";
String username="root";
String password="1111";
Connection conn = DriverManager.getConnection(url,username,password);
// 3.定义sql
String sql = "update account set money = 2000 where id = 1";
// 4.获取执行sql的对象 Statement
Statement stmt = conn.createStatement();
// 5.执行sql
int count = stmt.executeUpdate(sql); // 受影响的行数
// 6.处理结果
System.out.println(count);
// 7.释放资源
stmt.close();
conn.close();
}
}
结果如下:
1
Process finished with exit code 0