0
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 com.jdbd.connection.ConnectionDemo.main(ConnectionDemo.java:13)

here is my code

package com.jdbd.connection;

import java.sql.*;

public class ConnectionDemo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try{

            //1. get a connection to database

            Connection myconn = DriverManager.getConnection("jdbc:mysql@localhost:3306:emp","root","Dreamliner787");
            //2.create a statement
            Statement mystm =myconn.createStatement();
            //3. Execute sql query
            ResultSet myRs = mystm.executeQuery("select*from employee");
             //4. process the result set
            while(myRs.next()){
                System.out.println(myRs.getString("last")+ ","  + myRs.getString("first"));

            }

        }
        catch(Exception e){
            e.printStackTrace();

        }
    }

}

1 Answers1

1

The error is either because your URL is wrong, or the JDBC driver is missing.

A JDBC URL typically looks like this jdbc:mysql://localhost:3306/mysql. I'm not sure why you have an @ in there. But that probably is the problem.

You can pinpoint if the problem is in the classpath by loading the driver like this.

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

EDIT :

The Class.forName isn't JDBC specific. It's simply loading the Driver class into the current class loader. Nothing related to databases there.

Prior to JDBC 4.0 you had to initialize the driver this way. I guess since this worked, you must be using a lesser version.

Thihara
  • 7,031
  • 2
  • 29
  • 56