Skip to content Skip to sidebar Skip to footer

Could Not Create Connection To Database Server - Eclipse Mysql Error

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 { p

Solution 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.*;

publicclassConnectionTest {

privatestatic Connection con = null;

publicstatic 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());
        returnnull;
    }
    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());
        returnnull;
    }
    catch(Exception e){
        System.out.println(e.getMessage());
        returnnull;
    }
}

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

Solution 2:

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.

Post a Comment for "Could Not Create Connection To Database Server - Eclipse Mysql Error"