我的每个servlet里一般都有好几个查询语句的,
要完成一次rs.next()就关闭一次吗?
在整个servlet里我就关闭一次rs的啊

解决方案 »

  1.   

    不是每次next的时候,而是rs用完以后!
      

  2.   

    rs.next到最后,指针已经在末尾了,你再把它移回开始位置
      

  3.   

    rs.beforeFirst();rs.next()重新开始
      

  4.   

    rs.beforeFirst()
        Moves the cursor to the front of this ResultSet object, just before the first row. This method has no effect if the result set contains no rows.
      

  5.   

    谢谢各位了。
    用了,不过出现这个问题了:
    “对只转发结果集的无效操作”
    这又是什么错误?
    或者这个rs.beforeFirst();用在那个位置的啊?
      

  6.   

    在指针指在最后位置时可以用rs.beforeFirst();移到最开始位置。也可以用rs.previous()表示从最后一条倒序到第一条。
      

  7.   

    在你的rs.next()循环完成以后,下次开始之前。你不妨把代码贴出来看看。
      

  8.   

    try
    {
       stmt = con.createStatement();
       int all_id[]=new int[10000];
       String all_name[]=new String[10000];
       String all_title[]=new String[10000];
       String all_time[]=new String[10000];
       int all_hitnum[]=new int[100];
       int i=0;
       if(true)
       {
    String sel_all ="select id ,name,title,lyb_time,hit_num "+
                  "from p_bbs_main where lyb_top='n' order by order_id";
    rs=stmt.executeQuery(sel_all);
    while(rs.next())
    {
       all_id[i]=Integer.parseInt(rs.getString(1));
       all_name[i]=rs.getString(2);
       all_title[i]=rs.getString(3);
       all_time[i]=rs.getString(4);
       all_hitnum[i]=Integer.parseInt(rs.getString(5));
       i++;
    }
        }
       if(true)
       {
    String sel_id ="select id from p_bbs_reply";
    rs=stmt.executeQuery(sel_id);
             int j=0;
             int sel_id[]=new int[100];
    while(rs.next())
    {
       sel_id[j]=Integer.parseInt(rs.getString(1));
                j++;
    }
        }
        stmt.close();
        con.close();
        rs.close();
    }有时执行到第一个rs.next()后面的all_id[i]=Integer.parseInt(rs.getString(1));时就异常了,说是 用尽的resultSet .在运行了几次某个servlet后,关闭tomcat,然后再开tomcat,再运行servlet,用不用这个rs.beforeFirst();的啊??最主要是,这个rs.beforeFirst();放在那里才合适?一个servlet里可以放多少个??一个servlet里最多可以有多少个rs.next()的啊??好多问题,谢谢解答啦!!
      

  9.   

    原代码的一部分:import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.util.*;
    import java.sql.*;public class LybContent extends HttpServlet
    {

    private Statement statement = null;
    private Connection connection = null;
    private ResultSet rs = null;
    //private String url = "jdbc:odbc:myfirst";
    private String url = "jdbc:oracle:thin:@192.168.91.160:1521:beyes"; public void init(ServletConfig config)
    throws ServletException
    {
    super.init(config);
    try
    {
    //Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Class.forName("oracle.jdbc.driver.OracleDriver");
    connection = DriverManager.getConnection(url,"brer","brer");

    }
    catch (Exception e)
    {
    System.out.println("Connect error");
    e.printStackTrace();

    }
    }
    public void doGet (HttpServletRequest req,HttpServletResponse res)
    throws ServletException,IOException 
    {
    int lyb_id=Integer.parseInt(req.getParameter("lyb_content_id"));
    System.out.println("LybContent--lyb_id is : "+lyb_id);

    HttpSession session=req.getSession();
    String log_flag=(String)session.getAttribute("username");

    String v_systime[]=new String[5];
    v_systime=PubParameter.systime();

    PrintWriter output = res.getWriter();
    res.setContentType("text/html");

    int privi_num=0;
    try
    {
    statement =connection.createStatement();
    if(true)
    {
    String privi_sql="select privi_num from p_privilege where name='"+log_flag+"'";
    rs=statement.executeQuery(privi_sql);
    rs.next();
    System.out.println("LybContent--4");
    privi_num=Integer.parseInt(rs.getString(1));
    System.out.println("LybContent--5");
    } String main_info_sql="select a.name,a.infofrom,a.underwrite,a.chenghao,a.logtime,a.portrait,"+
    "b.commintegral,b.commlevel,b.newmess,b.replymess,b.degree,"+
    "c.title,c.content,c.lyb_time "+
    "from p_info a,p_info_comm b,p_bbs_main c "+
    "where a.id=b.id and c.name=a.name and c.id="+lyb_id;
    System.out.println("LybContent-- --main info sql is : "+main_info_sql);
    String info_name="";
    String info_from="";
    String info_underwrite="";
    String info_chenghao="";
    String info_logtime="";
    String info_portrait="";

    String info_level="";
    int info_integral=0,info_newmess=0,info_replymess=0;
    String info_degree="";

    String lyb_title="";
    String lyb_content="";
    String lyb_time="";

    if(true)
    {

    rs=statement.executeQuery(main_info_sql);
    rs.next();
    info_name=rs.getString(1);
    info_from=rs.getString(2);
    info_underwrite=rs.getString(3);
    info_chenghao=rs.getString(4);
    info_logtime=rs.getString(5);
    info_portrait=rs.getString(6);


    info_integral=Integer.parseInt(rs.getString(7));
    info_level=rs.getString(8);
    info_newmess=Integer.parseInt(rs.getString(9));
    info_replymess=Integer.parseInt(rs.getString(10));
    info_degree=rs.getString(11);

    lyb_title=rs.getString(12);
    lyb_content=rs.getString(13);
    lyb_time=rs.getString(14);

    if(true)
    {
    String add_hit_num="update p_bbs_main set hit_num=hit_num+1 where id= "+lyb_id;
    statement.executeQuery(add_hit_num);
    }
    int judge_record;
    if(true)
    {
    String sel_rp_count_id="select count(this_id) from p_bbs_reply where super_id= "+lyb_id;
    rs=statement.executeQuery(sel_rp_count_id);
    rs.next();
    judge_record=Integer.parseInt(rs.getString(1));
    }
    if(judge_record!=0)
    {
    int i=0;
    int m=0;
    String rp_sql="select a.name,a.infofrom,a.underwrite,a.chenghao,a.logtime,a.portrait,"+
    "b.commintegral,b.commlevel,b.newmess,b.replymess,b.degree,"+
    "c.this_id,c.content,c.time "+
    "from p_info a,p_info_comm b,p_bbs_reply c "+
    "where a.id=b.id and a.name=c.name and c.super_id="+lyb_id;
    System.out.println("LybContent-- sql is: "+rp_sql);
    rs=statement.executeQuery(rp_sql);

    String info_rp_name[]=new String[10000];
    String info_rp_from[]=new String[10000];
    String info_rp_underwrite[]=new String[10000];
    String info_rp_chenghao[]=new String[10000];
    String info_rp_logtime[]=new String[10000];
    String info_rp_portrait[]=new String[100];

    int info_rp_integral[]=new int[10000];
    String info_rp_level[]=new String[10000];
    int info_rp_newmess[]=new int[10000];
    int info_rp_replymess[]=new int[10000];
    String info_rp_degree[]=new String[1000];

    int rp_this_id[]=new int[10000];
    String rp_content[]=new String[100000];
    String rp_time[]=new String[10000];
    while(rs.next())
    {
    info_rp_name[m]=rs.getString(1);
    info_rp_from[m]=rs.getString(2);
    info_rp_underwrite[m]=rs.getString(3);
    info_rp_chenghao[m]=rs.getString(4);
    info_rp_logtime[m]=rs.getString(5);
    info_rp_portrait[m]=rs.getString(6);

    info_rp_integral[m]=Integer.parseInt(rs.getString(7));
    info_rp_level[m]=rs.getString(8);
    info_rp_newmess[m]=Integer.parseInt(rs.getString(9));
    info_rp_replymess[m]=Integer.parseInt(rs.getString(10));
    info_rp_degree[m]=rs.getString(11);

    rp_this_id[m]=Integer.parseInt(rs.getString(12));
    rp_content[m]=rs.getString(13);
    rp_time[m]=rs.getString(14);

    catch (Exception e)
    {
    System.out.println("LybContent--*****1*****"+e.getMessage()+"*****1*****");
    e.printStackTrace();
    }
    public void destroy()
    {
    try
    {
    connection.close();
    statement.close();
    rs.close();
    }
    catch (Exception e)
    {
    System.err.println("LybContent--error to close Connection");
    }
    }}