在用JSP+JAVABEAN开发WEB系统的时候,有一个这样的问题,利用JAVABEAN写了如下一个方法:
public ResultSet selectTable()
{
   ResultSet rs= null;
   try
   {
        //连接数据库
    String str_sql="select * from mytable";
        rs=statm.executeQuary(str_sql);
    }
    catch(Exception e)
    {
    }
    return rs;
}下面是我在JSP页面中调用此方法
myclassFile myF = new myclassFile();
ResultSet rs = myF.selectTable();
while(rs.next())
{
  .......
}
rs.close();请问当关闭记录集时,在JAVABEAN中会不会把数据连接也关闭了,如果不行,怎么样在rs.close()之后关闭数据库连接。谢谢

解决方案 »

  1.   

    public void closeDB()
        {
            try
            {
                if(stmt != null)
                    stmt.close();            if(conn != null)
                    conn.close();
            }
            catch(Exception exception)
            {
                System.out.println(exception.toString());
            }
        }
      

  2.   

    twtetgso(*七匹狼*) 这个方法是不是在执行rs.close();之后调用????
      

  3.   

    你如果在javaBean里关掉con的话,那么你的页面就绝对接收不到rs了,如果只想解决这个问题,那么在你的javaBean里加一个方法,
    public void close(){
     if (rs!=null) rs.close();
      if (stmt!=null) stmt.close();
     if (con!=null) con.close();
    }然后在页面调用。如果想最好的解决这个问题,那么请不要用ResultSet作为返回对象:)
      

  4.   

    twtetgso(*七匹狼*)  假设我一个页面中取得了多个javabean中的记录集,又如何关闭,是不是只要在JSP中的最后只执行一次即可,还是在每个之后都执行??马上就结帖
      

  5.   

    返回结果集是因为老板不让我在页面中写SQL语句,
      

  6.   

    这样行不行
    //javabean代码
    package SystemManage;
    import java.io.Serializable;
    import java.sql.*;
    import DataBaseConnect.*;public class SMGDepartment implements Serializable
    {
    public SMGDepartment()
    {
    }

    public ResultSet listTable()
    {
        ResultSet  rs=null;
    try
        {
     
               Connection con = SqlConnection.getConnection();
               Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
    String strSQL = "select * from myTable";
            rs = stmt.executeQuery(strSQL);
        }
    catch(Exception exception)
        {
               System.out.println("List Table Exception: " + exception.getMessage());
        }  
    return rs;  
    }
    public void closeDB()
        {
            try
            {
               Connection con = SqlConnection.getConnection();
               Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);

    if(stmt != null)
                    stmt.close();
                if(con != null)
                    con.close();
            }
            catch(Exception exception)
            {
                System.out.println(exception.toString());
            }
        }
    }///////////////////////////////////////////////////////////////////
    //jsp页面代码
    <%@ page errorPage="ErrPage.jsp"
             import="SystemManage.SMGDepartment,java.sql.*"
             contentType="text/html;charset=gb2312"%><HTML>
    <HEAD>
    <TITLE>
    系统管理主页
    </TITLE>
    </HEAD>
    <BODY BGCOLOR=#cccc66 leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" class="leftbg" LANGUAGE=javascript onload="return window_onload()">
    <%
    try{ %>     <table border="1" width=580 align=center bgcolor='#ffffff' cellpadding="2" cellspacing="2" bordercolor="#111111">

       <tr>
         <td align=center width=150>部门序号</td>
     <td align=center width=150>部门名称</td>
     <td align=center width=100>部门类型</td>
       </tr>
    <%
        SMGDepartment smgd = new SMGDepartment();
        ResultSet rs = smgd.listDepartment();
    while(rs.next()){
      String sp_department_id= rs.getString("sp_department_id");
      String sp_department_name = rs.getString("sp_department_name");
      String sp_department_typeid = rs.getString("sp_department_typeid"); 
      String sp_department_typename = rs.getString("sp_department_typename"); 
     
    out.println("<tr>");
    out.println("<td align=center width=150>"+sp_department_id+"</td>");
    out.println("<td align=center  width=150>"+sp_department_name+"</td>");
    out.println("<td align=center  width=100>"+sp_department_typename+"</td>");

    out.println("</tr>");
     }
     rs.close();
     smgd.closeDB();
    %>
    </table><%}
    catch(Exception e)
    {
    System.out.println("Exception: " + e.getMessage());
    }  
    %>
    </BODY>
    </HTML>
    请问是否正确????
      

  7.   

    返回结果集是因为老板不让我在页面中写SQL语句,
      

  8.   

    不对吧,你在public ResultSet listTable()和public void closeDB()
    中所指向的conn stmt都不是同一个对象。
      

  9.   

    一個最簡單的辦法:这样行不行
    //javabean代码
    package SystemManage;
    import java.io.Serializable;
    import java.sql.*;
    import DataBaseConnect.*;public class SMGDepartment implements Serializable
    {
    //加上這裡
    private Connection con = null;
    private Statement stmt = null;

    public SMGDepartment()
    {
    }

    public ResultSet listTable()
    {
        ResultSet  rs=null;
    try
        {
    //修改這裡 
                con = SqlConnection.getConnection();
                stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
    String strSQL = "select * from myTable";
            rs = stmt.executeQuery(strSQL);
        }
    catch(Exception exception)
        {
               System.out.println("List Table Exception: " + exception.getMessage());
        }  
    return rs;  
    }
    public void closeDB()
        {
            try
            {
               //刪除這裡

    //增加這裡
    if(rs != null)
                    rs.close();
                    
    if(stmt != null)
                    stmt.close();
                if(con != null)
                    con.close();
            }
            catch(Exception exception)
            {
                System.out.println(exception.toString());
            }
        }
    }///////////////////////////////////////////////////////////////////
    //jsp页面代码
    <%@ page errorPage="ErrPage.jsp"
             import="SystemManage.SMGDepartment,java.sql.*"
             contentType="text/html;charset=gb2312"%><HTML>
    <HEAD>
    <TITLE>
    系统管理主页
    </TITLE>
    </HEAD>
    <BODY BGCOLOR=#cccc66 leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" class="leftbg" LANGUAGE=javascript onload="return window_onload()">
    <%
    try{ %>     <table border="1" width=580 align=center bgcolor='#ffffff' cellpadding="2" cellspacing="2" bordercolor="#111111">

       <tr>
         <td align=center width=150>部门序号</td>
     <td align=center width=150>部门名称</td>
     <td align=center width=100>部门类型</td>
       </tr>
    <%
        SMGDepartment smgd = new SMGDepartment();
        ResultSet rs = smgd.listDepartment();
    while(rs.next()){
      String sp_department_id= rs.getString("sp_department_id");
      String sp_department_name = rs.getString("sp_department_name");
      String sp_department_typeid = rs.getString("sp_department_typeid"); 
      String sp_department_typename = rs.getString("sp_department_typename"); 
     
    out.println("<tr>");
    out.println("<td align=center width=150>"+sp_department_id+"</td>");
    out.println("<td align=center  width=150>"+sp_department_name+"</td>");
    out.println("<td align=center  width=100>"+sp_department_typename+"</td>");

    out.println("</tr>");
     }
     //刪除這裡
     //rs.close();
     smgd.closeDB();
    %>
    </table><%}
    catch(Exception e)
    {
    System.out.println("Exception: " + e.getMessage());
    }  
    %>
    </BODY>
    </HTML>
      

  10.   

    肚子好饿啊,老板没人性,谢谢 Leemaasn
    我去试一下,如果能行的话,只要你能来我们长沙,一定请你喝酒,