简体   繁体   中英

Java. Statement execution in cycle

I try to execute INSERT query in cycle:

String selectTableSQL = "SELECT * "
                      + "FROM testTable";

ResultSet rs = stmt.executeQuery(selectTableSQL);

while (rs.next()) {
     String rangeName = rs.getString("RANGENAME");                
     insertTableSQL = "INSERT INTO testTable2 "
                    + "VALUES ('" + rangeName + "')";

      try {
           stmt.executeUpdate(insertTableSQL);
      } catch (SQLException e) {
           // do nothing             
      }

But after one iteration this cycle breaks. If I don't execute INSERT query and print rangeName to screen everything works perfectly (there are a lot of values, not only one). So the problem is in statement execution. How can I solve it?

Thank you very much!

Of course it does: you're using the same stmt
Create a new stmt2 variable and use it inside loop without destroying the previous one.

First: Never do so

} catch (SQLException e) {
           // do nothing             
      }

Add a e.printStackTrace() to the catch block.

I think you get an exception when you do so. You may need a second statement variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM