<%@ page contentType="text/html;charset=gb2312" language="java" import="java.sql.*" errorPage=""%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312">
<title>通过JAVABEAN访问数据库</title>
</head>
<body>
<jsp:useBean id="t" class="com.prodb.Test1" scope="page"/>
<%
ResultSet rs=t.getRs("Test");
while(rs.next())
{out.print(rs.getInt(1)+"");
out.print(rs.getString(2)+"<br>");
}
t.close();
%>
</body>
</html>这是原代码
下面的是JAVABEAN的代码package com.prodb;
import java.sql.*;
import javax.naming.*;
import javax.sql.DataSource;
public class Test1{
private Statement stmt;
private ResultSet rs;
private static Connection con;
public static synchronized Connection getCon()throws Exception{
Context ctx;
DataSource ds;
try{
ctx=new InitialContext();
ds=(DataSource)ctx.lookup("java:comp/env/jdbc/Project");
return ds.getConnection();
}catch(SQLException e){
throw e;
}catch(NamingException e)
{
throw e;
}
} public Statement getStmt(){
try{
con=getCon();
stmt=con.createStatement();
}catch(Exception e){
System.out.println("getStmt");
System.out.println("e.getMessage()");
}
return stmt;
}
public Statement getStmtread(){
try{
con=getCon();
stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
}catch(Exception e){
System.out.println("getStmtread");
System.out.println("e.getMessage()");
}
return stmt;
}
public ResultSet getRs(String sql){
try{
stmt=this.getStmtread();
rs=stmt.executeQuery("select * from "+sql);
return rs;
}catch(Exception e){
System.out.println("getRS");
System.out.println("e.getMessage()");
} return null; }
public void close(){
try{ if(rs!=null)rs.close(); }catch(Exception e){} try{
if(stmt!=null)stmt.close(); }catch(Exception e){} try{
if(con!=null)con.close();
} catch(Exception e){}
{
System.out.println("close");
System.out.println("e.getMessage()");
}
} }运行时显示如下错误:type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception org.apache.jasper.JasperException: Exception in JSP: /Test3.jsp:118: <jsp:useBean id="t" class="com.prodb.Test1" scope="page"/>
9: <%
10: ResultSet rs=t.getRs("Test");
11: while(rs.next())
12: {out.print(rs.getInt(1)+"");
13: out.print(rs.getString(2)+"<br>");
14: }
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:504)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause java.lang.NullPointerException
org.apache.jsp.Test3_jsp._jspService(org.apache.jsp.Test3_jsp:62)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.15 logs.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312">
<title>通过JAVABEAN访问数据库</title>
</head>
<body>
<jsp:useBean id="t" class="com.prodb.Test1" scope="page"/>
<%
ResultSet rs=t.getRs("Test");
while(rs.next())
{out.print(rs.getInt(1)+"");
out.print(rs.getString(2)+"<br>");
}
t.close();
%>
</body>
</html>这是原代码
下面的是JAVABEAN的代码package com.prodb;
import java.sql.*;
import javax.naming.*;
import javax.sql.DataSource;
public class Test1{
private Statement stmt;
private ResultSet rs;
private static Connection con;
public static synchronized Connection getCon()throws Exception{
Context ctx;
DataSource ds;
try{
ctx=new InitialContext();
ds=(DataSource)ctx.lookup("java:comp/env/jdbc/Project");
return ds.getConnection();
}catch(SQLException e){
throw e;
}catch(NamingException e)
{
throw e;
}
} public Statement getStmt(){
try{
con=getCon();
stmt=con.createStatement();
}catch(Exception e){
System.out.println("getStmt");
System.out.println("e.getMessage()");
}
return stmt;
}
public Statement getStmtread(){
try{
con=getCon();
stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
}catch(Exception e){
System.out.println("getStmtread");
System.out.println("e.getMessage()");
}
return stmt;
}
public ResultSet getRs(String sql){
try{
stmt=this.getStmtread();
rs=stmt.executeQuery("select * from "+sql);
return rs;
}catch(Exception e){
System.out.println("getRS");
System.out.println("e.getMessage()");
} return null; }
public void close(){
try{ if(rs!=null)rs.close(); }catch(Exception e){} try{
if(stmt!=null)stmt.close(); }catch(Exception e){} try{
if(con!=null)con.close();
} catch(Exception e){}
{
System.out.println("close");
System.out.println("e.getMessage()");
}
} }运行时显示如下错误:type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception org.apache.jasper.JasperException: Exception in JSP: /Test3.jsp:118: <jsp:useBean id="t" class="com.prodb.Test1" scope="page"/>
9: <%
10: ResultSet rs=t.getRs("Test");
11: while(rs.next())
12: {out.print(rs.getInt(1)+"");
13: out.print(rs.getString(2)+"<br>");
14: }
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:504)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause java.lang.NullPointerException
org.apache.jsp.Test3_jsp._jspService(org.apache.jsp.Test3_jsp:62)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.15 logs.
try{
stmt=this.getStmtread();
rs=stmt.executeQuery("select * from "+sql);
return rs;
}catch(Exception e){
System.out.println("getRS");
System.out.println("e.getMessage()");
} return null; }
两个return…………
还有,貌似stmt,rs,con之类的,最好在各自的方法里声明,不要通用