Search code examples
javamysqljdbc

JDBC Driver Problem in a Dynamic Web Project


I have a problem with a Dynamic Web Project, I've been with this error for a couple of weeks and I can not solve it, it's about jdbc driver.

public class DatosDAO {

    private String url = "jdbc:mysql://localhost:3306/BCopia";
    private String usuario = "root";
    private String password = "";

    public DatosDAO() {}


    public boolean alta(Datos d) {

        try {
            Connection con = DriverManager.getConnection(url,usuario,password);
            PreparedStatement ps = con.prepareStatement("INSERT INTO datos VALUES (null, ?, ?, ?, ?)");
            ps.setString(1, d.getNombre());
            ps.setString(2, d.getDirectorioOrigen());
            ps.setString(3, d.getDirectorioDestino());
            ps.setInt(4, d.getIntervaloDias());
            ps.executeUpdate();

            con.close();
        } catch (Exception ex) {ex.printStackTrace();return false;}
        return true;
    }}

When attempt to run this method or any other method. I get the following error.

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/BCopia at java.sql.DriverManager.getConnection(Unknown Source) at java.sql.DriverManager.getConnection(Unknown Source) at modelo.DatosDAO.baja(DatosDAO.java:44) at control.Securalia.baja(Securalia.java:43) etc

I have the driver in lib and have the driver called in the build path and everything else, it is a dynamic web project. Can someone tell me what happens? The DB exists, etc.


Solution

  • First of all you have to add the database driver to the project library. after that you should mention the driver class name here's an example with firebird driver :

             Class.forName("org.firebirdsql.jdbc.FBDriver");
    

    Check your mysql jdbc driver to find the class name

    next you can establish a connection to you're database exactly like you've done with a connection URL and call the driver manager, here's an example :

             String connectionURL = "jdbc:firebirdsql://127.0.0.1:3030/c:\\db.fdb";
             Connection conn = DriverManager.getConnection(connectionURL, userName,password);
    

    good luck !