【问题标题】:Trying to connect to MySQL database through SIngleton pattern [duplicate]尝试通过 SIngleton 模式连接到 MySQL 数据库 [重复]
【发布时间】:2019-05-08 02:45:49
【问题描述】:

正如标题所说,我正在尝试通过单例设计模式连接到我的数据库,但它似乎没有连接,我找不到问题。

public class DatabaseConnection {
    private static Connection conn;

    static{
        try{
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/athentication", "root", "");

            System.out.println("connected");
        }catch(SQLException e){
            System.out.println(e);
        }
    }

    public static Connection getCon(){
        return conn;
    }
}

我也尝试通过 main 验证连接,但控制台中没有出现“已连接”消息,我也得到了这个:

java.sql.SQLException:服务器时区值“马来半岛标准时间”无法识别或代表多个时区。如果您想利用时区支持,您必须配置服务器或 JDBC 驱动程序(通过 serverTimezone 配置属性)以使用更具体的时区值。

【问题讨论】:

    标签: java mysql sql


    【解决方案1】:

    尝试将时区附加到连接字符串。

    conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/athentication?serverTimezone=" + TimeZone.getDefault().getID(), "root", "");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-06
      • 1970-01-01
      • 2021-05-14
      相关资源
      最近更新 更多