0

I have already set the classpath to mysql-connector-java-5.0.8-bin.jar and compiled my class successfully. But when I run it I get:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

The code is:

import java.sql.*;

public class JdbcExample
{

    public static void main(String arg[])
    {
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con=DriverManager.getConnection("jdbc:mysql://loacalhost:3306/sample","root","root");
            Statement st=con.createStatement();
            ResultSet rs=st.executeQuery("select * from sample");
            while(rs.next())
            {
                System.out.println(rs.getString(1));
            }
            con.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}
mhasan
  • 3,703
  • 1
  • 18
  • 37
  • Did you try to follow these links, http://stackoverflow.com/questions/1585811/classnotfoundexception-com-mysql-jdbc-driver and http://stackoverflow.com/questions/17484764/java-lang-classnotfoundexception-com-mysql-jdbc-driver-in-eclipse – Priyantha May 11 '17 at 14:53
  • yup...in all these links people used some IDE...which I did not. They all messed up in placing the jar in the wrong folder. That is not the case here. I am not using an IDE. Plus kindly note that the class compiled successfully when I used javac -cp "my classpath" filename.java... Check this link's answer by Stephen to understand the problem better http://stackoverflow.com/questions/43918756/classnotfound-exception-for-com-mysql-jdbc-driver-even-after-adding-mysql-con?stw=2 – Rakshit P R Vashishta May 11 '17 at 14:58
  • I tried stephen's solution of mentioning the jar classpath even while running...but it did not work... – Rakshit P R Vashishta May 11 '17 at 14:59
  • Are you using eclipse or some IDE for this? Clearly the jar is not on the classpath somehow. Assuming your java file is in C:\test directory, can u try putting the jar file also in the same directory and then run the java code? – Aritra Chatterjee May 11 '17 at 14:59
  • kindly check the link I mentioned in my comment above ...u are not understanding the problem...and no I didnt use an IDE.. had jar unavailability been the problem... the class would not have compiled... – Rakshit P R Vashishta May 11 '17 at 15:01
  • @RakshitPRVashishta the link from your comment above is just pointing back to this question... Please post HOW your are executing your program – user85421 May 11 '17 at 15:04
  • I agree with @Carlos Heuberger – Priyantha May 11 '17 at 15:05
  • sorry wrong link ...wait for a minute I will give the correct link – Rakshit P R Vashishta May 11 '17 at 15:07
  • the link isn't that important, but we need to know how you are starting your program, more precisely, how are you "adding" the driver JAR (and which JAR it is) - Is the mysql connector called "mysql-con.jar"? – user85421 May 11 '17 at 15:09
  • I compiled using javac -cp mysql-connector-java-5.0.8-bin.jar JdbcExample.java and it compiled successfully...but on running I got this exception – Rakshit P R Vashishta May 11 '17 at 15:11
  • http://stackoverflow.com/questions/37254131/java-lang-classnotfoundexception-com-mysql-jdbc-driver-error-even-after-importi here's the link that I was talking about – Rakshit P R Vashishta May 11 '17 at 15:12
  • see my answer below... the driver is not used for compilation only on execution - the link is also very clear about that: "...when you run the application... `java -classpath...`" (-cp == -classpath) – user85421 May 11 '17 at 15:15
  • Can you put the mysql jar in the JRE lib directory and then try running? Do a echo %JAVA_HOME%(windows) or whereis java(Ubuntu) from command line to get the home directory and go to its lib dir and paste the mysql file there and then try to run? – Aritra Chatterjee May 11 '17 at 15:19

2 Answers2

1

Make sure you are "adding" the mysql-connector-java-5.1.42-bin.jar file to the classpath when starting your program (obviously it can be a different version number).

something like

java -cp .;mysql-connector-java-5.0.8-bin.jar JdbcExample

or

set CLASSPATH=...;mysql-connector-java-5.0.8-bin.jar
java JdbcExample

assuming:

  1. the JAR is in the current folder... if that works, consider putting the JAR in a 'central' place a use the complete path in above commands

  2. using Windows, otherwise the separator would be : instead of ;

  3. the class is in no package

for Ubuntu:

java -cp .:mysql-connector-java-5.0.8-bin.jar JdbcExample
user85421
  • 28,957
  • 10
  • 64
  • 87
0

Looks like more of a classpath issue. Try adding the jar manually to your project. I ran the same with mysql-connector-java-5.0.8.jar and I dont get this error.

Ganesh Hariharan
  • 23
  • 1
  • 1
  • 5