【问题标题】:Trying to connect to mysql with java and this shows up [duplicate]尝试使用 java 连接到 mysql 并显示 [重复]
【发布时间】:2020-01-28 14:26:35
【问题描述】:
package com.practise;

import java.sql.*;

public class connectSQL {

    public static void main(String[] args) {

        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/test", "root", "Root!123321");

            Statement stmt=con.createStatement();
            ResultSet rs=stmt.executeQuery("select * from testTable");

            while(rs.next()){
                System.out.println(rs.getInt(1)+"  "+rs.getString(2)+"  "+rs.getString(3));
                con.close();
            }

        }catch(Exception e){

            System.out.println("We got an exception...");
            System.out.println(e.getMessage());
        }
    }
}

输出:

Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
We got an exception...
The server time zone value 'EEST' 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.

【问题讨论】:

  • 您是否尝试过加载“com.mysql.cj.jdbc.Driver”或删除您正在加载驱动程序的那一行?似乎它希望您执行的操作是因为您选择的驱动程序不再受支持。
  • 如果该警告不是您尝试连接时发生的异常,请发布异常。
  • msql jdbc驱动版本是多少?

标签: java mysql jdbc


【解决方案1】:

看起来像一个警告消息,由 a

Class.forName("com.mysql.jdbc.Driver")

打电话。您的代码继续运行,因为它只是一个警告。

主要是告诉你驱动类的名字改成了com.mysql.cj.jdbc.Driver。所以,改为使用:

Class.forName("com.mysql.cj.jdbc.Driver")

它还让您知道,从 Java 6 (JDBC 4.0) 开始,通常不需要使用 Class.forName 手动加载驱动程序类,因为 JDBC 现在能够自己加载正确的驱动程序(前提是驱动程序jar 文件在类路径中可用)。

【讨论】:

    【解决方案2】:

    因为其他人已经回答了你。您必须使用新的驱动程序语法。

    Class.forName("com.mysql.cj.jdbc.Driver");
    

    MySQL 驱动程序 .jar 文件必须在您的 CLASSPATH 中。

    您还需要配置您的 mysql 服务器以避免时区异常。 您可以手动在服务器中设置 UTC 时区,也可以在 jdbc url 中使用它。

    jdbc:mysql://localhost:3306/test?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
    

    【讨论】:

      猜你喜欢
      • 2016-05-12
      • 2018-11-09
      • 2012-12-15
      • 1970-01-01
      • 2012-10-20
      • 1970-01-01
      • 2019-10-06
      • 2011-10-06
      • 2017-01-27
      相关资源
      最近更新 更多