Java ResultSet moveToInsertRow() 方法及示例
当我们执行某些SQL查询(通常是SELECT查询)时,它们会返回表格数据。
java.sql.ResultSet 接口表示由SQL语句返回的此类表格数据。
即ResultSet对象保存由执行查询数据库语句的方法(通常是Statement接口的executeQuery()方法)返回的表格数据。
ResultSet对象有一个游标/指针,它指向当前行。最初,此游标位于第一行之前。
insertRow是JDBC中ResultSet的一个特殊行,它与可更新的ResultSet对象相关联。在其中,您可以使用UpdateXXX()方法((updateNClob(), updateNCharacterStream(), updateString(), updateInt(), updateNString(), updateBinaryStream()))构造新行。
在使用updateXXX()方法向表中的所有列添加值后,您可以使用insertRow()方法插入数据。
当您位于插入行时,您只能调用UpdateXXX()、getXXX()和insertRow()方法。
ResultSet接口的insertRow()方法将游标移动到插入行。
rs.moveToInsertRow(); rs.updateInt("ID", id); rs.updateString("First_Name", "Ishant"); rs.updateString("Last_Name", "Sharma"); rs.updateDate("Date_Of_Birth", new Date(904694400000L)); rs.updateString("Place_Of_Birth", "Delhi"); rs.updateString("Country", "India"); rs.insertRow();
要使用insertRow()方法将记录插入ResultSet,请:
创建ResultSet对象,例如:
Statement stmt = con.createStatement( ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet接口的moveToInsertRow()方法将游标导航到需要插入下一条记录的位置。因此,使用此方法将游标移动到适当的位置以插入一行。
rs.moveToInsertRow();
ResultSet接口的updateXXX()方法允许您向ResultSet对象中插入/更新值。
例如,如果您需要在第一列插入整数值,在第二列插入字符串值,可以使用updateInt()和updateString()方法,如下所示:
rs.updateInt(1, integerValue); rs.updateString(2, "stringValue");
最后,使用insertRow()方法将行插入到表和ResultSet中。
rs.insertRow();
示例
让我们使用CREATE语句在MySQL数据库中创建一个名为MyPlayers的表,如下所示:
CREATE TABLE MyPlayers( ID INT, First_Name VARCHAR(255), Last_Name VARCHAR(255), Date_Of_Birth date, Place_Of_Birth VARCHAR(255), Country VARCHAR(255), PRIMARY KEY (ID) );
现在,我们将使用INSERT语句在MyPlayers表中插入7条记录:
insert into MyPlayers values(1, 'Shikhar', 'Dhawan', DATE('1981-12-05'), 'Delhi', 'India'); insert into MyPlayers values(2, 'Jonathan', 'Trott', DATE('1981-04-22'), 'CapeTown', 'SouthAfrica'); insert into MyPlayers values(3, 'Kumara', 'Sangakkara', DATE('1977-10-27'), 'Matale', 'Srilanka'); insert into MyPlayers values(4, 'Virat', 'Kohli', DATE('1988-11-05'), 'Delhi', 'India'); insert into MyPlayers values(5, 'Rohit', 'Sharma', DATE('1987-04-30'), 'Nagpur', 'India'); insert into MyPlayers values(6, 'Ravindra', 'Jadeja', DATE('1988-12-06'), 'Nagpur', 'India'); insert into MyPlayers values(7, 'James', 'Anderson', DATE('1982-06-30'), 'Burnley', 'England');
下面的示例将MyPlayers表的内容检索为ResultSet对象,并向其中以及表中插入新记录。
import java.sql.Connection; import java.sql.Date; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class ResultSet_moveToInsertRow { 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_SENSITIVE, ResultSet.CONCUR_UPDATABLE); //Query to retrieve records String query = "Select * from MyPlayers"; //Executing the query ResultSet rs = stmt.executeQuery(query); rs.last(); int id = rs.getInt("ID")+1; rs.moveToInsertRow(); rs.updateInt("ID", id); rs.updateString("First_Name", "Ishant"); rs.updateString("Last_Name", "Sharma"); rs.updateDate("Date_Of_Birth", new Date(904694400000L)); rs.updateString("Place_Of_Birth", "Delhi"); rs.updateString("Country", "India"); rs.insertRow(); rs.beforeFirst(); System.out.println("Contents of the table after inserting a new row: "); while(rs.next()) { System.out.print("ID: "+rs.getInt("ID")+", "); System.out.print("Name: "+rs.getString("First_Name")+", "); System.out.print("Age: "+rs.getString("Last_Name")+", "); System.out.print("Salary: "+rs.getDate("Date_Of_Birth")+", "); System.out.print("Country: "+rs.getString("Place_Of_Birth")+", "); System.out.print("Address: "+rs.getString("Country")); System.out.println(); } } }
输出
Connection established...... Contents of the table after inserting a new row: ID: 1, Name: Shikhar, Age: Dhawan, Salary: 1981-12-05, Country: Delhi, Address: India ID: 2, Name: Jonathan, Age: Trott, Salary: 1981-04-22, Country: CapeTown, Address: SouthAfrica ID: 3, Name: Kumara, Age: Sangakkara, Salary: 1977-10-27, Country: Matale, Address: Srilanka ID: 4, Name: Virat, Age: Kohli, Salary: 1988-11-05, Country: Mumbai, Address: India ID: 5, Name: Rohit, Age: Sharma, Salary: 1987-04-30, Country: Nagpur, Address: India ID: 6, Name: Ravindra, Age: Jadeja, Salary: 1988-12-06, Country: Nagpur, Address: India ID: 7, Name: James, Age: Anderson, Salary: 1982-06-30, Country: Burnley , Address: England ID: 8, Name: Ishant, Age: Sharma, Salary: 1998-09-02, Country: Delhi, Address: India