无法在servlet

时间:2016-12-13 16:52:11

标签: java servlets jdbc oracle10g

我正在尝试创建一个html页面,它接受一些输入并使用jdbc和servlet将该数据保存在oracle数据库中。 这是HTML文件

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="HelloServlet" method="post">
	<input type="text" placeholder="ID" name="id">
	<input type="text" placeholder="NAME" name="name">
	<input type="text" placeholder="SALARY" name="salary">
	<input type="submit" value="submit">
</form>	
</body>
</html>

这是javafile包含servlet

package com.gaurav;
import java.sql.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

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

import org.apache.jasper.tagplugins.jstl.core.Out;
@WebServlet("/HelloServlet")
public class HelloServlet extends HttpServlet {

private static final long serialVersionUID = 1L;


@Override
protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {

    // Set response content type


    // Actual logic goes here.

    //String name=request.getParameter("name");
    //out.println(name);

}

@Override
protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    String url="jdbc:oracle:thin:@localhost:1521:xe";
    String user="system";
    String pass="inception";


    try{
        int id=Integer.parseInt(request.getParameter("id"));
        String name=request.getParameter("name");
        int salary=Integer.parseInt(request.getParameter("salary"));
        Class.forName("oracle.jdbc.driver.OracleDriver");  
        Connection con=DriverManager.getConnection(url,user,pass);
        String query="INSERT INTO employee VALUES(?,?,?)";
        PreparedStatement stm=con.prepareStatement(query);
        stm.setInt(1, id);
        stm.setString(2, name);
        stm.setInt(3, salary);
        stm.executeUpdate();
        con.close();
    }

    catch(ClassNotFoundException e){
        out.println("class not found ");
    }
    catch(SQLException e){
        out.println("SQL Exception");
    }



}

@Override
public void destroy() {
    // resource release
    super.destroy();
}
}

file hierarchy

每当我试图运行上述程序时,它都没有显示任何错误,但是当我写一些数据并按下提交按钮时,它会抛出一个ClassNotFound异常。我不知道发生了什么事情。所以有人帮助我。

堆栈追踪:

java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1285)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1119)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at com.gaurav.HelloServlet.doPost(HelloServlet.java:50)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:108)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:783)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:789)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)

2 个答案:

答案 0 :(得分:0)

看起来您正在使用Oracle 10g。从Oracle 9i开始,不推荐支持oracle.jdbc.driver.OracleDriver(文档here)。您可以尝试使用oracle.jdbc.OracleDriver吗?

答案 1 :(得分:0)

检查您是否在项目中加载了正确的oracle jar驱动程序。