它是ResultSet类的常量,表示可更新的ResultSet对象的并发模式。通常,您会将其作为值传递给createStatement() 方法。
Statement createStatement(int resultSetType, int resultSetConcurrency);
以此为并发的ResultSet是可更新的。即,一旦获得ResultSet对象,就可以更新其内容。
假设我们在数据库中有一个名为Employee的表,其内容如下:
+----+---------+--------+----------------+ | Id | Name | Salary | Location | +----+---------+--------+----------------+ | 1 | Amit | 3000 | Hyderabad | | 2 | Kalyan | 4000 | Vishakhapatnam | | 3 | Renuka | 6000 | Delhi | | 4 | Archana | 96000 | Mumbai | | 5 | Sumith | 11000 | Hyderabad | | 6 | Rama | 11000 | Goa | | 7 | Mahesh | 5300 | Vishakhapatnam | | 8 | Ramesh | 12000 | Hyderabad | | 9 | Suresh | 7600 | Pune | | 10 | Santosh | 96000 | Mumbai | +----+---------+--------+----------------+
下面的示例将员工的薪水值增加5000,并打印结果。由于我们在其中创建的ResultSet是可更新的,因此您可以对其进行更新并显示值。
import java.sql.*; public class Updatable { public static void main(String [] args) throws Exception { //注册驱动程序 DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //获得连接 String mysqlUrl = "jdbc:mysql://localhost/TestDB"; Connection con = DriverManager.getConnection(mysqlUrl, "root", "password"); System.out.println("Connection established......"); //创建一个Statement对象 Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); //检索数据 ResultSet rs = stmt.executeQuery("select * from Employees"); //打印表的内容 System.out.println("Contents of the table: "); rs.beforeFirst(); while(rs.next()) { System.out.print("ID: " + rs.getInt("id")); System.out.print(", Salary: " + rs.getInt("Salary")); System.out.print(", Name: " + rs.getString("Name")); System.out.println(", Location: " + rs.getString("Location")); } System.out.println(); //的起点 rs.beforeFirst(); //每位员工的薪水提高5000- while(rs.next()) { //通过列名检索 int newSal = rs.getInt("Salary") + 5000; rs.updateInt( "Salary", newSal ); rs.updateRow(); } System.out.println("Contents of the ResultSet after increasing salaries"); rs.beforeFirst(); while(rs.next()) { System.out.print("ID: " + rs.getInt("id")); System.out.print(", Salary: " + rs.getInt("Salary")); System.out.print(", Name: " + rs.getString("Name")); System.out.println(", Location: " + rs.getString("Location")); } System.out.println(); } }
Connection established...... Contents of the table: ID: 1, Salary: 3000, Name: Amit, Location: Hyderabad ID: 2, Salary: 4000, Name: Kalyan, Location: Vishakhapatnam ID: 3, Salary: 6000, Name: Renuka, Location: Delhi ID: 4, Salary: 96000, Name: Archana, Location: Mumbai ID: 5, Salary: 11000, Name: Sumith, Location: Hyderabad ID: 6, Salary: 11000, Name: Rama, Location: Goa ID: 7, Salary: 5300, Name: Mahesh, Location: Vishakhapatnam ID: 8, Salary: 12000, Name: Ramesh, Location: Hyderabad ID: 9, Salary: 7600, Name: Suresh, Location: Pune ID: 10, Salary: 96000, Name: Santosh, Location: Mumbai Contents of the ResultSet after increasing salaries ID: 1, Salary: 8000, Name: Amit, Location: Hyderabad ID: 2, Salary: 9000, Name: Kalyan, Location: Vishakhapatnam ID: 3, Salary: 11000, Name: Renuka, Location: Delhi ID: 4, Salary: 101000, Name: Archana, Location: Mumbai ID: 5, Salary: 16000, Name: Sumith, Location: Hyderabad ID: 6, Salary: 16000, Name: Rama, Location: Goa ID: 7, Salary: 10300, Name: Mahesh, Location: Vishakhapatnam ID: 8, Salary: 17000, Name: Ramesh, Location: Hyderabad ID: 9, Salary: 12600, Name: Suresh, Location: Pune ID: 10, Salary: 101000, Name: Santosh, Location: Mumbai