Sqlexception error while trying to connect to database

Asked

Viewed 420 times

1

I am creating a project with JDBC. When I run the code it gives the following error message:

Exception in thread "AWT-Eventqueue-0" java.lang.Runtimeexception: java.sql.Sqlexception: No suitable driver found for jdbc:mysql://localhost:3306/Car At Connectionfactory.getConnection(Connectionfactory.java:32)

I already checked if the name of my bank is correct and the connector driver is already inside my project.

Code:

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

public class ConnectionFactory {

    public  Connection getConnection() {

        String url = "jdbc:mysql://localhost:3306/Carro";
        try {
            return DriverManager.getConnection(url, "root", "root");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }


    }

}
  • 2

    Did you add the mysql driver to classpath? The error states that the driver was not found.

1 answer

1

First, check that the Mysql driver is in your classpath. You can download The JAR from the 8.0.8-dmr driver here. When placing the driver in the classpath, this problem should be solved.

But, there is one more serious problem you must solve. You are manipulating the database on the same swing thread. This can make the application non-responsive. The reason is that the database may take a while to request, and during this period, your screen will be frozen. To solve this, try using the class SwingWorker so as not to let the swing crash by doing operations in the database.

See also that answer for more information.

Browser other questions tagged

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