简体   繁体   English

如何在 Java - EDT 时区连接到数据库

[英]How to Connect to DB in Java - EDT time zone

I have an issue that I am facing while I am trying to perform automate testing.我在尝试执行自动化测试时遇到了一个问题。 I want to connect to DB and perform select query.我想连接到数据库并执行 select 查询。 Until now when The DB were in UTC time everything worked.到目前为止,当数据库处于 UTC 时间时,一切正常。 However today the DB team changed DB timezone to edt.然而,今天 DB 团队将 DB 时区更改为 edt。

this is my code:这是我的代码:

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;
   }




}

and this is the exception that I get:这是我得到的例外:

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)

this is the value in the build gradle这是构建 gradle 中的值

runtimeOnly 'mysql:mysql-connector-java:8.0.17'

When I run this command in the DB当我在数据库中运行此命令时

SELECT @@system_time_zone ;

The result is结果是

EDT

I do not need the time zone for my testing just to connect to the DB.我不需要时区来进行测试,只是为了连接到数据库。 untill yesterday in UTC it worked.直到昨天在UTC它才起作用。 what I should add to my function?我应该在我的 function 中添加什么? regards问候

You can override the timezone using the serverTimeZone parameter in the connection string URL, like this...您可以使用连接字符串 URL 中的serverTimeZone参数覆盖时区,如下所示...

"jdbc:mysql://localhost/test?serverTimezone=UTC"

see this URL看到 这个 URL

Thanks to roninjoe this is the solution感谢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"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM