使用 JDBC 更新 1000 多条记录 -

Update 1000+ Records using JDBC -

Hi, I'm trying to update 1000+ records using Java Database Connection (JDBC) but my solution doesn't work:

Connection c = null;
Statement statement = null;
String color = "init";
try{
String builder= "SELECT COLOR,SHAPE FROM TBL_SHAPES"; // this line of code will fetch 1000+ records of data from the database.
c = getConnection();
statement = c.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = statement.executeQuery(builder.toString());
if(rs.getFetchSize() > 0) {
while(rs.next()) {
    color = rs.getString("COLOR");
        if(!color.equalsIgnoreCase("init")) {//this code will check if the color is not "init" 
           switch(color) {
              case "green":
               rs.updateString("SHAPE", "TRIANGLE");
                  break;
                case "orange":
                   rs.updateString("SHAPE", "CIRCLE");
                  break;
                  case "yellow":
                    rs.updateString("SHAPE", "SQUARE");
                   break;
         }
       }
       rs.updateRow();
     }
    }}

Is there any efficient way to update 1000+ records? Thank you in advance!

您可以使用更新查询来更新 table

Connection c = null;
  PreparedStatement statement = null;
  String color = "init";
  try{
     String builder= "UPDATE TBL_SHAPES SET SHAPE = ? WHERE COLOR = ? ";  
     c = getConnection();
     statement = c.prepareStatement(builder);
     statement.setString(1, "TRIANGLE");
     statement.setString(2, "green");
     statement.addBatch();
     statement.setString(1, "CIRCLE");
     statement.setString(2, "orange");
     statement.addBatch();
     statement.setString(1, "SQUARE");
     statement.setString(2, "yello");
     statement.addBatch();
     int[] result = statement.executeBatch();

  }catch(Exception e){
    
  }

您可以使用这样的代码来修复它。希望这是你的要求