0

I am creating a simple JDBC Java Application, and I added mysql jar file in Library and it is saving in Reference Libraries, but I am getting error as, java.lang.ClassNotFoundException

I am using mysql 8.0.12

java.lang.ClassNotFoundException: com.mysql.jdbc.driver
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at Jdbc.main(Jdbc.java:15)
java.lang.NullPointerException
    at Jdbc.main(Jdbc.java:32)

my code:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Savepoint;
import java.sql.Statement;

public class Jdbc {

    public static void main(String[] args) {
        Connection con=null;
        Statement st=null;

    try {
        Class.forName("com.mysql.jdbc.driver");
        con=DriverManager.getConnection("jdbc:mysql//localhost:3306/rakeshdb","root","raki");
        con.setAutoCommit(false);
        st=con.createStatement();


        st.executeUpdate("insert into emp values(111,'RAkE',100000,'hyd')");
        Savepoint sp=con.setSavepoint();
        st.executeUpdate("insert into emp values(222,'RAkESH',10000,'sec')");
        con.rollback();
        st.executeUpdate("insert into emp values(333,'RAkI',1000,'bvrm')");
        con.commit();
        System.out.println("Transcation SUCCESS");
    } catch (Exception e) {
        e.printStackTrace();
        try {
            con.rollback();
            System.out.println("Transcation FAILURE");
        } catch (Exception e2) {
        e2.printStackTrace();
        }

    }finally {

    }

    }
}
vicky
  • 1,021
  • 2
  • 7
  • 7

3 Answers3

2

It is expected :

java.lang.ClassNotFoundException: com.mysql.jdbc.driver

as com.mysql.jdbc.driver is not the JDBC driver class for MySQL.
But com.mysql.jdbc.Driver is.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
0

You need to install SQL driver for Java from their(SQL) site and input .jar file inside Libraries.

basha
  • 142
  • 2
  • 11
0

You are getting the error because you have provided wrong driver class details in Class.forName().

What I would recommend to you is to not use this at all as Class.forName() is not needed since JDBC 4.0. Any JDBC 4.0 drivers that are found in your classpath are automatically loaded.

So, try commenting that line and run your program. If you have the driver jar file in the class-path, it should get automatically loaded(Assuming you are using JDBC 4.0 driver)

Ashishkumar Singh
  • 3,580
  • 1
  • 23
  • 41