【问题标题】:i want to connect to db on java, but there is some errors我想连接到java上的db,但是有一些错误
【发布时间】:2018-10-06 18:17:29
【问题描述】:
package dbb;

import java.sql.*;

public class test {
    public static void main (String[] args) {
        String url = "jdbc:mysql://localhost:3306/test";
        String user = "root";
        String pass = "1234";

        Connection conn = null;

        try {
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("Driver Searched");
            conn = DriverManager.getConnection(url, user, pass);
            System.out.println("Connection Succeed" + conn);
        } catch (ClassNotFoundException e) {
            System.out.println("Driver Not Searched");
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

我在 java 上编写了这段代码,但有一些错误。 例如, .

显示已经搜索到Driver,但还是连接失败。

我该如何解决这个错误?

【问题讨论】:

标签: java mysql jdbc


【解决方案1】:

正如评论中已经建议使用最新驱动程序的非 ssl 连接,您应该使用以下代码:

package dbb;
import java.sql.*;

public class test {
public static void main (String[] args) {
    String url = "jdbc:mysql://localhost:3306/test?useSSL=false";
    String user = "root";
    String pass = "1234";

    Connection conn = null;

    try {
        Class.forName("com.mysql.cj.jdbc.Driver");
        System.out.println("Driver Searched");
        conn = DriverManager.getConnection(url, user, pass);
        System.out.println("Connection Succeed" + conn);
    } catch (ClassNotFoundException e) {
        System.out.println("Driver Not Searched");
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}
}

【讨论】:

  • 还是有问题!!这里:java.sql.SQLException:服务器时区值 '´???¹?±¹ ???ؽ?'无法识别或代表多个时区。如果您想利用时区支持,您必须配置服务器或 JDBC 驱动程序(通过 serverTimezone 配置属性)以使用更具体的时区值。
  • 请分享堆栈跟踪。
  • SLL 部分只是警告
  • 这: 引起:com.mysql.cj.exceptions.InvalidConnectionAttributeException:服务器时区值'´???¹?±¹ ???ؽ?'无法识别或代表多个时区。如果您想利用时区支持,您必须配置服务器或 JDBC 驱动程序(通过 serverTimezone 配置属性)以使用更具体的时区值。
  • 请参考提供的网址link[link]
【解决方案2】:
public static void main(String[] args) {

    String url = "jdbc:mysql://localhost:3306/test";
    String user = "root";
    String pass = "Admin@123";

    java.sql.Connection conn = null;

    try {
        Class.forName("com.mysql.jdbc.Driver");
        System.out.println("Driver Searched");
        conn = DriverManager.getConnection(url, user, pass);
        System.out.println("Connection Succeed" + conn);
    } catch (ClassNotFoundException e) {
        System.out.println("Driver Not Searched");
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

mysql-connector-java-5.1.6jar 在我的系统中运行良好。可能这是 MySql 驱动程序的问题。

您可以尝试以下方法: 您可能需要在 jdbc url 中明确指定时区。:

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

【讨论】:

  • Connector/J 5.1.6真的很古老(2008年发布),最新的5.1.x是2018年3月的5.1.46,最新的版本是上周的8.0.11(2018 -04-19)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-19
  • 2010-09-27
  • 2014-08-22
  • 2022-01-23
相关资源
最近更新 更多