2 cyq6567916 cyq6567916 于 2016.09.11 00:17 提问

Servlet访问数据库报错500

这是网页提示的错误
HTTP Status 500 - 查询失败


type Exception report

message 查询失败

description The server encountered an internal error that prevented it from fulfilling this request.

exception

java.lang.RuntimeException: 查询失败
Dao.EmpDao.finAll(EmpDao.java:32)
web.SqlServlet.service(SqlServlet.java:23)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

root cause

org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot load JDBC driver class 'oracle.jdbc.driver.OracleDriver'
org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1429)
org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
util.DBUtil.getConnection(DBUtil.java:36)
Dao.EmpDao.finAll(EmpDao.java:16)
web.SqlServlet.service(SqlServlet.java:23)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

root cause

java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1856)
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1705)
org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1420)
org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
util.DBUtil.getConnection(DBUtil.java:36)
Dao.EmpDao.finAll(EmpDao.java:16)
web.SqlServlet.service(SqlServlet.java:23)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

note The full stack trace of the root cause is available in the Apache Tomcat/7.0.67 logs.

这是我的代码,我用测试代码访问数据库的时候,都没有问题,一旦用Servlet访问就出现500问题
package web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

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

import Dao.EmpDao;
import entity.Emp;

public class SqlServlet extends HttpServlet {

@Override
protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    req.setCharacterEncoding("utf-8");
    res.setContentType("text/html;charset=utf-8");
    EmpDao dao = new EmpDao();
    List<Emp> list = dao.finAll();
    PrintWriter out = res.getWriter();
    out.println("<table border='1px' cellspacing='0' width='40%'>");
    out.println("  <tr>");
    out.println("   <th>编号</th>");
    out.println("   <th>姓名</th>");
    out.println("   <th>职位</th>");
    out.println("   <th>月薪</th>");
    out.println("  </tr>");
    for(Emp emp:list){
        out.println("<tr>");
        out.println("   <td>"+emp.getEmpno()+"</td>");
        out.println("   <td>"+emp.getEname()+"</td>");
        out.println("   <td>"+emp.getJob()+"</td>");
        out.println("   <td>"+emp.getSal()+"</td>");
        out.println("</tr>");
    }
    out.println("</table>");
    out.close();
}

}

下面是dao包的代码package Dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import entity.Emp;
import util.DBUtil;

public class EmpDao {
public List finAll(){
Connection con = null;
try {
con=DBUtil.getConnection();
String sql = "select * from student";
PreparedStatement ps = con.prepareStatement(sql);
List list = new ArrayList();
ResultSet rs = ps.executeQuery();
while(rs.next()){
Emp emp = new Emp();
emp.setEmpno(rs.getInt("empno"));
emp.setEname(rs.getString("ename"));
emp.setJob(rs.getString("job"));
emp.setSal(rs.getDouble("sal"));
list.add(emp);
}
return list;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("查询失败",e);
}finally{
DBUtil.close(con);

    }
}

}

请各位大神帮帮忙 谢谢

7个回答

YouBeHui
YouBeHui   2016.09.11 16:39
已采纳

500问题是响应问题,说明响应出错,org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot load JDBC driver class 'oracle.jdbc.driver.OracleDriver' 和 java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver 很明显的说明了你服务器中没有添加oracle6.jar类似的驱动包,添加进去就可以了

cyq6567916
cyq6567916 多谢,我把包放错地方了,我放在了main包里,应该放在WEB-INF包里。
大约一年之前 回复
Evankaka
Evankaka   Ds   Rxr 2016.09.11 12:04

org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot load JDBC driver class 'oracle.jdbc.driver.OracleDriver'
这里说得很明白了,你没有添加Oracle用java连接的驱动包。

cyq6567916
cyq6567916 谢谢
大约一年之前 回复
qq_29594393
qq_29594393   Ds   Rxr 2016.09.11 00:40

产生错误的原因有太多可能,一般都是一些细节的问题,只能靠自己检查解决

cyq6567916
cyq6567916 多谢
大约一年之前 回复
Ro_bot
Ro_bot   2016.09.11 01:20

应该是你部署web时没有把Oracle数据库的jar包部署到,检查下

cyq6567916
cyq6567916 谢谢
大约一年之前 回复
cyq6567916
cyq6567916   2016.09.11 08:36

我检查两天了 而且直接把老师的代码复制过来也不行
jar包? 我测试是可以链接数据库的 但是用Sevlet就不行 我就是在Servlet工程包下测试的 jar包应该没有问题

cyq6567916
cyq6567916   2016.09.11 08:37

细节我一步一步检查两天了 大神 你一句细节就给我打发走了。。。。
而且最后我直接复制老师的代码都没有用

qq_35903284
qq_35903284   2017.03.07 23:49

应该是服务器端的问题

Csdn user default icon
上传中...
上传图片
插入图片