java—尝试通过单例模式连接到mysql数据库

w6mmgewl  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(312)

这个问题在这里已经有答案了

mysql jdbc驱动程序5.1.33-时区问题(32个答案)
两年前关门了。
正如标题所说,我试图通过单例设计模式连接到我的数据库,但它似乎没有连接,我找不到问题所在。

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验证连接,但控制台中没有显示“connected”消息,我也得到了以下结果:
java.sql.sqlexception:服务器时区值“马来半岛标准时间”无法识别或表示多个时区。如果要利用时区支持,必须配置服务器或jdbc驱动程序(通过servertimezone配置属性)以使用更具体的时区值。

7d7tgy0s

7d7tgy0s1#

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

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

相关问题