7

I am trying to write a program to connect to a MySQL database in eclipse, but I get the error "java.sql.SQLException: No suitable driver found".

The java code is:

import java.sql.*;

public class FirstExample {

//static final String S_JDBC_DRIVER = "com.mysql.jdbc.Driver";  
static final String S_DB_URL = "jdbc:mysql://localhost:3306/emp";
static final String S_USER = "root";
static final String S_PASS = "root";

public static void main(String[] args) {

    try {

        System.out.println("Connecting to database...");
        //Class.forName(S_JDBC_DRIVER);
        Connection connection = DriverManager.getConnection(S_DB_URL,
                S_USER, S_PASS);

        System.out.println("Creating statement...");
        Statement statement = connection.createStatement();
        String sql = "SELECT * FROM Employee";
        ResultSet resultSet = statement.executeQuery(sql);

        while (resultSet.next()) {

            int iId = resultSet.getInt("id");
            int iAge = resultSet.getInt("age");
            String sFirst = resultSet.getString("fname");
            String sLast = resultSet.getString("lname");

            System.out.print("ID: " + iId);
            System.out.print("\tAge: " + iAge);
            System.out.print("\tFirst: " + sFirst);
            System.out.println("\tLast: " + sLast);
        }

        resultSet.close();
        statement.close();
        connection.close();
    } catch (SQLException se) {

        for (Throwable t : se) {
            t.printStackTrace();
        }
    } 
    System.out.println("Goodbye!");
}

}

The output in the console tab is:

Connecting to database...
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/emp
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at FirstExample.main(FirstExample.java:21)
Goodbye!

I have used the MySQL Connector/J. It is unzipped in the MySQL installation directory and the jar file is added to the CLASSPATH.

Also refer to this image. There is an ! mark at the project root.image01

I get the error as in the next image: image02 when I include the 2 commented statements:

static final String S_JDBC_DRIVER = "com.mysql.jdbc.Driver";
Class.forName(S_JDBC_DRIVER);
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
RGB314
  • 177
  • 1
  • 4
  • 15

6 Answers6

4

For all but the most trivial applications the CLASSPATH environment variable is NOT used. Normally the libraries are include in the Class-Path entry in the manifest of the jar, or in the -cp option of the java commandline.

In this case you need to add the MySQL JDBC driver to the buildpath of your Eclipse project.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
4

I had the same problem. I solved it by adding:

Class.forName("com.mysql.jdbc.Driver");
Simon Adcock
  • 3,554
  • 3
  • 25
  • 41
André
  • 156
  • 1
  • 10
  • Another way to do is ```Class.forName(Driver.class.getName());```. I tried simply ```Driver.class.getName();``` and it didn't work, surprisingly enough. – André Willik Valenti Oct 03 '17 at 00:56
1

you can place the path like java -cppwd/mysql-connector-java-5.1.22-bin.jar:. <classname>.

make sure that your in same directory where the mysql driver is .

Hope that helps .

Raja G
  • 5,973
  • 14
  • 49
  • 82
1

Load Driver class just before getting the connection.

Use this code:

Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test_db", "user", "passw");
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Sumit Jadiya
  • 637
  • 6
  • 7
0

Alternatively you can also add the installed jar file to your eclipse project by selecting the project in eclipse, right click it and go down to properties, select the Java Build Path>>select the Libraries Tab>>Add external jar file and browse for the installed mysql-connector-java.jar file or any mysql java connector file int the /usr/share/java/ directory for most ubuntu users. Click okay and rebuild your project. Good luck

0

I encountered the same problem as you, but I handled it as follows: I copied the jar, which is called mysql-connector-java-5.1.23-bin.jar, into the \Apache Software Foundation\Tomcat 6.0\lib, and restarted tomcat. Hope that helps