获取错误“ResultSet关闭后不允许操作”

时间:2016-07-08 08:03:38

标签: java mysql database jdbc

我得到了例外:

  

ResultSet关闭后不允许操作

我哪里错了?

try{

     Class.forName("com.mysql.jdbc.Driver");

     Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/project","root","root");
     Statement s=con.createStatement();
     ResultSet rs1=s.executeQuery("Select * from items");

     while(rs1.next()) {
       q= (q+Integer.parseInt(rs1.getString("qty")));
       //update items Set qty=5 where name='Maggi';
       s.executeUpdate("update items SET qty="+q+" WHERE name='"+value+"'");
     }
}                                        
catch(Exception ee){
  System.out.println(ee);
}

1 个答案:

答案 0 :(得分:2)

由于您需要两个不同的Statement对象来执行查询和更新部件,请尝试:

[self.pagingScrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[lastView]-40-[view]"
                                                                                options:0
                                                                                metrics:nil
                                                                                  views:@{@"lastView":lastView, @"view":view, @"_pagingScrollView":_pagingScrollView}]];

还要考虑使用PreparedStatement并使用 try{ Class.forName("com.mysql.jdbc.Driver"); Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/project","root","root");enter code here Statement s=con.createStatement(); ResultSet rs1=s.executeQuery("Select * from items"); while(rs1.next()){ q= (q+Integer.parseInt(rs1.getString("qty"))); s=con.createStatement(); // <- create a new statement here s.executeUpdate("update items SET qty="+q+" WHERE name='"+value+"'"); } } catch(Exception ee){ System.out.println(ee); } } 方法来避免潜在的SQL注入