runtime error: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
Asked Answered
H

6

5

I am new to mysql and jdbc and I am getting the error in this title. I have been searching all day and cannot find a solution that works for me.

What I have tried: uninstall/reinstall mysql, copy paste mysql-connector-java-5.1.25-bin.jar and ojdbc7.jar to same location as the .class file I am trying to run, rebuilt the program in a different directory, and probably a couple other things.

I am using notepad++ for coding and the windows command prompt to compile and run. it compiles fine but I try to run with

C:\Projects\bin>java -cp . ClientBase

The output is:

java.lang.ClassnNotFoundException: com.mysql.jdbc.Driver

at java.net.URLClassloader$1.run(URLClassLoader.java:336)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:432)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:188)
at ClientBase.main(ClientBase.java:21)
Goodbye.

// import packages
import java.sql.*;

// create class ClientBase
public class ClientBase{
            // JDBC driver name and database URL
            static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";                
            static final String DB_URL = "jdbc:mysql://localhost/CLIENTBASE";          



 // Database credentials
    static final String USER = "root";
    static final String PASS = "";

// Begin method main
public static void main(String[] args) {
    Connection conn = null;
    Statement stmt = null;

    try{
        // register JDBC driver
        Class.forName("com.mysql.jdbc.Driver");

        // Open connection
        System.out.println("Connecting to database...");
        conn = DriverManager.getConnection(DB_URL, USER, PASS);

        // Execute a query
        System.out.println("Creating statement...");
        stmt = conn.createStatement();
        String sql;
        sql = "SELECT id, name, address, address 2, city, phone, state, zip, fax FROM CLIENTBASE";
        ResultSet rs = stmt.executeQuery(sql);

        // Extract data from result set
        while(rs.next()){
            // Retrieve by column name
            int id = rs.getInt("id");
            String name = rs.getString("name");
            String address = rs.getString("address");
            String address2 = rs.getString("address2");
            String city = rs.getString("city");
            String phone = rs.getString("phone");
            String state = rs.getString("state");
            String zip = rs.getString("zip");
            String fax = rs.getString("fax");

            // Display values
            System.out.print("ID: " + id);
            System.out.print(" Name: " + name);
            System.out.println("Address:" + address);
            System.out.println(address2);
            System.out.print("City:" + city);
            System.out.print(" State: " + state);
            System.out.println(" Zip: " + zip);
            System.out.print("Phone: " + phone);
            System.out.println(" Fax: " + fax);
        } // end while

        // clean up
        rs.close();
        stmt.close();
        conn.close();
    }catch(SQLException se){
        // Handle errors for JDBC
        se.printStackTrace();
    }catch(Exception e){
        // Handle errors for Class.forName
        e.printStackTrace();
    }finally{
        // finally block used to close resources
        try{
            if(stmt!=null)
                stmt.close();
        }catch(SQLException se){
            se.printStackTrace();
        } // end finally
    } // end try
System.out.println("Goodbye.");
} // End method main
} // end class ClientBase

I should also say that I am going off an online tutorial for this code. It is not exactly how they have it as I decided to make something a little different than theirs, but it is generally the same. I don't think it is a code problem though from what the error is.

Any help would be appreciated! I'm going crazy!

Hookah answered 10/8, 2013 at 5:16 Comment(2)
Can you post folder hierarchy?Patrickpatrilateral
// finally block used to close resources - really ? so why you are closing them in try ? Also try to format your code !!!Boxhaul
G
12

You need to add a connector library to the Runtime classpath:

java -cp .;mysql-connector-java-5.1.25-bin.jar ClientBase

My example uses Windows classpath separator ";", on other systems it may be different (":" on Linux/Mac). It also assumes, that mysql-connector-java-5.1.25-bin.jar is located on the same folder. If it's not the case, then put a path to the library instead of the plain name.

ClientBase stands for Java class file name here

c:\>javac Test.java
c:\>java -cp .;F:\CK\JavaTest\JDBCTutorial\mysql-connector-java-5.1.18-bin Test
Gussy answered 10/8, 2013 at 5:21 Comment(4)
Thank you, this worked! I saw this same answer with other posts but I was was not understanding it from them and was typing it in wrong by adding a semi-colon after the jar and the full class path to ClientBase.Hookah
i also get same problem :- i get the file and the paste it to the same folder where my class file is saved then run the following command in my linux java -cp .:mysql-connector-java-5.1.18-bin.jar FirstExampleEtrem
I am running project through NetBeans, how do i configure it "default" so that i don't have to pass the command line argument everytime?Nachison
Tanks for the clarification about the difference between ";" and ":" it was causing me troubles.Squirm
E
3

I was also get same problem :-
How I solved for Linux system.

1.) Download file mysql-connector-java-5.1.18-bin.jar from given link or other.

2.) Then paste it to same folder or directory of your class or file (on where you want connection to present)

3.) Now use the following command by your linux command prompt.

java -cp .:mysql-connector-java-5.1.18-bin.jar YourFileName ..

Thats all you need to do... :)

Etrem answered 7/8, 2014 at 9:34 Comment(0)
H
2

if u use netbeans,do following 1.Open Netbeans IDE 2.Right-click your Project. 3.Select Properties. 4.On the left-hand side click Libraries. 5.Under "Compile" tab - click Add Jar/Folder button. 6.Select Downloaded "mysql-connector-java-5.1.25-bin.jar" file (Download Connector/J from dev.mysql.com) 7.Click OK Run Again... Its work.

Hallway answered 20/2, 2014 at 4:46 Comment(0)
M
1

What did you import ? From the documentation: http://dev.mysql.com/doc/connector-j/en/connector-j-usagenotes-connect-drivermanager.html

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
// Notice, do not import com.mysql.jdbc.*
// or you will have problems!

Comment:

Why are you using notepad++ ? install an IDE (Eclipse/Netbeans/IntelliJ) - it'll be much easier to locate such problems (un-included jars for example)

Menendez answered 10/8, 2013 at 5:21 Comment(0)
C
1

Add mysql.connector-java-x.x.x-bin.jar to your libraries folder. No need to import anything. Have a good one.

Crocodile answered 13/7, 2014 at 17:43 Comment(0)
U
0

If You Are Using Swing you have to add External Jar of my-sql-connect

else if You are Using jsp servlet You have to add External jar and Copy that Jar into WEB_INF/lib project folder

Undertaking answered 16/5, 2017 at 17:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.