23

我写了一个 Java Servlet 程序,但是当我运行它时,它显示了异常

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

我的代码是

package skypark;

import java.io.*;
import javax.servlet.*;
import java.text.*;
import javax.servlet.http.*;
import java.sql.*;
import java.sql.Date;

public class Registration extends HttpServlet {

    private static final long serialVersionUID = 1L;

    public static Connection prepareConnection() throws ClassNotFoundException, SQLException {
        String dcn = "oracle.jdbc.driver.OracleDriver";
        String url = "jdbc:oracle:thin:@JamesPJ-PC:1521:skypark";
        String usname = "system";
        String pass = "tiger";
        Class.forName(dcn);
        return DriverManager.getConnection(url, usname, pass);
    }

    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        PrintWriter out = resp.getWriter();

        try {
            String phone1, dofb, date1, month, year, uname, fname, lname, address, city, state, country;
            String pin, email, password, gender, lang, qual, relegion, privacy, hobbies, fav;

            uname = req.getParameter("uname");
            fname = req.getParameter("fname");
            lname = req.getParameter("lname");
            date1 = req.getParameter("date");
            month = req.getParameter("month");
            year = req.getParameter("year");
            address = req.getParameter("address");
            city = req.getParameter("city");
            state = req.getParameter("state");
            country = req.getParameter("country");
            pin = req.getParameter("pin");
            email = req.getParameter("email");
            password = req.getParameter("password");
            gender = req.getParameter("gender");
            phone1 = req.getParameter("phone");

            lang = "";
            qual = "";
            relegion = "";
            privacy = "";
            hobbies = "";
            fav = "";

            dofb = date1 + "-" + month + "-" + year;
            int phone = Integer.parseInt(phone1);
            DateFormat formatter;
            java.util.Date dob;
            formatter = new SimpleDateFormat("dd-MM-yy");
            dob = formatter.parse(dofb);

            Connection con = prepareConnection();
            String Query = "Insert into regdetails values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            PreparedStatement ps = con.prepareStatement(Query);

            ps.setString(1, uname);
            ps.setString(2, fname);
            ps.setString(3, lname);
            ps.setDate(4, (Date) dob);
            ps.setString(5, address);
            ps.setString(6, city);
            ps.setString(7, state);
            ps.setString(8, country);
            ps.setString(9, pin);
            ps.setString(10, lang);
            ps.setString(11, qual);
            ps.setString(12, relegion);
            ps.setString(13, privacy);
            ps.setString(14, hobbies);
            ps.setString(15, fav);
            ps.setString(16, gender);

            int c = ps.executeUpdate();

            String query = "insert into passmanager values(?,?,?,?)";
            PreparedStatement ps1 = con.prepareStatement(query);

            ps1.setString(1, uname);
            ps1.setString(2, password);
            ps1.setString(3, email);
            ps1.setInt(4, phone);

            int i = ps1.executeUpdate();

            if (c == 1 || c == Statement.SUCCESS_NO_INFO && i == 1 || i == Statement.SUCCESS_NO_INFO) {
                out.println("<html><head><title>Login</title></head><body>");
                out.println("<center><h2>Skypark.com</h2>");
                out.println("<table border=0><tr>");
                out.println("<td>UserName/E-Mail</td>");
                out.println("<form action=login method=post");
                out.println("<td><input type=text name=uname></td>");
                out.println("</tr><tr><td>Password</td>");
                out.println("<td><input type=password name=pass></td></tr></table>");
                out.println("<input type=submit value=Login>");
                out.println("</form></body></html>");
            } else {
                out.println("<html><head><title>Error!</title></head><body>");
                out.println("<center><b>Given details are incorrect</b>");
                out.println(" Please try again</center></body></html>");
                RequestDispatcher rd = req.getRequestDispatcher("registration.html");
                rd.include(req, resp);
                return;
            }
        } catch (ClassNotFoundException cnfe) {
            out.println("<html><head><title>Error!</title><body>");
            out.println("<b><i>Class not found " + cnfe + "</i></b>");
            out.println("</body></html>");
        } catch (SQLException sqle) {
            out.println("<html><head><title>Error!</title><body>");
            out.println("<b><i>Unable to process try after some time Sql error</i></b>");
            out.println("</body></html>");
        } catch (ParseException e) {
            out.println("<html><head><title>Error!</title><body>");
            out.println("<b><i>Unable to process Parseint exc " + e + "</i></b>");
            out.println("</body></html>");
        }

