<%@ page contentType = "text/html" pageEncoding = "utf-8"%>
<%@ page import = "java.sql.*"%>
<html>
<head><title>www.lk.com</title></head>
<body>
<%!
public static final String DBDRIVER = "oracle.jdbc.Driver.OracleDriver";
public static final String DBURL = "jdbc:oracle:thin:@localhost:1502:hte";
public static final String DBUSER = "system";
public static final String DBPASS = "lukai138628";
%>
<%
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
%>
<%
try{
Class.forName(DBDRIVER);
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS);
String sql = "select empon, ename, job, hiredate, sal from emp";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
%>
<center>
<table border = "1" width = "80%">
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<%
while(rs.next())
{
int empon = rs.getInt(1);
String ename = rs.getString(2);
String job = rs.getString(3);
String date = rs.getString(4);
Float sal = rs.getFloat(5);
%>
<tr>
<td><%=empon%></td>
<td><%=ename%></td>
<td><%=job%></td>
<td><%=sal%></td>
<td><%=date%><td>
</tr>
<%
}
%>
</table>
</center>
<%
}catch(Exception e){
System.out.println(e);
}finally{
rs.close();
pstmt.close();
conn.close();
}
%>
</body>
</html>
type Exception report
message
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.jsp at line 64 61: }catch(Exception e){ 62: System.out.println(e); 63: }finally{ 64: rs.close(); 65: pstmt.close(); 66: conn.close(); 67: } Stacktrace: org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:505) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:416) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267) javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause
java.lang.NullPointerException org.apache.jsp.list_jsp._jspService(list_jsp.java:134) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267) javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.18 logs.
public static final String DBDRIVER = "oracle.jdbc.Driver.OracleDriver";
public static final String DBURL = "jdbc:oracle:thin:@localhost:1502:hte";
Driver 改成 driver
端口号改成1521
把oracle的驱动放在\Tomcat 6.0\lib就可以了。
你这个代码写得,后台代码肿么全整到jsp页面?不利于复用。
rs.close(); pstmt.close(); conn.close();
关闭的时候判断一下是否为null
不行网上搜索java jdbc
同上。有个异常是空指针异常,说明有个值为null,无法close。
看你sql语句那hiredate应该是个Date类型的字段,可以用rs.getString来接收?
我设定的是String类型的。