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

This question already has answers here:
The infamous java.sql.SQLException: No suitable driver found
(21 answers)
Closed 7 years ago.
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();
}

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

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

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

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!

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"

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

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.

( If your url is correct and still get that error messege )
Do following steps to setup the Classpath in netbeans,
Create a new folder in your project workspace and add the downloaded .jar file(eg:- mysql-connector-java-5.1.35-bin.jar )
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.

Related

Error in Clean and Build project java 1.7 same code works in IDE netbeans (No suitable driver found) [duplicate]

This question already has answers here:
UCanAccess driver not found when converted to JAR file
(2 answers)
Closed 6 years ago.
I have developed a small game in which the text input by the user needs to be posted in a MS Access Database.
It all works fine in NetBeans but whenever I access it from the JAR file (which is ultimately what I need to hand to my client), it does not post anything to the database. In fact it returns the following error:
java.sql.SQLException: No suitable driver found for jdbc:ucanaccess://C:\Users\Paul\Desktop\Spelli\RispostiDB.mdb
This is the relevant code:
public void postAnsDB()
{
String tableName = "Form"+studentGroup +"_"+studentSurname+"_"+studentName+"";
answerModifier();
try{
String strurl="jdbc:ucanaccess://C:\\Users\\Paul\\Desktop\\Spelli\\RispostiDB.mdb";
Connection conn=DriverManager.getConnection(strurl, "", "");
Statement stmt=conn.createStatement();
//Post Student Details to DB
String post = "INSERT INTO "+tableName+"(ID, responses, Correct_Response, Valid_Invalid, Marks) VALUES ('"+ansID+"', '"+answer+"', '"+correct+"', '"+valid+"', '"+marks+"');";
stmt.executeUpdate(post);
}
catch(Exception e)
{
System.out.println("Exception found in postAnsDB: "+e);
}
}
When you build your project in NetBeans it creates a dist folder in the home folder of your project, puts the JAR file for your code in that folder, and also puts the dependencies (JAR files for required components like UCanAccess, Jackcess, HSQLDB, and Apache Commons bits) into a subfolder named lib. You need to ensure that the lib folder gets copied to the target machine and placed in the same folder as your main JAR file. Look here for details.
You could go for the fat-jar approach.
You can see it here if you use ant.
If have a maven project you could have this plugin to create a fat-jar
The benefit of a fat-jar application is that you could have everything inside one jar and don't need to worry with the lib dir like it was mentioned in another answer.
Also be careful with hard-coded paths like:
String
strurl="jdbc:ucanaccess://C:\\Users\\Paul\\Desktop\\Spelli\\RispostiDB.mdb";
that you have in your code.

Servlet and JAR on Apache Tomcat

I'm currently trying to get a servlet to perform a SQL query and return the results on a web page. I am using SQLite and the JAR has been added to my project class path. However, Eclipse informs me that the code:
Class.forName("org.sqlite.JDBC");
has a "ClassNotFoundException"
When I run the code the server gives me a message about the ClassNotFoundException.
When I first looked up help I read that I should put the JAR into the "lib" folder of Apache, which I did.
I also clicked on the Servers in my Eclipse Project, clicked "Profile As -> Profile Configurations" and added the JAR to the Classpath as well.
I also tried adding the JAR to the WEB-inf/lib as well as another answer suggested, but the problem still occurs.
It makes sense that the JAR needs to be on the server somewhere so that the servlet can find the class, I just don't know how to fix the problem.
"I also tried adding the JAR to the WEB-inf/lib as well as another answer suggested, but the problem still occurs."
That is where the JAR should physiclly be.
Then go properties> build path > add JARS and add the JAR
If you are using eclispe you dont need to setup by yourself paths
you need to add jdbc driver to right folder and then
by right folder I mean (WebContent -> WEB-INF -> lib) you can make this without using eclipse
find your workspace and pase the driver right there, after this refresh(F5) your project in eclpise and it should be there
to be sure, create new DynamicWebProject and paste this code
import java.sql.*;
public class SQLiteJDBC {
public static void main(String args[]) {
Connection conn = null;
Statement stat = null;
try {
Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection("jdbc:sqlite:test.db");
conn.setAutoCommit(false);
System.out.println("sqlite opened successfully");
stat = conn.createStatement();
// sql is the query check it online on sqlite page or sql queries
String sql = "INSERT INTO 'name of db and parameters to eqecute'";
stat.executeUpdate(sql);
stat.close();
conn.commit();
conn.close();
} catch (Exception e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
}
}
}

Connect to a MySQL DB with JSP through another class

Can I connect to a MySQL DB with JSP through another class?
Here is my my own suggestion:
...<body>
<h3>The User List</h3>
<br>
<%
// JSP/Java kode
UsersDAPInterface users = new UserDAPClass(); // This java class contains mysql connection etc.
%>
</body>...
When I do this, I get a lot of errors and exceptions, including ClassNotFound, NullPointer etc.
I also got this:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
Which should mean that it doesn't have the mysql driver.. But I have imported the mysql driver so I don't know if it should be imported in a different way than usually.
Any suggestions?
Make sure that this jar file in present in WEB-INF/lib folder of your application, as long as its present there, this issue should not come.
<jsp:useBean/> use this action to load any class to jsp. This is best practice.
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
This exceptions occurs due to your MySQL Connector Driver is not laoded. Use an IDE to load the driver. Manually check whether the com.mysql.jdbc.Driver is founded by writing it on Java class.
Download lib file from here and put it in your lib folder of your application. Have no idea what IDE are you using, but on Netbeans, take following steps:
Right click on your project
Select properties
In the category panel, select Libraries
Add jar folder just downloaded from Maven
You are ready.
You need to add jdbc driver class in your webapp library.

JDBC Driver not found error in Crystal Reports XI?

I installed crystal reports XI for the reports of my java application. When I tried to create a new mysql connection via the JDBC(JNDI) I got an error "Failed to open the connection. Details: JDBC Driver not found." So I followed the tutorials on the internet and edit CRConfig.xml to path my mysql driver. I placed it under the same directory where the other jar files were located but still I got the same error message. I also placed it on my environment variables but still no luck. Please help! I need this done by tomorrow.
edit: btw Im doing this in crystal reports. Im trying to create a report to preview on my application.
Something to check, if you have folder or filenames with spaces in them you should enclose them within quotation marks. If that's not the problem, copy your CRconfig classpath into your question, it might be some other issue.

Keeps getting Class Not Found Exception for Postgres driver in eclipse plugin

I am developing a simple plugin for eclipse, which has one view connecting to Postgres. I have installed postgres which is running fine. I have a driver:
postgresql-9.0-801.jdbc4.jar
which was provided with the installation package.
Now I'm trying to connect to database using the code:
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
return false;
}
Of course, I've added mentioned JAR to class path:
right click on project > preferences > Java Build Path
It is added to "Libraries".
Still I get an error:
java.lang.ClassNotFoundException: org.postgresql.Driver
I have also checked the box in "Order and Export" tab.
What am I doing wrong?
You said it's a plugin? Then you should NOT add it to lib path directly like that. You are developing an OSGi bundle and you must open MANIFEST.MF in the manifest editor, then at the Runtime tab, Classpath section, add your JAR. Also check at the Build tab that it's included in the build. Yeah, OSGi, another name for simplicity. Enjoy.

Categories