ResultSet 的 updateRow() 方法及示例
当我们执行某些 SQL 查询(通常是 SELECT 查询)时,它们会返回表格数据。
java.sql.ResultSet 接口表示 SQL 语句返回的此类表格数据。
即,ResultSet 对象保存由执行查询数据库语句的方法(通常是 Statement 接口的 executeQuery() 方法)返回的表格数据。
ResultSet 对象有一个光标/指针,指向当前行。最初,此光标位于第一行之前。
ResultSet 接口的 updateRow() 方法将当前行的内容更新到数据库。例如,如果我们使用 ResultSet 接口的 updateXXX() 方法(updateNClob()、updateNCharacterStream()、updateString()、updateInt()、updateNString()、updateBinaryStream())更新了特定记录的值
您需要调用此方法才能将对行所做的更改反映到数据库中。
注意:要使用 updateRow() 方法更新 ResultSet 的内容。ResultSet 必须是 CONCUR_UPDATABLE 类型,并且表必须包含主键约束。
让我们使用如下所示的 CREATE 语句在 MySQL 数据库中创建一个名为 customers 的表:
CREATE TABLE customers ( ID INT, Name VARCHAR(20), AGE INT, SALARY INT, ADDERSS VARCHAR(255), PRIMARY KEY (ID) );
现在,我们将使用 INSERT 语句在 customers 表中插入 12 条记录 -
insert into customers values(1, 'Amit', 25, 3000, 'Hyderabad'); insert into customers values(2, 'Kalyan', 27, 4000, 'Vishakhapatnam'); insert into customers values(3, 'Renuka', 30, 5000, 'Delhi'); insert into customers values(4, 'Archana', 24, 1500, 'Mumbai'); insert into customers values(5, 'Kaushik', 30, 9000, 'Kota'); insert into customers values(6, 'Hardik', 45, 6400, 'Bhopal'); insert into customers values(7, 'Trupthi', 33, 4360, 'Ahmedabad'); insert into customers values(8, 'Mithili', 26, 4100, 'Vijayawada'); insert into customers values(9, 'Maneesh', 39, 4000, 'Hyderabad'); insert into customers values(10, 'Rajaneesh', 30, 6400, 'Delhi'); insert into customers values(11, 'Komal', 29, 8000, 'Ahmedabad'); insert into customers values(12, 'Manyata', 25, 5000, 'Vijayawada');
以下 JDBC 程序建立与数据库的连接,将 customers 表的内容检索到 ResultSet 对象中,使用 updateXXX() 方法更新 id 值为 5 的行的内容,并使用 updateRow() 方法将整行更新到数据库。
示例
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class ResultSet_updateRow { public static void main(String args[]) throws SQLException { //Registering the Driver DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //Getting the connection String mysqlUrl = "jdbc:mysql://127.0.0.1/mydatabase"; Connection con = DriverManager.getConnection(mysqlUrl, "root", "password"); System.out.println("Connection established......"); //Creating the Statement Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); //Query to retrieve records String query = "Select * from Customers"; //Executing the query ResultSet rs = stmt.executeQuery(query); //Updating the contents of the record with id value 5 while(rs.next()) { if(rs.getInt("ID")==5) { //Updating the salary rs.updateInt("Salary", 11000); //Updating the address rs.updateString("ADDRESS","Narsipatnam"); //Updating the row rs.updateRow(); } } rs.beforeFirst(); System.out.println("Contents of the Customers table after the update: "); //Printing the contents of the table while(rs.next()) { System.out.print("ID: "+rs.getInt("ID")+", "); System.out.print("Name: "+rs.getString("Name")+", "); System.out.print("Age: "+rs.getInt("Age")+", "); System.out.print("Salary: "+rs.getInt("Salary")+", "); System.out.print("Address: "+rs.getString("Address")); System.out.println(); } } }
输出
Connection established...... Contents of the table Customers: ID: 1, Name: Amit, Age: 25, Salary: 3000, Address: Hyderabad ID: 2, Name: Kalyan, Age: 27, Salary: 4000, Address: Vishakhapatnam ID: 3, Name: Renuka, Age: 30, Salary: 5000, Address: Delhi ID: 4, Name: Archana, Age: 24, Salary: 1500, Address: Mumbai ID: 5, Name: Koushik, Age: 30, Salary: 11000, Address: Narsipatnam ID: 6, Name: Hardik, Age: 45, Salary: 6400, Address: Bhopal ID: 7, Name: Trupthi, Age: 33, Salary: 4360, Address: Ahmedabad ID: 8, Name: Mithili, Age: 26, Salary: 4100, Address: Vijayawada ID: 9, Name: Maneesh, Age: 39, Salary: 4000, Address: Hyderabad ID: 10, Name: Rajaneesh, Age: 30, Salary: 6400, Address: Delhi ID: 11, Name: Komal, Age: 29, Salary: 8000, Address: Ahmedabad ID: 12, Name: Manyata, Age: 25, Salary: 5000, Address: Vijayawada
广告