SQLException error while trying to connect to the database

1

I'm 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 /   at ConnectionFactory.getConnection (ConnectionFactory.java:32)

I've already checked if my bank name 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);
        }


    }

}
    
asked by anonymous 31.10.2017 / 22:45

1 answer

0

First, make sure the MySQL driver is in your classpath. You can download the driver version 8.0.8-dmr JAR here . When placing the driver in the classpath, this problem should be resolved.

But there is one more serious problem that you should solve. You are manipulating the database on the same swing thread. This may leave the application unresponsive. The reason is that the database may take a while to make the request, and during that time, your screen will be frozen. To resolve this, try to use the SwingWorker so as not to let the swing hang because it is doing operations in the database.

See also this answer for more information.

    
31.10.2017 / 23:02