Java 无法创建与数据库服务器的连接 - Eclipse MySQL 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24255601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:06:20  来源:igfitidea点击:

Could not create connection to database server - Eclipse MySQL error

javaandroidmysqleclipse

提问by Lucas Yoshimura

can you help me? I already did the jdbc build path and my DB savetime exists.

你能帮助我吗?我已经做了 jdbc 构建路径并且我的数据库保存时间存在。

my class:

我的课:

    package br.com.savetime;

    import java.sql.*;

    public class CriarConexao {

    private static Connection con = null;

    public static Connection abrirBanco(){
        Connection con;
        try{
            Class.forName("com.mysql.jdbc.Driver");  
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/savetime", "root", "root");
            System.out.println("conectando");
            return con;
        }
        catch(ClassNotFoundException cnfe){
            System.out.println("driver nao encontrado: " + cnfe.getMessage());
            return null;
        }
        catch(SQLException sql){
            System.out.println("SQLException: " + sql.getMessage());
            System.out.println("SQLState: " + sql.getSQLState());
            System.out.println("Erro: " + sql.getErrorCode());
            System.out.println("StackTrace: " + sql.getStackTrace());
            return null;
        }
        catch(Exception e){
            System.out.println(e.getMessage());
            return null;
        }
    }

    public static void fecharBDcon(){
        try{
            con.close();
        }
        catch(Exception e){
            System.out.println("erro ao fechar o banco" + e.getMessage());
        }
    }

}

and my error:

和我的错误:

06-17 03:55:07.139: I/System.out(1367): SQLException: Could not create connection to database server.

06-17 03:55:07.139: I/System.out(1367): SQLState: 08001

06-17 03:55:07.139: I/System.out(1367): Erro: 0

06-17 03:55:07.149: I/System.out(1367): StackTrace: [Ljava.lang.StackTraceElement;@41bc1af0

采纳答案by SparkOn

Your code is working fine just provide the mysql-connector.jarin classpath.We can find it hereand the code of yours which I tried is :

您的代码工作正常,只需提供mysql-connector.jar类路径。我们可以在这里找到它,我尝试过的代码是:

import java.sql.*;

public class ConnectionTest {

private static Connection con = null;

public static Connection abrirBanco(){
    Connection con;
    try{
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "welcome");
        System.out.println("conectando");
        return con;
    }
    catch(ClassNotFoundException cnfe){
        System.out.println("driver nao encontrado: " + cnfe.getMessage());
        return null;
    }
    catch(SQLException sql){
        System.out.println("SQLException: " + sql.getMessage());
        System.out.println("SQLState: " + sql.getSQLState());
        System.out.println("Erro: " + sql.getErrorCode());
        System.out.println("StackTrace: " + sql.getStackTrace());
        return null;
    }
    catch(Exception e){
        System.out.println(e.getMessage());
        return null;
    }
}

public static void fecharBDcon(){
    try{
        con.close();
    }
    catch(Exception e){
        System.out.println("erro ao fechar o banco" + e.getMessage());
    }
}
public static void main(String arr[])
{
    System.out.println("Making connection..");
    Connection connection = ConnectionTest.abrirBanco();
    System.out.println("Connection made...");
}
}

回答by Rajeev Rahi

This is just the mismatch of mysql connector jar file. simply update your jar files with higher version or update your dependency if you are using maven. I updated my dependency in pom.xml with 6.0.6 version and it worked.

这只是 mysql 连接器 jar 文件的不匹配。只需使用更高版本更新您的 jar 文件或更新您的依赖项(如果您使用的是 maven)。我用 6.0.6 版本更新了 pom.xml 中的依赖项,并且它工作正常。