【问题标题】:How to solve timezone error in Java EE project? [duplicate]如何解决 Java EE 项目中的时区错误? [复制]
【发布时间】:2021-02-01 04:16:10
【问题描述】:

我知道这个问题不是第一次讨论,但他们没有帮助我,请帮助(最好详细)。 我在 intellij IDEA 上创建了一个 EE Web 项目。 与 MySQL 连接(下图) 然后我把这段代码写在 index.jsp

    <%
try
{
    Class.forName("com.mysql.jdbc.Driver"); //load driver
    Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/dbuser","root","root"); //create connection
    
    if(request.getParameter("btn_login")!=null) //check login button click event not null
    {
        String dbemail,dbpassword;
        
        String email,password;
        
        email=request.getParameter("txt_email"); //txt_email
        password=request.getParameter("txt_password"); //txt_password
        
        PreparedStatement pstmt=null; //create statement
        
        pstmt=con.prepareStatement("select * from login where email=? AND password=?"); //sql select query 
        pstmt.setString(1,email);
        pstmt.setString(2,password);
        
        ResultSet rs=pstmt.executeQuery(); //execute query and store in resultset object rs.
        
        if(rs.next())
        {
            dbemail=rs.getString("email");
            dbpassword=rs.getString("password");
            
            if(email.equals(dbemail) && password.equals(dbpassword))
            {
                session.setAttribute("login",dbemail); //session name is login and store fetchable database email address
                response.sendRedirect("welcome.jsp"); //after login success redirect to welcome.jsp page
            }
        }
        else
        {
            request.setAttribute("errorMsg","invalid email or password"); //invalid error message for email or password wrong
        }
        
        con.close(); //close connection 
    }
    
}
catch(Exception e)
{
    out.println(e);
}
%>

运行后出现这个错误

"服务器时区值 'Öåíòðàëüíàÿ Àçèÿ (çèìà)' 无法识别 或代表多个时区。您必须配置 服务器或 JDBC 驱动程序(通过 serverTimezone 配置属性) 如果要使用时区,请使用更具体的时区值 支持。”

P.S:在 Itellej IDEA 时区是 UTC ,我也尝试在 mysql 中进行更改

SET @@global.time_zone = '+00:00';

【问题讨论】:

  • 您好,检查this 答案可能会有所帮助。
  • @Swati 我看了看,但我究竟如何添加到我的代码中?你能帮忙吗
  • 更改您的连接字符串,如"jdbc:mysql://localhost:3306/dbuser?useUnicode=true&amp;useJDBCCompliantTimezoneShift=true&amp;useLegacyDatetimeCode=false&amp;serverTimezone=UTC" 并查看是否有效。此外,该链接有很多建议,请尝试其他以及查看。
  • @Swati 但在这一行中,我们还必须将密码写入本地服务器,所以我应该在哪里写? "jdbc:mysql://localhost:3306/dbuser?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC"
  • 只需在您传递连接字符串的代码中将jdbc:mysql://localhost:3306/dbuser 更改为jdbc:mysql://localhost:3306/dbuser?useUnicode=true&amp;useJDBCCompliantTimezoneShift=true&amp;useLegacyDatetimeCode=false&amp;serverTimezone=UTC。此外,密码和用户名将相同,无需在那里进行任何更改。

标签: mysql jsp jdbc timezone


【解决方案1】:
 Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/dbuser","root","root"); //create connection

必须重写为

Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/dbuser?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC","root","root"); //create connection

This post was helpful

【讨论】:

    猜你喜欢
    • 2019-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-30
    • 1970-01-01
    • 1970-01-01
    • 2019-10-26
    • 2021-10-10
    相关资源
    最近更新 更多