user3182420
user3182420

Reputation: 21

Java.lang.ClassnotfoundException com.mysql.jdbc.Driver

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.

Upvotes: 0

Views: 899

Answers (1)

peter.petrov
peter.petrov

Reputation: 39437

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.

Upvotes: 1

Related Questions