简体   繁体   中英

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);


    }
}

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.

Solution

  • 1) Firstly download ojdbc6.jar and ojdbc6_g.jar from google.

  • 2) If you are connecting to Oracle 11g from Java and running on version Java 6 then include ojdbc6.jar or ojdbc6_g.jar in your application's classpath.

  • 3) Once you complete the download, paste the files in C:\\Program Files\\Java\\jdk1.6.0_23\\jre\\lib\\ext folder .

This is just a reference solution for those who has Java 1.8 and oracle Version 12+. In my case i had Java 1.8 therefore downloading oracle8.jar and placing that in the classpath as mentioned above helped to solve my problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM