java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/dbname
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/dbname
Make sure you run this first:
Class.forName(com.mysql.jdbc.Driver);
This forces the driver to register itself, so that Java knows how to handle those database connection strings.
For more information, see the MySQL Connector reference.
You have to load jdbc driver
. Consider below Code.
try {
Class.forName(com.mysql.jdbc.Driver);
// connect way #1
String url1 = jdbc:mysql://localhost:3306/aavikme;
String user = root;
String password = aa;
conn1 = DriverManager.getConnection(url1, user, password);
if (conn1 != null) {
System.out.println(Connected to the database test1);
}
// connect way #2
String url2 = jdbc:mysql://localhost:3306/aavikme?user=root&password=aa;
conn2 = DriverManager.getConnection(url2);
if (conn2 != null) {
System.out.println(Connected to the database test2);
}
// connect way #3
String url3 = jdbc:mysql://localhost:3306/aavikme;
Properties info = new Properties();
info.put(user, root);
info.put(password, aa);
conn3 = DriverManager.getConnection(url3, info);
if (conn3 != null) {
System.out.println(Connected to the database test3);
}
} catch (SQLException ex) {
System.out.println(An error occurred. Maybe user/password is invalid);
ex.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/dbname
I had the same problem, my code is below:
private Connection conn = DriverManager.getConnection(Constant.MYSQL_URL, Constant.MYSQL_USER, Constant.MYSQL_PASSWORD);
private Statement stmt = conn.createStatement();
I have not loaded the driver class, but it works locally, I can query the results from MySQL, however, it does not work when I deploy it to Tomcat, and the errors below occur:
No suitable driver found for jdbc:mysql://172.16.41.54:3306/eduCloud
so I loaded the driver class, as below, when I saw other answers posted:
Class.forName(com.mysql.jdbc.Driver);
It works now! I dont know why it works well locally, I need your help, thank you so much!