Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No suitable driver found for 'jdbc:mysql://localhost:3306/mysql [duplicate]

Tags:

java

mysql

jdbc

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(); } 
like image 639
user994165 Avatar asked Nov 16 '11 04:11

user994165


People also ask

Could not connect to database no suitable driver found?

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.

How do you fix DriverManager error?

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.

How do I know if MySQL JDBC driver is installed?

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.


2 Answers

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"; 

See also:

  • Mini tutorial on MySQL + JDBC connectivity
like image 177
BalusC Avatar answered Sep 23 '22 01:09

BalusC


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

like image 22
prashant kumar Avatar answered Sep 22 '22 01:09

prashant kumar