user3629217
user3629217

Reputation: 41

How to fix: “No suitable driver found for jdbc:mysql://localhost/dbname”

Hi, I have this code here, but when the application starts showing me the error! How to fix it? :

ERROR :

"No suitable driver found for jdbc:mysql://localhost/database"

CODE :

try {
        connection = DriverManager.getConnection("jdbc:mysql://"+host+"/"+database,username,password);
        connection.setAutoCommit(false);
        System.out.println("Connected to MySQL!");

    } catch (SQLException e) {
        System.out.println("Couldn't connect to MySQL!");
        System.out.println(e.getMessage());
        System.exit(0);
    }

Upvotes: 0

Views: 272

Answers (2)

Sireesh Yarlagadda
Sireesh Yarlagadda

Reputation: 13716

Make sure, you have the right version of jar - something like this mysql-connector-java-5.1.14-bin.jar

Remember does the host argument includes the port also. (By Default 3306 is the port for MySQL.)

Load the class just like below

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

Then it works fine.

Upvotes: 1

Pravat Panda
Pravat Panda

Reputation: 1090

Ensure the MySQL jar(contains the driver class) is in the classpath. To load it before its used actually(may be before try block), you can call

Class.forName(<Fully qualified Driver Class name goes here>);

Upvotes: 0

Related Questions