ResultSet遍历过程中修改自身数据,不会改变循环的过程;
import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; public class TestTbl { public static void main(String[] args) throws Exception { Connection tXdConn = Conn.getTXdConn(); String sql1 = "select * from tbl "; PreparedStatement statement = tXdConn.prepareStatement(sql1); ResultSet resultSet = statement.executeQuery(); while(resultSet.next()){ String content1 = resultSet.getString("content1"); if(content1.equals("b")){ String sql2 = "update tbl set content1 = ‘c‘"; PreparedStatement statement2 = tXdConn.prepareStatement(sql2); statement2.executeUpdate(); statement2.close(); String string = resultSet.getString("content1"); System.out.println("update:" + string); } System.out.println(content1); } resultSet.close(); statement.close(); tXdConn.close(); } }
测试结果:不会改变循环的过程;
时间: 2024-10-13 05:27:55