Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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

share|improve this question
    
Well how are you running this? How are you making the MySQL JDBC driver available to Java? – Jon Skeet Feb 16 '14 at 15:20
    
Check if required classes are in your classpath – gipinani Feb 16 '14 at 15:21
    
is mysql driver in your classpath? – Kent Feb 16 '14 at 15:21
    
@kent :see my edit :) – Akari Feb 16 '14 at 15:38
    
@mserioli see my edit :) – Akari Feb 16 '14 at 15:38
up vote 1 down vote accepted

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.

share|improve this answer
    
see my edit :) ..... – Akari Feb 16 '14 at 15:42
    
Are you sure you are adding it to the same project you are running. Right Click the project -- > build path -- > configure build path and In Libraries Tab press Add External Jar and Select your jar. – Aniket Thakur Feb 16 '14 at 15:53
1  
As the Exception clearly suggests class loading failed as class was not located I would suggest use java -cp pathToMySqlCJar yourMainClass to run and see. If this succeeds you are adding not adding jar to your project correctly. Try Rebuilding your project in Eclipse. – Aniket Thakur Feb 16 '14 at 15:58

@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.

share|improve this answer
    
see my edit :) .... – Akari Feb 16 '14 at 15:41

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.