Using Java, I get this error when attempting to connect to a mysql database:
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/mysql at java.sql.DriverManager.getConnection(Unknown Source) at java.sql.DriverManager.getConnection(Unknown Source) at MyTest1.main(MyTest1.java:28)
I'm using the mysql-connector-java-5.1.18-bin.jar
driver. It is in my build path. I have restarted MySQL. I've also logged on from the command line with root and no password and it connected fine. I'm not currently seeing a port 3306 in netstat. Previously I was getting a different error (I didn't change the code). The error was "jdbc mysql Access denied for user 'root'@'localhost password NO"
try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { String url = "jdbc:mysql://localhost:3306/mysql"; Connection con = DriverManager.getConnection(url, "root", ""); } catch (Exception e){ e.printStackTrace(); }
SQLException: No suitable driver found for 'jdbc:mysql://localhost:3306/mysql [Solution] This error comes when you are trying to connect to MySQL database from Java program using JDBC but either the JDBC driver for MySQL is not available in the classpath or it is not registered prior to calling the DriverManager.
If you are not import mysql connector jar file these type of error can occur. So you can download jar file related to the your version and you can paste it in your own project /WEB-INF/lib folder.
You can determine the version of the JDBC driver that you installed, by calling the getDriverVersion method of the OracleDatabaseMetaData class. You can also determine the version of the JDBC driver by executing the following commands: java -jar ojdbc5. jar.
In this particular case (assuming that the Class#forName()
didn't throw an exception; your code is namely continuing with running instead of throwing the exception), this SQLException
means that Driver#acceptsURL()
has returned false
for any of the loaded drivers.
And indeed, your JDBC URL is wrong:
String url = "'jdbc:mysql://localhost:3306/mysql";
Remove the singlequote:
String url = "jdbc:mysql://localhost:3306/mysql";
You have to set classpath for mysql-connector.jar
In eclipse, use the build path
If you are developing any web app, you have to put mysql-connector to the lib folder of WEB-INF Directory of your web-app
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With