to update the contents of the resultset you need to create a statement by passing the resultset type updatable, as:
//creating a statement objectstatement stmt = con.createstatement(resultset.type_scroll_insensitive,resultset.concur_updatable);
就像getxxx()和setxxx()方法一样,resultset接口还提供了updatexxx()方法来更新结果集中行的内容。
这些方法接受表示要更新的行的索引或列标签的字符串值。
请注意,如果您需要更新resultset的内容,表格应该有一个主键。
示例假设我们有一个名为employees的表,其中有5条记录,如下所示:
+----+---------+--------+----------------+| id | name | salary | location |+----+---------+--------+----------------+| 1 | amit | 3000 | hyderabad || 2 | kalyan | 4000 | vishakhapatnam || 3 | renuka | 6000 | delhi || 4 | archana | 9000 | mumbai || 5 | sumith | 11000 | hyderabad |+----+---------+--------+----------------+
以下示例演示了如何更新结果集的内容:
import java.sql.*;public class resultsetexample { public static void main(string[] args) throws exception { //registering the driver drivermanager.registerdriver(new com.mysql.jdbc.driver()); //getting the connection string mysqlurl = "jdbc:mysql://localhost/testdb"; connection con = drivermanager.getconnection(mysqlurl, "root", "password"); system.out.println("connection established......"); //creating a statement object statement stmt = con.createstatement( resultset.type_scroll_sensitive, resultset.concur_updatable); //retrieving the data resultset rs = stmt.executequery("select * from employees"); //printing the contents of the table system.out.println("contents of the table: "); printrs(rs); //moving the pointer to the starting point in the resultset rs.beforefirst(); //updating the salary of each employee by 5000 while(rs.next()){ //retrieve by column name int newsal = rs.getint("salary") + 5000; rs.updateint( "salary", newsal ); rs.updaterow(); } system.out.println("contents of the resultset after increasing salaries"); printrs(rs); // set position to second record first rs.beforefirst(); rs.absolute(2); system.out.println("record we need to delete: "); 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(" "); //deleting the row rs.deleterow(); system.out.println("contents of the resultset after deleting one records..."); printrs(rs); system.out.println("goodbye!"); } public static void printrs(resultset rs) throws sqlexception{ //ensure we start with first row 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: hyderabadid: 2, salary: 4000, name: kalyan, location: vishakhapatnamid: 3, salary: 6000, name: renuka, location: delhiid: 4, salary: 9000, name: archana, location: mumbaiid: 5, salary: 11000, name: sumith, location: hyderabadconetnets of the resultset after increaing salariesid: 1, salary: 8000, name: amit, location: hyderabadid: 2, salary: 9000, name: kalyan, location: vishakhapatnamid: 3, salary: 11000, name: renuka, location: delhiid: 4, salary: 14000, name: archana, location: mumbaiid: 5, salary: 16000, name: sumith, location: hyderabadrecord we need to delete:id: 2, salary: 9000, name: kalyan, location: vishakhapatnamcontents of the resultset after deleting one records...id: 1, salary: 8000, name: amit, location: hyderabadid: 3, salary: 11000, name: renuka, location: delhiid: 4, salary: 14000, name: archana, location: mumbaiid: 5, salary: 16000, name: sumith, location: hyderabadgoodbye!
以上就是如何使用 jdbc 程序更新 resultset 的内容?的详细内容。