0

I am trying to connect my JDBC to MySQL, but it gives me "no suitable driver found". This happens only on Windows, while on macOS it will work just fine. It gave me the same error on macOS, but I started MySQL server from MySQL preferences pane in Mac settings and made sure the password was correct in my code.

This is the main class

public class Library {
    public static void main(String[] args){
        final String DB_URL="jdbc:mysql://localhost";
        final String USER = "root";
        final String PASS = "root";

        // Open a connection
        try(Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
            Statement stmt = conn.createStatement();
        ) {
            String sql = "CREATE DATABASE Biblioteca";
            stmt.executeUpdate(sql);
            System.out.println("Database created successfully...");
        } catch (SQLException e) {
            System.out.println(e);
        }

        ConnectionProvider connection=new ConnectionProvider(USER,PASS,"Biblioteca");
        UserTable userTable=new UserTable(connection.getMySQLConnection());
        userTable.createTable("Users");
        AddressTable addressTable=new AddressTable(connection.getMySQLConnection());
        addressTable.createTable("Address");
        AdminTable adminTable=new AdminTable(connection.getMySQLConnection());
        adminTable.createTable("Admin");

This is the connection class

public final class ConnectionProvider {
    private final String username;
    private final String password;
    private final String dbName;

    /**
     * @param username the username used to connect to the database
     * @param password the password used to connect to the database
     * @param dbName the name of the database to connect to
     */
    public ConnectionProvider (final String username, final String password, final String dbName) {
        this.username = username;
        this.password = password;
        this.dbName = dbName;
    }

    /**
     * @return a Connection with the database specified in the class constructor
     * @throws IllegalStateException if the connection could not be establish
     */
    public Connection getMySQLConnection() {
        final String dbUri = "jdbc:mysql://127.0.0.1/"+dbName;
        try {
            // Thanks to the JDBC DriverManager we can get a connection to the database
            return DriverManager.getConnection(dbUri, this.username, this.password);
        } catch (final SQLException e) {
            throw new IllegalStateException("Could not establish a connection with db", e);
        }
    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Steve
  • 23
  • 6
  • Can you please check if all the services related to MYSQL are up on your windows machine ? if not then start it then try. – Vishal Yadav Jun 20 '22 at 15:06
  • The mysql driver jar must be on the classpath. The old usage with `Class.forName("com.mysql.jdbc.Driver");` will show this. So search the mysql-connector-java-_nn_-bin.jar – Joop Eggen Jun 20 '22 at 15:07
  • The "no suitable driver" error message doesn't have anything to do with MySQL running or not, it has to do with the driver not being on the classpath (or for very old versions, not having been explicitly loaded). See the duplicate. – Mark Rotteveel Jun 21 '22 at 08:40

0 Answers0