Eclipse Juno JDBC编程中的ClassNotFound异常



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>

And My Error is:

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。

尝试使用jdk代替jre。使用jre6和tomcat6。JRE7和TOMCAT7在我的情况下不是很好。所以我照我建议的做了。甚至servlet也通过添加依赖项而得到更新。

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

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

嗨,朋友,首先你得安装jdk,然后把classes12.jar添加到你的库中

相关内容

  • 没有找到相关文章

最新更新