        out.flush();
        out.close();
    }
}

我的课程路径是:

C: \Windows\ system32 > echo % classpath %
  E: \app\ JamesPJ\ product\ 11.2.0\ dbhome_1\ oui\ jlib\ classes12.jar;
E: \app\ JamesPJ\ product 11.2.0\ dbhome_1\ jlib\ orai18n.jar;
E: \app\ JamesPJ\ product\ 11.2.0\ dbhome_1\ jdbc\ lib\ ojdc6_g.jar;
E: \app\ JamesPJ\ product\ 11.2.0\ dbhome_1\ BIN;
C: \Program Files\ Java\ jdk1.7.0_09\ bin;
C: \Users\ JamesPJ\ Documents;
E: \app\ JamesPJ\ product\ 11.2.0\ dbhome_1\ jdbc\ lib;
E: \app\ JamesPJ\ product\ 11.2.0\ dbhome_1\ jlib;
E: \app\ JamesPJ\ product\ 11.2.0\ dbhome_1\ jdbc\ lib\ ojdbc6.jar;
E: \app\ JamesPJ\ product\ 11.2.0\ dbhome_1\ oc4j\ jdbc\ lib\ orai18n.jar;
E: \app\ JamesPJ\ product\ 11.2.0\ dbhome_1\ oc4j\ jdbc\ lib\ ocrs12.jar;
E: \app\ JamesPJ\ product\ 11.2.0\ dbhome_1\ owb\ wf\ lib\ ojdbc14.jar;
C: \Program Files\ Apache Software Foundation\ Tomcat 7.0\ lib\ servlet - api.jar

当我给

 java oracle.jdbc.driver.OracleDriver 

命令提示符中的命令,它显示以下行

Error: Main method not found in class oracle.jdbc.driver.OracleDriver, please define the main method as:
       public static void main(String[] args)
4

7 回答 7

30

您是否在 Web 应用程序的 lib 文件夹中复制了 classes12.jar 并在 eclipse 中设置了类路径。

在包资源管理器构建路径中右键单击项目 -> 添加外部档案...

选择您的 ojdbc6.jar 存档

按确定

或者

浏览此链接并仔细阅读并进行操作。

现在应该在 Package explorer 下的“Referenced Librairies”中引用该库。现在尝试再次运行您的程序。

于 2012-12-16T18:14:43.563 回答
22

通过C:\apache-tomcat-7.0.47\lib 路径(此路径可能因您安装 Tomcat 服务器的位置而异),ojdbc14.jar如果不包含则过去。

然后在 Eclipse 中重新启动服务器,然后在服务器上运行您的应用程序

于 2013-11-20T07:58:05.250 回答
9

团队!要从您的 Servlet 执行 SQL 查询,您应该在文件夹中添加 JDBC jar 库

WEB-INF/lib

在此之后,您可以调用驱动程序,例如:

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

现在 Y 可以使用与 DB-server 的连接

==> 73!

于 2015-03-13T10:27:59.393 回答
4

尝试通过服务器库“C:\apache-tomcat-7.0.47\lib”添加ojdbc6.jar,

然后在eclipse中重启服务器。

于 2015-07-16T11:17:44.283 回答
0

我遇到了同样的错误,但是在将 ojdbc14.jar 复制到 lib 文件夹后,没有更多例外。(从某处复制 ojdbc14.jar 并将其粘贴到 WebContent 内的 lib 文件夹中。)

于 2014-05-28T10:18:40.517 回答
0

我有同样的问题,但能够通过执行以下操作来解决它:

右键单击项目 -> 属性,然后在部署程序集中添加 JAR(odjbc6 或 14)文件。

于 2014-07-14T17:53:28.247 回答
0

我遇到了同样的错误,但是在将 ojdbc14.jar 复制到 lib 文件夹后,没有更多例外。(从某处复制 ojdbc14.jar 并将其粘贴到 WebContent 内的 lib 文件夹中。)

于 2014-09-06T03:09:15.713 回答