Find the jdbc mysql driver and establish connection

Asked

Viewed 1,186 times

2

I’m trying to make a connection to java directly with jdbc. I inserted the mysql jar jdbc into the project properties, javabuildpath and adding the external jar. I am making the following example and it seems to me that it does not find the bank.

import java.sql.Connection;  
import java.sql.DriverManager;  
import java.sql.SQLException;  

public class Disciplina {  

private int idDisciplina;  
private String disciplina;  
private float cargaHoraria;  

public static void main(String args[]) throws SQLException {  
Connection conexao = DriverManager  
.getConnection("jdbc:mysql://localhost/ucsal");  
System.out.println("Conectado!");  
conexao.close();  
}  
}  

Note: throws exception, I will not treat because I want to see the error. And about the inserted jar, when I go in javabuildpatch, in the library tab, mysql jdbc has nothing inside the library. Everything is None (source target "None", java doc Location "None", Native library Location "None").

I just downloaded the jar, didn’t download the rest of the contents of the mysql_jdbc.zip folder.

2 answers

2

first- Check in your database server, if you actually created the database ucsal;

2nd- Missing add login and password access to your database server, in this case would look like this:

DriverManager.getConnection("jdbc:mysql://localhost/ucsal", "seuLogin", "suaSenha");

3º- I believe you haven’t added the driver incorrectly. One way to do this is to:

  1. creating a folder in your project, entitled lib;
  2. copy and paste your connectorJ in this directory;
  3. then add it to your classpath, if you are using the Eclipse, right click on Driver > build path > Add to Build Path.
  • Ok, it worked. Now I’m already doing the query’s. Gratro.

  • @Andrénascimento, blz. Now just mark as answer to the question. :)

-1


I made the connection class as follows:

package conect_DB;  

import java.sql.DriverManager;  
import java.sql.SQLException;  

public class Conexao {  

    public static java.sql.Connection conexao;    
    private static String servidor = "jdbc:mysql://localhost:3306/ucsal";    

    public static boolean conectar() {    
    try {    

        Class driverMysql = Class.forName("com.mysql.jdbc.Driver");    

        conexao = DriverManager.getConnection(servidor, "root", "");    

        if (conexao == null) {    

        return false;  
        }  
    }  

    catch (ClassNotFoundException e) {    
        System.out.println(e.getMessage() + " on method 'conectar' error");    
    }    

    catch (SQLException e) {    
        System.out.println(e.getMessage());    
    }    

    return true;    
    }    

    public static void desconectar() {    
    try {    
        conexao.close();    
    }    

    catch (SQLException e) {    
        System.out.println(e.getMessage());    
    }    

    catch (NullPointerException e) {    
        System.out.println("method 'close()' error. Not conection opened");    
    }    
    }    

} 
  • 4

    This is the solution to your question?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.