Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我想连接到java上的db,但有一些错误_Java_Mysql_Jdbc - Fatal编程技术网

我想连接到java上的db,但有一些错误

我想连接到java上的db,但有一些错误,java,mysql,jdbc,Java,Mysql,Jdbc,我在java上编写了这段代码,但是有一些错误。 例如 它显示驱动程序已被搜索,但连接仍然失败 如何修复此错误?正如注释中所建议的,要使用最新驱动程序的非ssl连接,您应该使用以下代码: package dbb; import java.sql.*; public class test { public static void main (String[] args) { String url = "jdbc:mysql://localhost:3306/test";

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

它显示驱动程序已被搜索,但连接仍然失败


如何修复此错误?

正如注释中所建议的,要使用最新驱动程序的非ssl连接,您应该使用以下代码:

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();
        }
    }
}
在我使用
mysql-connector-java-5.1.6jar的系统中运行良好。这可能是MySql驱动程序的问题

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

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();
    }
}

将stacktraces添加为文本而不是尽可能重复的图像:您可以执行错误日志建议的操作:使用
com.mysql.cj.jdbc.Driver
;通过向url添加
usesll=false
来禁用SSL。时区值看起来像SSL握手中的某个值。可能的重复值仍然存在问题!!此处:java.sql.SQLException:无法识别服务器时区值''''。必须配置服务器或JDBC驱动程序(通过serverTimezone配置属性)若要使用时区支持,请使用更具体的时区值。请共享stack trace.SLL部分只是警告,这是由:com.mysql.cj.exceptions.InvalidConnectionAttributeException引起的:服务器时区值''''''???ª''½''无法识别或表示多个时区。如果要利用时区支持,必须配置服务器或JDBC驱动程序(通过serverTimezone配置属性)以使用更具体的时区值。请参考提供的URL[link]Connector/J 5.1.6非常古老(2008年发布),最新的5.1.x从2018年3月起为5.1.46,上周发布的最新版本为8.0.11(2018-04-19)。
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();
    }
}
String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";