Frage

I have been Googling this for the last 3 hours and I can not believe how little information there is on it. I have used the ODBC drivers to connect in C++ applications, PDO in PHP etc and I thought connecting in Java would be easy.

I get this exception java.sql.SQLException no suitable driver found. when I try

// Connect to the database
    try {
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/schema_name", "root", "");
    }
    catch(SQLException ex) {
        JOptionPane.showMessageDialog(NULL, ex);
        System.exit(0);
    }

I am using Netbeans and in the Services tab I can connect to the database perfectly and it says it is using the com.mysql.jdbc.Driver driver so it must be on the computer so it is really annoying me now.

Any help would be great thanks.

War es hilfreich?

Lösung

Ensure the the MySQL JDBC Driver is loaded into the classpath prior to attempting to connect to the database

DriverManager will automatically load the driver class com.mysql.jdbc.Driver.

Andere Tipps

You are not including the driver, try that

// Connect to the database
    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/schema_name", "root", "");
    }
   catch(ClassNotFoundException ex) {
        JOptionPane.showMessageDialog(null, "Driver not found\n"+ ex);
        System.exit(0);
    }
    catch(SQLException ex) {
        JOptionPane.showMessageDialog(null,  ex);
        System.exit(0);
    }

This is what, called Loading Class at Runtime.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top