我在尝试执行自动化测试时遇到了一个问题。我想连接到数据库并执行选择查询。直到现在,当数据库处于UTC时间时,一切正常。但是,今天数据库团队将数据库时区更改为edt。
这是我的代码:
public class DBSupport {
private final String ID = "id";
private final String CLASS = "com.mysql.cj.jdbc.Driver";
DBConfiguration dbConfiguration;
public String getUserIdFromDB(String newmail) throws Exception {
dbConfiguration = new DBConfiguration();
String query = "select id from users where email = '" + newmail + "' limit 1;";
String userId = null;
Class.forName(CLASS);
Connection conn = DriverManager.getConnection(dbConfiguration.getUrl(), dbConfiguration.getUserName(), dbConfiguration.getPassword());
Statement stmt = conn.createStatement();
ResultSet rowResult = stmt.executeQuery(query);
if (rowResult.next()) {
userId = rowResult.getString(ID);
System.out.println(userId);
}
conn.close();
return userId;
}
}
这是我得到的例外:
java.sql.SQLException: The server time zone value 'EDT' 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)
这是构建gradle中的值
runtimeOnly 'mysql:mysql-connector-java:8.0.17'
当我在数据库中运行此命令时
SELECT @@system_time_zone ;
结果是
EDT
我不需要时区进行测试,只需连接数据库即可。直到昨天在UTC工作。我应该添加什么功能?问候
您可以使用连接字符串URL中的serverTimeZone
参数来覆盖时区,像这样...
"jdbc:mysql://localhost/test?serverTimezone=UTC"
请参见this URL
感谢roninjoe这是解决方案
Connection con = DriverManager.getConnection("jdbc:mysql://DB_IP:DB_PORT/DB_NAME?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC","db_user","db_password"));