有一個在運行時使用此代碼是如何在運行時解決「java.lang.ClassNotFoundException:oracle.jdbc.driver.OracleDriver」?
拋出java.lang.ClassNotFoundException一個問題:甲骨文爲:jdbc:驅動程序:OracleDriver`。
但是同一個JDBC驅動程序的另一個程序運行正常,但是這個JDBC驅動程序在java applet中發現了一個異常。所以請幫助我解決這個問題。我是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);
}
}
唯一的例外來自當你試着去'的Class.forName(「......一個OracleDriver」)'因爲你沒有在你的類路徑中的Oracle JDBC類。有很多很多其他的問題都是這樣的,快速的谷歌搜索會給你一個答案。 –