我在按钮事件处理中连续使用executeQuery来去除数据库的值赋于变量。
发现只能执行一句。而后一句则不执行,为什么?
代码如下
public void jButton1_actionPerformed(ActionEvent e) {
        try {
rs=toUpdate.executeQuery("select zhjhdbl from jchshjb where ID="+1);
             rs.next();
             n10=rs.getFloat("zhjhdbl");
             rs.close();             rs=toUpdate.executeQuery("select zhxfzhj from gdzchzhjwxhdyzchtxb where ID="+(t-1)+"");
             rs.next();
             n11=rs.getFloat("zhxfzhj");
}
catch{
..........
这是为什么?请各位前辈帮忙!!!
谢谢!!!

解决方案 »

  1.   

    Only   one   ResultSet   object   per   Statement   object   can   be   open   at   any   point   in   time.   
      Therefore,   if   the   reading   of   one   ResultSet   object   is   interleaved   with   the   reading   of   another,   each   must   have   been   generated   by   different   Statement   objects.   All   statement   execute   methods   implicitly   close   a   statment's   current   ResultSet   object   if   an   open   one   exists.   
        
      一个Statement   object   一次只能打开一个ResultSet   object   
      

  2.   

    rs.close(); 
    是不是关闭了。。后面的没作用了。
      

  3.   

     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con=DriverManager.getConnection("jdbc:odbc:jjtz","sa","sa");
             toUpdate=con.createStatement();
             rs = toUpdate.executeQuery("select qshnf from xmmchnfb where ID="+1);
                         rs.next();
                        n1=rs.getInt("qshnf");
                        //con.setAutoCommit(false);
                         //toUpdate.addBatch("update jshtzgsb set yearn=(select year1 from jshtzb where ID='1') where ID="+1);
                         //toUpdate.executeBatch();
                         //con.commit();
                         //con.setAutoCommit(true);
                         rs = toUpdate.executeQuery("select zhzhnf from xmmchnfb where ID="+1);
                         rs.next();
                         n2=rs.getInt("zhzhnf");
                n3=n2-n1+1;
                rs = toUpdate.executeQuery("select year1 from jshtzb where ID=(select max(ID) from jshtzb)");
                         rs.next();
                         n4=rs.getInt("year1")+1;
                n5=n4-n1;
                rs = toUpdate.executeQuery("select zhjnx from jchshjb where ID="+1);
                        rs.next();
                        n6=rs.getInt("zhjnx");这个就可以
      

  4.   

    最奇怪的是如果两句之间有其他语句就可以执行,如中间插个循环语句。
                   rs=toUpdate.executeQuery("select ynshsde from lrylrfpb where ID="+t);
                 rs.next();
                 n8=rs.getFloat("ynshsde");
                 if(n8>0){
                     toUpdate.executeUpdate("update lrylrfpb set sdsh=(select ynshsde from lrylrfpb where ID="+t+")*(select sdshl from jchshjb where ID="+1+") where ID="+t);
                 }
                 else{
                     toUpdate.executeUpdate("update lrylrfpb set sdsh="+0+" where ID="+t);
                 }             toUpdate.executeUpdate("update lrylrfpb set jlr=(select ynshsde from lrylrfpb where ID="+t+")-(select sdsh from lrylrfpb where ID="+t+") where ID="+t);             rs=tUpdate.executeQuery("select dnhdqk from lrylrfpb where ID="+t);
                 rs.next();
                 n9=rs.getFloat("dnhdqk");
                 rs.close();可是只要两个紧挨在一起就不行。
      

  5.   

    你试一下,把rs.close();放在
    rs=toUpdate.executeQuery("select zhxfzhj from gdzchzhjwxhdyzchtxb where ID="+(t-1)+""); 
    rs.next(); 
    n11=rs.getFloat("zhxfzhj"); 
    下面
      

  6.   

    试过。
    也不行
    一个用rs
    另一个用rs1
    还是只能运行一个
      

  7.   

    那你设个断点,debug一下,看看到那步执行了没有?rs里的值改变了没有??
      

  8.   

    debug了。其实我在按钮事件中的数据库操作很多句
    这只是其中两句紧挨的
    因为debug了所以知道在第二个那里出错了
      

  9.   

    如果可以执行,而且rs里的值没改变,
    那就是你的SQL语句,没查到数据select zhxfzhj from gdzchzhjwxhdyzchtxb where ID="+(t-1)+""你看看这条语句,能执行查出数据吗??
      

  10.   

    可能是一个sqlserver的低版本JDBC驱动的原因. 
    1. 你可以多创建个Statement打开结果集. 
    2. 换个新版本的驱动, 也可到sun的网站上找个第三方的jdbc驱动
      

  11.   

    不是close的原因
    试过了。