简体   繁体   中英

No suitable driver found for jdbc.mysql

I'm a student doing a course in Java and I've been stumped by an error I cannot resolve.

The error as mentioned above is

No suitable driver found for jdbc.mysql

I've imported the Library and specified it in code, I've also tried numerous solutions posted by users on stackoverflow and yet, nothing. If anyone has any further suggestions help would be greatly appreciated

My code

(I had to change my Xampp server conf file to listen to port 8080 because 80 was occupied by PID4 "System")

public class DBConnect {

  String DB_URL = "jdbc.mysql://localhost:3306/phpmyadmin/BCStationary?";

  public DBConnect() throws ClassNotFoundException {
    Connection conn = null;

    try {
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      Class.forName("com.mysql.jdbc.Driver");
      conn = DriverManager.getConnection(DB_URL, "root", "");
      System.out.println("Connection Successful");
    } catch (SQLException ex) {
      System.out.println("Conn error ");
      Logger.getLogger(DBConnect.class.getName()).log(Level.SEVERE, null, ex);
    }
  }
}

You have a typo in your URL, which is not following the standard protocol.

Use:

"jdbc:mysql://localhost:3306/..."

(note the colon instead of the dot between jdbc and mysql ).

Notes

  • You don't need the question mark at the end if you're not using parameters. But you will need either properties or GET-like parameters if your database requires authentication, etc.
  • I'm not sure about the phpmyadmin part. I suspect you'll need to remove that and point to your database name directly.
  • You don't need to explicitly register the driver. In Java <= 6, you will still need the reflective Class.forName invocation.

Class.forName("com.mysql.jdbc.driver"); should come before DriveManager.registerDriver .

DriveManager.registerDriver doesn't need to be use either.

You can simply your method by:

  public DBConnect() throws ClassNotFoundException {
    Connection conn = null;

    try {
      //Call to .newInstance() is a work around for some
      // broken java instances.
      Class.forName("com.mysql.jdbc.Driver").newInstance();
      conn = DriverManager.getConnection(DB_URL, "root", "");
      System.out.println("Connection Successful");
    } catch (SQLException ex) {
      System.out.println("Conn error ");
      Logger.getLogger(DBConnect.class.getName()).log(Level.SEVERE, null, ex);
    }
  }

If you are using the latest mysql drivers (mysql-connector 8.0) the class is com.mysql.cj.jdbc.Driver

https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-usagenotes-connect-drivermanager.html

 try{
    Class.forName("com.mysql.jdbc.Driver");
       Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306  /dbname","UserName","Password");

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

    please add mysql-3.0.5-connector jar  file
   there is not required to registerDriver and other 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM