0

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

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
5
  • "SQLException: Could not create connection to database" - seems relevant Commented Jun 17, 2014 at 4:14
  • 1
    Do you have your Driver jar in your classpath? Commented Jun 17, 2014 at 4:22
  • Read this: Common Problems and Solutions Commented Jun 17, 2014 at 4:24
  • @RavinderReddy's link is now dead. Try this instead: dev.mysql.com/doc/connector-j/8.0/en/… Commented Aug 4, 2019 at 5:33
  • @Wodin: yes, the one I posted was in MySQL version 5.0. Seems access to documentation on versions 5.0 and earlier is revoked. Commented Aug 5, 2019 at 6:28

2 Answers 2

1

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...");
}
}
Sign up to request clarification or add additional context in comments.

3 Comments

thanks a lot, my connector was wrong as you told. But now, this error occurs: SQLException: Communications link failure due to underlying exception: I/System.out(1153): ** BEGIN NESTED EXCEPTION ** I/System.out(1153): android.os.NetworkOnMainThreadException I/System.out(1153): STACKTRACE: android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117) I/System.out(1153): at java.net.InetAddress.lookupHostByName(InetAddress.java:385) can you help me again?
It would be very useful if you post the full stacktrace and the failing class
I think here is better to see stackoverflow.com/questions/24276366/…
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.