关于错误500的问题%@ page contentTypetext/html charsetUTF-8 pageEncodingUTF-8%%@ page imp
关于错误500的问题
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.util.Date"%>
<html>
<body>
<%!public static final String DBDRIVER = "com.mysql.jdbc.Driver";
public static final String DBURL = "jdbc:mysql://localhost:3306/test";
public static final String DBUSER = "root";
public static final String DBPASSWORD = "admin";
%>
<%
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
%>
<%
try {
Class.forName(DBDRIVER);
conn = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD);
String sql = "select * from emp";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
%>
<center>
<table border="1" width="80%">
<tr>
<td>雇员编号</td>
<td>雇员姓名</td>
<td>雇员工作</td>
<td>雇员工资</td>
<td>雇佣日期</td>
</tr>
<%
while (rs.next()) {
int empno = rs.getInt("empno");
String ename = rs.getString("ename");
String job = rs.getString("job");
Date d = new Date();
d = rs.getDate("hiredate");
float sal = rs.getFloat("sal");
%>
<tr>
<td><%=empno%></td>
<td><%=ename%></td>
<td><%=job%></td>
<td><%=sal%></td>
<td><%=d%></td>
</tr>
<%
}
%>
</table>
</center>
<%
} catch (Exception e) {
System.out.println(e);
} finally {
rs.close();
pstmt.close();
conn.close();
}
%>
</body>
</html>
写出来以后没有报错啊,但是为什么运行的时候 说是500错误
HTTP Status 500 - An exception occurred processing JSP page /list_emp.jsp at line 58
type Exception report
message An exception occurred processing JSP page /list_emp.jsp at line 58
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /list_emp.jsp at line 58
55: } catch (Exception e) {
56: System.out.println(e);
57: } finally {
58: rs.close();
59: pstmt.close();
60: conn.close(); // 如果直接关闭连接也可以
61: }
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.lang.NullPointerException
org.apache.jsp.list_005femp_jsp._jspService(list_005femp_jsp.java:142)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
[解决办法]rs 等等关闭之前先判断一下是不是 null