This question already has answers here:
Connect Java to a MySQL database
(14 answers)
Closed 4 years ago.
This question might have asked here number of times . After doing some google search for the above error and doing some update, I can't understand why I'm still getting that error. I've already put my driver-- mysql-connector-java-5.1.5-bin in the classpath:
Java_Home\jre\lib\
Java_Home\jre\lib\ext\
Java_Home\lib
and the code which I'm using to connect to mysql database is:
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mail","root","");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select message_body from deadletter");
String dbtime;
while (rs.next()) {
dbtime = rs.getString(1);
System.out.println(dbtime);
}
con.close();
}
catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
}
}
and the complete stacktrace of the above exception is:
java.lang.ClassNotFoundException: com.mysql.jdbc:Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:307)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at mail.main(mail.java:114)
Now, what's wrong I'm doing here?
If you're facing this problem with Eclipse, I've been following many different solutions but the one that worked for me is this:
Right click your project folder and open up Properties.
From the right panel, select Java Build Path then go to Libraries tab.
Select Add External JARs to import the mysql driver.
From the right panel, select Deployment Assembly.
Select Add..., then select Java Build Path Entries and click Next.
You should see the sql driver on the list. Select it and click first.
And that's it! Try to run it again! Cheers!
I too struggled with the same problem and finally got the solution for it.
Just copy the MySql-Connector.jar into Tomcat's lib folder, and then remove the jar from the webapp's lib folder, and then, run the project.
The most common cause is that you have some conflict in where your classes are loaded from. For example if you have 2 locations and one has JDBC drivers and the other one not then if your classloader loads from the 1st location and some class from the 1st location wants to use the driver - the driver is not there. So look for the duplicate JARs that are using your driver
I have the same problem
but I found this after a long search:
http://www.herongyang.com/JDBC/MySQL-JDBC-Driver-Load-Class.html
But I made some change. I put the driver in the same folder as my ConTest.java file,
and compile it, resulting in ConTest.class.
So in this folder have
ConTest.class
mysql-connector-java-5.1.14-bin.jar
and I write this
java -cp .;mysql-connector-java-5.1.14-bin.jar ConTest
This way if you not use any IDE just cmd in windows or shell in linux.
this ans is for eclipse user......
first u check the jdbc jar file is add in Ear libraries....
if yes...then
check...in web Content->web Inf folder->lib
and past here jdbc jar file in lib folder.....
1) Download connector from here https://www.mysql.com/products/connector/
2) Select JDBC driver for mysql
3) click on Platform Independent (Architecture Independent), ZIP Archive
4) Download the file and unzip it
5) (For Eclipse)Click Project->properties-> Java Build Path->Libraries
(For Netbeans) right click libraries on left bar-> add jar
6) Click on add external jar
7) select mysql-connector-java-5.1.40-bin.jar
8) Done!
If you are using NetBeans follow the following steps:
Right click on 'Libraries' and select 'Add Library..'
Scroll down the list of 'Global Libraries' and select 'MySQL JDBC Driver' and click on 'Add Library'.
What did you put exactly in lib, jre/lib or jre/lib/ext? Was it the jar mysql-connector-java-5.1.5-bin.jar or something else (like a directory)?
By the way, I wouldn't put it in lib, jre/lib or jre/lib/ext, there are other ways to add a jar to the classpath. You can do that by adding it explicitly the CLASSPATH environment variable. Or you can use the -cp option of java. But this is another story.
In NetBeans,
right click on libraries, select properties
select rub tab
there you need to add the mysql-connector-java.jar and apply
You should download MariaDB connector.
Then:
Expand your project.
Right-click on Libraries.
Select Add Jar/Folder.
Add mariadb-java-client-2.0.2.jar which you just downloaded.
Ok, i find solution changing the path of mysql-conector-java.jar to the follow path:
ProjectName/WebContent/Web-inf/lib/mysql-conector-java.jar
So you need to add the conector to project again and delete the previous one.
Most of the possible solution has been covered above. From my experience of this issue, i have placed the mysql-connector-jar in the /WEB-INF/lib folder of the webapp module and it worked fine for me.
In netbean
Right click on your project, then you click on Libraries, then Run tab, add library, then select mysql connector
In IntelliJ
Do as they say in eclipse
"If you're facing this problem with Eclipse, I've been following many different solutions but the one that worked for me is this:
Right click your project folder and open up Properties.
From the right panel, select Java Build Path then go to Libraries tab.
Select Add External JARs to import the mysql driver.
From the right panel, select Deployment Assembly.
Select Add..., then select Java Build Path Entries and click Next.
You should see the sql driver on the list. Select it and click first.
And that's it! Try to run it again! Cheers!"
Here we have to add the jar file in Project Structure -> Libraries -> +(add)
I keep the mysql-connector jar with my project rather than in Javahome. As a result, you can be sure it can be found by being sure its in the local classpath. A big upside is that you you can more the project to another machine and not have to worry about (or forget) to set this up again. I personally like including it in version control.
Ok..May be i can also contribute my solution..
Right click on project -properties -->Deployment assembly...there you need to add the mysql-connector-java.jar and apply it...which makes your prject configured with web-Libraries that has this sql connector...This worked for me..
I hope this works for you guys as well
The only thing that worked for me was downloading the mysql-connector-java-5.0.8-bin.jar directly from the MySQL website:
http://dev.mysql.com/downloads/connector/j/3.1.html
Then if you're using Eclipse paste this mysql-connector-java-5.0.8-bin.jar in the WEB-INF lib folder
If doesn't works try with any of the solutions posted in this link:
http://javarevisited.blogspot.com/2012/03/jdbc-javalangclassnotfoundexception.html
I experienced the same error after upgrading my java to 1.8.0_101. Try this:
i. Remove the mysql.jar fro your buildpath.
ii. Clean the server and project
iii. Add the jar file back to the build path.
Worked for me.
Just copy the MySQL JDBC drive jar file and paste it to Tomcat or whatever is the server's lib folder. It works for me.
just copy and paste the mysqlconnector jar to your project folder and then build path it will definitely work.
see to it that the argument of Class. forName method is exactly "com. mysql. jdbc. Driver".
If yes then see to it that mysql connector is present in lib folder of the project. if yes then see to it that the same mysql connector . jar file is in the ClassPath variable (system variable)..
I am using external jar in my mule flow. Jar file make database connection in postgres database. Standalone jar file working fine. But when I use this jar file in my mule it give error
Could not get JDBC Connection; nested exception is java.sql.SQLException: No suitable driver found for jdbc:postgresql://xxx/xxx
I have add postgres jar file in both application as external jar(mule application & standalone jar)
java.sql.SQLException: No suitable driver found for jdbc:postgresql exception come when Java don't find driver from jar file provided.
you have make sure that jar is correct and it present in your project class path.
The exception Could not get JDBC Connection; nested exception is java.sql.SQLException: No suitable driver found for jdbc:postgresql://xxx/xxx clearly says that the jar files is not detected in the classpath .. Now if you are using Mule Standalone you could add the jar in the location ${standaone_installation_folder}/lib/user
and if you are using Mule/anypoint studio you can add by right clicking the project and select java build path and then select add external jar where you select postgresql-jdbc3.jar
or alternately you can also create a lib folder in your project and put the jar file there and then right click on the jar file and add it into classpath
When using Anypoint studio, I would rather play by it's rules to make sure you overcome any classloader hierarchy problems. Postgresql is not supported with a simplified configuration, but in the documentation you'll find that the example actually explains how to use postgresql.
To install the database driver for a generic installation in Studio Visual Editor, follow the steps below:
If you haven't already done so, download the driver for your particular database. For example, the driver for a MySQL database is available for download online.
Add the driver's .jar file to the root folder in your project. In Studio, you can drag and drop the file from your local drive into the project folder.
Add the .jar file to the build path of your project. Right click the project name, then select Build Path > Configure Build Path…
In the wizard that appears, click the Libraries tab, then click Add Jars…
Navigate to the root folder in your project, then select the .jar file for your database driver.
Click OK to save, then OK to exit the wizard. Notice that your project now has a new folder named Referenced Libraries in which your database driver .jar resides.
I have java code which connects to a MS-SQL database and procures some data.
Before running the code I set the class path in the Unix Server and it used to work fine.
But for some reason the same jar file which was working a few days back is throwing a Class not found exception java.lang.ClassNotFoundException: net.sourceforge.jtds.jdbc.Driver
I have un-jared the jar file and found that the driver class which I'm loading :
Class.forName("net.sourceforge.jtds.jdbc.Driver");
is present in the specified package structure.
I don't understand how this is happening.
Can anyone suggest any tips to resolve this.
The issue of java.lang.ClassNotFoundException might be the outcome of several reasons:
Jar file is not available in class path. (This is not your case. as
you have mentioned).
Your jar file is corrupted.
Your environment variable value is not accessible by current user.
(Permission revoked by administrator).
Current user don't have permission to read jar file.
Please verify the last three options.
Right button on project-> Properties - > Java built Path -> Order and Export -> And check checkBox with you jar.
If you connect on Android project to Use 1.2.7 version.
I know it's late, but for others facing the same problem..
If you are using eclipse:
1. Go to Project -> Properties -> Java Build Path -> Order and export.. and check the jar file for jtds.version.jar
2. Go to Run Configurations and add this jar file to the user defined jars.
Hope this helps..:)
Today I am doing Connectivity of SqlServer using JTDS driver. But when I run program
I found Below error
Java.lang.ClassNotFoundException: net.sourceforge.jtds.jdbc.Driver
Caused by: java.lang.ClassNotFoundException: net.sourceforge.jtds.jdbc.Driver in loader dalvik.system.PathClassLoader[/data/app/com.kmkm.lll-2.apk]
I already search on this topic and I found answer as
java -classpath .:jtds.jar MyApp
but I am confuse how and where I run this command?
Why this error comes?
I had the same problem with my Android app in Eclipse. What worked for me was to export the external jars with my app.
If you have already added the jtds-1.2.5 driver, good. Otherwise:
Right click on the Project > Properties > Libraries Tab
Add the jtds-1.2.5.jar file by clicking on "Add External JARs"
Next, in the Order and Export tab (Right next to Libraries in the Java Build Path window) check the box for jtds-1.2.5.jar (and any other external jars you might have).
This will export the .jar containing the driver with the .apk file.
If you are using eclipse, Try this steps
Right click on your project
Click Build Path > Configure Build Path...
Choose Libraries tag and click Add External JARs...
Point your jtds.jar and click OK.
Rebuild and run again.
If you use a third party library, make sure you have all its required dependencies, if it requires any.
Ok I am on Win Vista and correctly set my MAVEN_HOME, JAVA_HOME stuff. but I do not have a class path yet. I have also installed MySQL. now I have opened a Hibernate book and at the very first pages it says "make sure the jdbc driver is in your classpath" . I have also downloaded some Zip file that is ConnectorJ or some name like that which is basically the mySql driver for java... but my problem for now is this sentence that I have no clue how to do it: "make sure the jdbc driver is in your classpath"
would you please help me about this classpath thing?
thanks
Here is a good tutorial regarding, setting the class path. Further you might like to read Managing the Java classpath (Windows).
Having said that, you should not set the classpath for your driver in Windows environment variable. Instead, you should include that driver jar inside your IDE under project properties. But I noticed that you are actually using Maven. In this case you should look for the driver under Maven in order to fulfil that dependency. Maven will download the driver jar, if doesn't exist, and make it local.
In case, you are not using any IDE, then you can create a lib directory and tell the compiler that all required jars are in there, at the time of compilation/execution. You can find HOW, in the former link given above.
Since you are using Maven, you just need to put the JDBC driver as a dependency in your pom.xml file. Maven will add it to the classpath whenever it compiles/runs your application.
What you do when deploying your application is dependent on the technologies in use.
If it's a command line application create a %CLASSPATH% variable or add the path to the jdbc.jar file using the java -cp {path\to\jdbc.jar} option.
If it's a web application, you'll need to package the driver jar in your .war/.ear/.sar (the maven assembly plugin can do this) or include it in the ./lib folder of the application container and declare it as scope=provided in maven.
Steps to setup JDBC for Eclipse projects
Download JDBC zip archive from
https://dev.mysql.com/downloads/connector/j/5.0.html
Extract the file and copy the executable jar file to program files->Java->jdk->bin
Right click on the project and select Buildpath->Add external archives->(Jar file)
Compile the program
you can directly connect database by following steps:
1) download mysql-connector-java 5.0.8 and extract the files.
2) then place the folder in program files.
3) then simply add this library on your project by right clicking on it.
4) and here you go. Run your app with db connectivity.