Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

I am getting a java.lang.ClassnotFoundException while running this program:

import java.lang.*;
import java.sql.*;
import java.io.*;
import java.util.*;

class MysqlCon{
    public static void main(String args[]){
        try{
            Class.forName("com.mysql.jdbc.Driver");

            Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/sonoo","root","root");
            //here sonoo is the database name, root is the username and root is the password
            Statement stmt=con.createStatement();

            ResultSet rs=stmt.executeQuery("select * from emp");

            while(rs.next())
                System.out.println(rs.getInt(1)+"  "+rs.getString(2)+"  "+rs.getString(3));

            con.close();

        }catch(Exception e){ System.out.println(e);}

    }
}

SQl Code:

create database sonoo;  
use sonoo;  
create table emp(id int(10),name varchar(40),age int(3));  

I also set the classpath to C:\Program Files\Java\jre7\lib\ext\mysql-5.0.7.jar then also its giving an same error.

I think the code is perfectly fine and there is wrong something with mysql.jar file. Also I downloaded this jar file from the following website: http://www.java2s.com/Code/Jar/m/Downloadmysql507jar.html and I'm using Mysql 5.6 server.

share|improve this question
    
How are you running this? What do you mean by by "set the classpath"? – ike_love Jan 10 '14 at 15:46
    
Why you don't use the official JDBC - driver for Mysql? dev.mysql.com/downloads/connector/j – Peter Jan 10 '14 at 16:04
    
And: Because this is a system library which you propabily be using a lot , why don't you just add it to your IDE configuration? You won't even have to care too much about classpaths, because your IDE will configure the build path automatically. – Peter Jan 10 '14 at 16:06
    
donwload the jar file from [link]mysql.com/downloads/connector/j – fady taher Jan 10 '14 at 16:12
    
Seriously: what article or book is telling people to put the jar in the lib/ext folder? And then tell people to also manually put that folder on the classpath?? This is far from the first time I see this happening. – Gimby Jan 10 '14 at 16:30

1 Answer 1

up vote 1 down vote accepted

1) Try surrounding this in parentheses, it might help.
"C:\Program Files\Java\jre7\lib\ext\mysql-5.0.7.jar"

2) Also, open the JAR (with some program able
to open ZIP files) and make sure the class is in
there (under the right package/directory structure).

The error definitely means the class is not on
the classpath at runtime even though you think it is.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.