Reputation: 11
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);
}
}
}
Upvotes: 1
Views: 221
Reputation: 1
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
Upvotes: 0
Reputation: 2935
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
Upvotes: 0
Reputation: 48404
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
phpmyadmin
part. I suspect you'll need to remove that and point to your database name directly.Class.forName
invocation. Upvotes: 1