Search code examples
javaoracle-databasejdbc

How to solve "java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver" at runtime?


There is a problem at runtime with this code which is :

java.lang.classNotFoundException: oracle:jdbc:driver:OracleDriver

but another program of same JDBC driver are run properly but this JDBC driver is found a exception in java applet. So please help me for this problem.

I'm new in Java.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.sql.*;
import java.io.*;

/*<applet code="EmpDetails" width=300 height=500></applet>*/

public class EmpDetails extends Applet implements ActionListener{
TextField firstName, lastName, userId, pass, email, phone;
Button submit,cancel;
String msg = "";

public void init(){

    setLayout(new GridLayout(10,2,0,30));


    Label fname = new Label("First Name : ");
    Label lname = new Label("\nLast Name : ");
    Label uid = new Label("User Id : ");
    Label pas = new Label("Password : ");
    Label emailid = new Label("Email Id : ");
    Label ph = new Label("Phone : ");

    firstName = new TextField(10);
    lastName = new TextField(10);
    userId = new TextField(16);
    pass = new TextField(16);
    email = new TextField(30);
    phone = new TextField(12);
    pass.setEchoChar('*');

    submit = new Button("Submit");
    cancel = new Button("Cancel");

    add(fname);
    add(firstName);

    add(lname);
    add(lastName);
    add(uid);
    add(userId);
    add(pas);
    add(pass);
    add(emailid);
    add(email);
    add(ph);
    add(phone);
    add(submit);
    add(cancel);

    firstName.addActionListener(this);
    lastName.addActionListener(this);
    userId.addActionListener(this);
    pass.addActionListener(this);
    email.addActionListener(this);
    phone.addActionListener(this);
    submit.addActionListener(this);
    cancel.addActionListener(this);
    }
    public void actionPerformed(ActionEvent ae)
    {
        String str = ae.getActionCommand();
        if(str.equals("Submit"))
        {
                try{
                    Class.forName("oracle.jdbc.driver.OracleDriver");
                    String url = "jdbc:oracle:thin:@localhost:1521:XE";
                    String id = "system";
                    String passw = "root";

                    Connection con = DriverManager.getConnection(url , id , passw);

                    Statement st = con.createStatement();

                    String u,fn,ln,ps,em,pn;
                    u = userId.getText();
                    fn = firstName.getText();
                    ln = lastName.getText();
                    ps = pass.getText();
                    em = email.getText();
                    pn = phone.getText();
                    String urld = "INSERT INTO EMPDETAILS(id,firstname,lastname,email,password,phone)" + "values" + "('" + u + "','" + fn + "','" + ln + "','" + em + "','" + ps + "','" + pn + "')";
                    st.executeUpdate(urld);
                    con.close();
                    st.close();
                    msg = "Recode added successfull ";
                }

                catch(Exception e){ msg = e.toString();}
        }

        else{
            msg = "No any data added";

        }
        repaint();
    }
    public void paint(Graphics g){
        g.drawString(msg,6,300);


    }
}

Solution

  • The reason why you encounter this Exception is, that you use the wrong package to refer to the OracleDriver class

    Therefore, you should change the incorrect class load call

    Class.forName("oracle.jdbc.driver.OracleDriver");
    

    into

    Class.forName("oracle.jdbc.OracleDriver");
    

    as this class file implements the java.sql.Driver interface which is actually checked for at runtime.

    For reference, see also the description in the official JavaDoc provided by Oracle:

    The Oracle JDBC driver class that implements the java.sql.Driver interface.