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

问题描述:

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

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

我的课:

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

}

和我的错误:

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

您的代码运行正常,只需在classpath中提供 mysql-connector.jar .我们可以找到它

Your code is working fine just provide the mysql-connector.jar in classpath.We can find it here and the code of yours which I tried is :

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...");
}
}