如何在 JDBC 中向 ResultSet 中插入行?
您可以將表的某些內容作為 ResultSet 檢索,並直接向其中插入新行。為此,首先,您需要確保 ResultSet 是可更新的。
ResultSet 介面的 moveToInsertRow() 方法將游標導航到您需要插入下一條記錄的位置。因此,使用此方法將游標移動到適當的位置以插入行。
ResultSet 介面的 updateXXX() 方法允許您將值插入或更新到 ResultSet 物件中。
使用以下方法向新行新增值,例如如果您需要在第 1 列插入一個整數值,並在第 2 列插入 String 值,則可以使用 updateInt() 和 updateString() 方法執行此操作,如下所示
rs.updateInt(1, integerValue); rs.updateString(2, "stringValue");
insertRow() 方法將行插入到 ResultSet 中以及表中。
因此,使用此方法將上述建立的行插入到結果集物件和表中。
示例
假設我們在資料庫中有一個名為 Employees 的表格,其內容如下
+----+---------+--------+----------------+ | 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 | +----+---------+--------+----------------+
以下示例將此表格的內容作為 ResultSet 物件檢索,並將新記錄插入到 ResultSet 以及表格中。
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class InsertRowToResultSet { public static void main(String[] args) throws Exception { //Registering the Driver DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //Getting the connection String url = "jdbc:mysql:///testdb"; Connection con = DriverManager.getConnection(url, "root", "password"); //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 Result Set System.out.println("Contents of the Result Set"); 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.moveToInsertRow(); rs.updateInt(1, 7); rs.updateString(2, "Santosh"); rs.updateInt(3, 96000); rs.updateString(4, "Mumbai"); rs.insertRow(); //Retrieving the contents of result set again System.out.println("Contents of the ResultSet after inserting another row in to it"); 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")); } } }
輸出
Contents of the Result Set 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 Contents of the ResultSet after inserting another row in to it 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: 96000, Name: Santosh, Location: Mumbai
廣告