Akari
Akari

Reputation: 856

How to solve java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

I've written this java application to respond the data from the command line and store it into a database :

 import java.util.Scanner;
import java.sql.*;


public class Program {


    public static void main(String[] args)throws ClassNotFoundException 


    {


        Connection conn=null;

        try {

            Class.forName("com.mysql.jdbc.Driver");

             conn = DriverManager.getConnection("jdbc:mysql://localhost/DevOps_DB","root","root");


             PreparedStatement st = conn.prepareStatement("INSERT INTO pers " + "VALUES ('"+args[0]+"'); ");
             st.executeUpdate();


        } 



        catch (SQLException ex) {

            System.out.println("SQL Exception : "+ ex.getMessage());

            System.out.println("Vendor Error : "+ ex.getErrorCode());

        }


        catch(ClassNotFoundException ex) {

            ex.printStackTrace();


        }



//      
//      for(String arg : args)            
//      {   
//           System.out.println(arg);            
//      }




    }





}

But I've got the following exception :

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:190)
    at Program.main(Program.java:18)

why ? ... any help to fix the problem ?

EDIT :

I added the .jar file , see the following image :

enter image description here

Upvotes: 6

Views: 55410

Answers (7)

Hoang Duong Nguyen
Hoang Duong Nguyen

Reputation: 1

I had a proplem same. Then i moved file .jar from (D:) to (C:) and it ư

Upvotes: -1

Deepika Vashisth
Deepika Vashisth

Reputation: 21

Use com.mysql.cj.jdbc.Driver instead of com.mysql.jdbc.Driver.

Upvotes: 0

Anshuman
Anshuman

Reputation: 11

Extract the mysql driver which you can download from a link! and copy the .jar file of the mysql zip and paste it here in "lib"Screenshot of the location u have to paste in into (in the interface)

Upvotes: 0

Kirtikumar Thakur
Kirtikumar Thakur

Reputation: 41

ok you added the jar.do one thing open server, form window->show view->servers then double click on server

then it will open a new window then goto open Launch configuration then

go to class path and agin add mysql-connector.jar click on ok.

Upvotes: 4

kkashyap1707
kkashyap1707

Reputation: 531

You have to download mysql connector jar file(driver implementation) and add it to your class path.

Upvotes: 3

Aniket Thakur
Aniket Thakur

Reputation: 68905

You need to download mysql connector jar file(driver implementation) and add it to your class path.

On the side note if your are using JDBC 4.0 (Java 7 and even 6 I think) then you need not use Class.forName("com.mysql.jdbc.Driver");. Simply add the jar to the class path. Driver implementation will automatically be searched and loaded from class path.

Upvotes: 5

Suneet Bansal
Suneet Bansal

Reputation: 2702

@Akari : this is related to the .jar file for MySql. Try to check whether the .jar for MySql is set to the classpath or not.

Upvotes: 2

Related Questions