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();
        }
    }
}
13
  • Did you try to follow these links, stackoverflow.com/questions/1585811/… and stackoverflow.com/questions/17484764/… Commented May 11, 2017 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 stackoverflow.com/questions/43918756/… Commented May 11, 2017 at 14:58
  • I tried stephen's solution of mentioning the jar classpath even while running...but it did not work... Commented May 11, 2017 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? Commented May 11, 2017 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... Commented May 11, 2017 at 15:01

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

10 Comments

mysql-connector-java-5.1.42-bin.jar: command not found the jar is in the current folder,
mysql-connector-java-5.1.42-bin.jar: command not found this is what I get
I checked , the jar is in the currrent folder, I am using ubuntu, and the class is in no package
you have to use the driver you got - ...5.0.8-bin.jar - this answer was wrotten BEFORE you have given us the correct driver name. READ the end of my first sentence above: (obviously it can be a different version number)
changed answer to reflect the version you mentioned in the question's comment
|
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.

2 Comments

kindly follow up through all comments to my question before answering..u have not understood the problem.
again pls read through before suggesting links.... I have been through the entire process before asking the question..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.