Ayrad
Ayrad

Reputation: 4056

MYSQLjava.sql.SQLException: No suitable driver

Having trouble connecting to a mysql database in java. It used to work fine a couple of days ago and I'm not sure what happened.

The URL seems to be constructed correctly and I can access the db using adminer. I also added mysql-connector-java-5.1.33.jar to my debug configuration classpath in eclipse. Not sure what's wrong here.

private final String URL = "jdbc:mysql://localhost:3306/test";
private final String USR = "test";
private final String PWD = "***";

...

  try {
            con = DriverManager.getConnection(URL, USR, PWD);
            st = con.createStatement();
            rs = st.executeQuery("SELECT VERSION()");

            if (rs.next()) {
                System.out.println(rs.getString(1));
            }

        } catch (SQLException ex) {
            System.out.println("Ereur de connection MYSQL" + ex);
            //Logger lgr = Logger.getLogger(Version.class.getName());
            //lgr.log(Level.SEVERE, ex.getMessage(), ex);

        } 

Upvotes: 0

Views: 55

Answers (1)

gprathour
gprathour

Reputation: 15333

From your code seems like you have not loaded the Driver class and the exception says the same thing too. No Suitable Driver. You need to do the following, before trying to create a connection.

Class.forName("com.mysql.jdbc.Driver");

Upvotes: 1

Related Questions