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

匿名 (未验证) 提交于 2019-12-03 02:08:02

问题:

This question already has an answer here:

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(); } 

回答1:

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:



回答2:

you have to set classpath for mysql-connector.jar in eclipse using build path and also you have to put mysql-connector to the lib folder of WEB-INF Directory of Your web-app- ( If you are developing any web app).



回答3:

When using Netbean, go under project tab and click the dropdown button there to select Libraries folder. Right Click on d Library folder and select 'Add JAR/Folder'. Locate the mysql-connectore-java.*.jar file where u have it on ur system. This worked for me and I hope it does for u too. Revert if u encounter any problem



回答4:

This error happened to me, generally it'll be a problem due to not including the mysql-connector.jar in your eclipse project (or your IDE).

In my case, it was because of a problem on the OS.

I was editing a table in phpmyadmin, and mysql hung, I restarted Ubuntu. I cleaned the project without being successful. This morning, when I've tried the web server, it work perfectly the first time.

At the first reboot, the OS recognized that there was a problem, and after the second one, it was fixed. I hope this will save some time to somebody that "could" have this problem!



回答5:

A typographical error in the string describing the database driver can also produce the error.

A string specified as:

"jdbc:mysql//localhost:3307/dbname,"usrname","password" 

can result in a "no suitable driver found" error. The colon following "mysql" is missing in this example.

The correct driver string would be:

jdbc:mysql://localhost:3307/dbname,"usrname","password" 


回答6:

i had same problem i fix this using if developing jsp, put mysql connetor into WEB-INF->lib folder after puting that in eclipse right click and go build-path -> configure build patha in library tab add external jar file give location where lib folder is



回答7:

Just telling my resolution: in my case, the libraries and projects weren't being added automatically to the classpath (i don't know why), even clicking at the "add to build path" option. So I went on run -> run configurations -> classpath and added everything I needed through there.



回答8:

( If your url is correct and still get that error messege ) Do following steps to setup the Classpath in netbeans,

  1. Create a new folder in your project workspace and add the downloaded .jar file(eg:- mysql-connector-java-5.1.35-bin.jar )
  2. Right click your project > properties > Libraries > ADD jar/Folder Select the jar file in that folder you just make. And click OK.

Now you will see that .jar file will be included under the libraries. Now you will not need to use the line, Class.forName("com.mysql.jdbc.Driver"); also.

If above method did not work, check the mysql-connector version (eg:- 5.1.35) and try a newer or a suitable version for you.



回答9:

I was getting same error. I just removed the mysql-connector jar from my project and downloaded latest one. Cleaned my project and it works now.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!