如何在运行时解决“java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38682483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to solve "java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver" at runtime?
提问by Mohit Prakash
There is a problem at runtime with this code which is :
此代码在运行时存在一个问题,即:
java.lang.classNotFoundException: oracle:jdbc:driver:OracleDriver
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.
但是同一JDBC驱动程序的另一个程序运行正常,但在java小程序中发现该JDBC驱动程序异常。所以请帮我解决这个问题。
I'm new in Java.
我是 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);
}
}
回答by MWiesner
The reason why you encounter this Exception is, that you use the wrong package to refer to the OracleDriver
class
你遇到这个异常的原因是,你使用了错误的包来引用这个OracleDriver
类
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.
因为这个类文件实现了java.sql.Driver
在运行时实际检查的接口。
For reference, see also the description in the official JavaDocprovided by Oracle:
作为参考,另见Oracle提供的官方JavaDoc中的描述:
The Oracle JDBC driver class that implements the java.sql.Driver interface.
实现 java.sql.Driver 接口的 Oracle JDBC 驱动程序类。
回答by Gopinath Padala
Solution
解决方案
1) Firstly download
ojdbc6.jar
andojdbc6_g.jar
from google.2) If you are connecting to Oracle 11g from Java and running on version Java 6 then include
ojdbc6.jar
orojdbc6_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
.
1)首先从谷歌下载
ojdbc6.jar
和ojdbc6_g.jar
。2) 如果您从 Java 连接到 Oracle 11g 并在 Java 6 版上运行,则在应用程序的类路径中包含
ojdbc6.jar
或ojdbc6_g.jar
。3) 下载完成后,将文件粘贴到
C:\Program Files\Java\jdk1.6.0_23\jre\lib\ext folder
.
回答by Touseef Zaki
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.
这只是为那些拥有 Java 1.8 和 oracle Version 12+ 的人提供的参考解决方案。就我而言,我有 Java 1.8,因此下载 oracle8.jar 并将其放在类路径中,如上所述有助于解决我的问题。