"No suitable driver found" in Java database connection

Asked

Viewed 1,078 times

4

Well, I use the eclipse and I’m trying to connect a Mysql database with my project, my code, compared to other tutorials I found, it’s perfect, and it’s this:

package pack;

import java.sql.*;

import javax.swing.JOptionPane;


public class Banco {

public Statement stm;
public ResultSet rs;
public Connection conn;
public String Driver = "com.mysql.jdbc.Driver";

public void Conecta(){
    System.setProperty("jdbc.Drivers", Driver);
    try {
        conn = DriverManager.getConnection("jdbc:mysql:meu_caminho", "meu_login", "minha_senha");
        JOptionPane.showMessageDialog(null, "Conectado!");
    } catch (SQLException e) {
        JOptionPane.showMessageDialog(null, "Erro!" + "\n" + e.getMessage());
    }
}

public void Desconecta(){
    try {
        conn.close();
    } catch (SQLException e) {
        JOptionPane.showMessageDialog(null, "Erro ao fechar!");
    }
}

}

the problem is it gives the error "No suitable driver found for meu_caminho"

the solution everyone says is that I have to put the jdbc driver in my project, I tried to download the java connector on the Mysql site itself, but it is an msi file, and what all the tutorials say are to put the . jar, but I can’t find this jar at all, the only one I found was a 4shared link, a version 5.1.13 in . jar, but even after I add the library, the same error continues giving...

someone knows where I get this . jar?

Note: about this . jar I found, I put it in the project, I right-clicked, I went into properties, java build path, I added . jar, was created the Referenced folder Libraries, and when I open, there is the "com.mysql.jdbc.Driver" and yet, it does NOT connect...

1 answer

7


You need to load the class with.mysql.jdbc.Driver. Example:

public void Conecta(){
    //System.setProperty("jdbc.Drivers", Driver);
    try {
        Class.forName("com.mysql.jdbc.Driver"); //adicione essa linha
        conn = DriverManager.getConnection("jdbc:mysql:meu_caminho", "meu_login", "minha_senha");
        JOptionPane.showMessageDialog(null, "Conectado!");
    } catch (SQLException e) {
        JOptionPane.showMessageDialog(null, "Erro!" + "\n" + e.getMessage());
    }
}

Class.forName() causes the class passed as argument to dynamically load the class calling it. And your mistake rightly accuses the lack of the appropriate Driver.

I don’t know exactly what the System.setProperty("jdbc.Drivers", Driver); does, but it seems that it was a failed attempt to add the Driver class, comment on that line and add the line I indicated above.

  • Math, I got this answer from another question. I wanted to leave just one comment, from Java SE 6 (JDBC 4.0) explicit calls to Class.forName are no longer required (see documentation on Drivermanager). Have a strong galley using Java 5 or legacy drivers around :).

  • 1

    Yes, I had read this in the link of the duplicate question of this aq, then I was going to test to see if it proceeds, but I haven’t had time yet. Curious fact so many people having this kind of problem.

Browser other questions tagged

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