求助各位 我编写的JSP在最后关闭数据库连接池部分出现了错误代码如下:
<%@ page contentType="text/html;charset=GBK"%>
<%@ page import="java.util.*"%>
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>
<%@ page session="false" %><%
                 String id=request.getParameter("id");
                 String value=request.getParameter("value");
            try{
                 InitialContext ctx=new InitialContext();
                 DataSource ds=(DataSource) ctx.lookup("java:comp/env/jdbc/prac");
                 Connection conn=ds.getConnection();
                 PreparedStatement stmt=conn.prepareStatement("select * from user where "+id+"=?");
                 stmt.setString(1,value);
                 ResultSet rs=stmt.executeQuery();
                 if (rs.next()){
                    out.println(rs.getString(1));
                    out.println(rs.getString(2));
                  }
               }catch(Exception e){
                 e.printStackTrace();
               }finally{
                         try{
                             if (rs != null){
                               rs.close();
                              }
                             if (stmt!=null){
                               stmt.close();
                              }
                             if (conn!=null){
                               conn.close();
                              }
                            }catch(Exception e){
                             out.println(e.toString());
                            }
                        }
%>
错误如下:type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception org.apache.jasper.JasperException: Unable to compile class for JSP: An error occurred at line: 26 in the jsp file: /find.jsp
rs cannot be resolved
23:                  e.printStackTrace();
24:                }finally{
25:                          try{
26:                              if (rs != null){
27:                                rs.close();
28:                               }
29:                              if (stmt!=null){
An error occurred at line: 27 in the jsp file: /find.jsp
rs cannot be resolved
24:                }finally{
25:                          try{
26:                              if (rs != null){
27:                                rs.close();
28:                               }
29:                              if (stmt!=null){
30:                                stmt.close();
An error occurred at line: 29 in the jsp file: /find.jsp
stmt cannot be resolved
26:                              if (rs != null){
27:                                rs.close();
28:                               }
29:                              if (stmt!=null){
30:                                stmt.close();
31:                               }
32:                              if (conn!=null){
An error occurred at line: 30 in the jsp file: /find.jsp
stmt cannot be resolved
27:                                rs.close();
28:                               }
29:                              if (stmt!=null){
30:                                stmt.close();
31:                               }
32:                              if (conn!=null){
33:                                conn.close();
An error occurred at line: 32 in the jsp file: /find.jsp
conn cannot be resolved
29:                              if (stmt!=null){
30:                                stmt.close();
31:                               }
32:                              if (conn!=null){
33:                                conn.close();
34:                               }
35:                             }catch(Exception e){
An error occurred at line: 33 in the jsp file: /find.jsp
conn cannot be resolved
30:                                stmt.close();
31:                               }
32:                              if (conn!=null){
33:                                conn.close();
34:                               }
35:                             }catch(Exception e){
36:                              out.println(e.toString());
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:92)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:439)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:349)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:327)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:314)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:589)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

解决方案 »

  1.   

    <%@ page contentType="text/html;charset=GBK"%>
    <%@ page import="java.util.*"%>
    <%@ page import="java.sql.*"%>
    <%@ page import="javax.sql.*"%>
    <%@ page import="javax.naming.*"%>
    <%@ page session="false" %><%
      String id=request.getParameter("id");
      String value=request.getParameter("value");
      InitialContext ctx = null;
      DataSource ds = null;
      Connection conn = null;
      PreparedStatement stmt= null;
      ResultSet rs = null;
      try{
      ctx=new InitialContext();
       ds=(DataSource) ctx.lookup("java:comp/env/jdbc/prac");
       conn=ds.getConnection();
      stmt=conn.prepareStatement("select * from user where "+id+"=?");
      stmt.setString(1,value);
      rs=stmt.executeQuery();
      if (rs.next()){
      out.println(rs.getString(1));
      out.println(rs.getString(2));
      }
      }catch(Exception e){
      e.printStackTrace();
      }finally{
      try{
      if (rs != null){
      rs.close();
      }
      if (stmt!=null){
      stmt.close();
      }
      if (conn!=null){
      conn.close();
      }
      }catch(Exception e){
      out.println(e.toString());
      }
      }
    %>
      

  2.   

    定义成全局变量就OK了,try语句块里的定义的变量都是局部变量,只能在try块里识别