使用Eclipse Juno进行JDBC编程中的ClassNotFound异常

时间:2013-03-26 06:38:34

标签: jsp java-ee servlets classnotfoundexception jdbc-odbc

StackOverFlow的所有活跃人群, 祝贺洒红(印度节日)

我在Eclipse JUNO中创建了一个简单的JDBC程序:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;


public class Connect {
static Connection conn;
public static void main(String[] args) {
    try {
        Class.forName("oracle.jdbc.driver.OracleDriver");
        conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","elt","elt");
        System.out.println("Connected");
    } catch (ClassNotFoundException cnf) {
        cnf.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}
}

当我在Eclipse Juno中执行此程序时,我得到了输出:

Connected

但是当我创建一个包含单个jsp页面和单个servlet的Java EE项目时,它会发出错误。我的JSP页面的代码是:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form method="post" action="LoginAction">
    User ID : <input type="text" name="uname"><br><br>
    Password : <input type="password" name="upass"><br><br>
    <input type="Submit" value="submit">
</form>
</body>
</html>

我的servlet的代码如下:

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginAction extends HttpServlet {

static String u_name,u_pass;
static Connection conn;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doPost(request, response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    out.println("Hello Foo !");
    u_name = request.getParameter("uname");
    u_pass = request.getParameter("upass");
    out.println(u_name);
    out.println(u_pass);

    try {
        Class.forName("oracle.jdbc.driver.OracleDriver");
        conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","elt","elt");
        out.println("Connected");
    } catch (ClassNotFoundException cnf) {
        cnf.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}
}

My web.xml的代码是:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
 <display-name>eLearning</display-name>
 <welcome-file-list>
 <welcome-file>login.jsp</welcome-file>
 </welcome-file-list>
 <servlet>
<servlet-name>LoginAction</servlet-name>
<servlet-class>LoginAction</servlet-class>
 </servlet>
 <servlet-mapping>
<servlet-name>LoginAction</servlet-name>
<url-pattern>/LoginAction</url-pattern>
 </servlet-mapping>
</web-app>

我的错误是:

java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
at     org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at LoginAction.doPost(LoginAction.java:32)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:931)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

在你开始建议错误的来源之前,我想告诉你我已经添加了classes12.jar,因为我的普通java程序正在运行。我还添加了ojdbc14.jar 。我正在使用oracle 10g。

3 个答案:

答案 0 :(得分:1)

尝试使用jdk代替jre。使用jre6和tomcat6。 JRE7和TOMCAT7在我的情况下表现不佳。所以我做了我建议你的事。甚至servlet也会通过添加依赖项来获取更新。

答案 1 :(得分:0)

classes12.jar用于JDK 1.2 / 1.3,ojdbc14.jar用于JDK 1.4 / 1.5。

如果您使用的是JDK 1.4或更高版本,则从类路径中删除classes12.jar并仅使用ojdbc14.jar

答案 2 :(得分:0)

嗨朋友首先你必须安装jdk,然后将classes12.jar添加到你的库