您可以将表的内容作为ResultSet检索,然后直接在表中插入新行。为此,首先,您需要确保您的ResultSet是可更新的。
ResultSet接口的moveToInsertRow()方法将光标导航到需要插入下一条记录的位置。因此,使用此方法将光标移动到适当的位置以插入一行。
ResultSet接口的updateXXX()方法允许您将值插入/更新到ResultSet对象中。
使用这些方法将值添加到新行中,例如,如果您需要在第1列插入整数值,而在第2列插入字符串值,则可以使用updateInt()
andupdateString()
方法这样做:
rs.updateInt(1, integerValue); rs.updateString(2, "stringValue");
insertRow()方法插入排至结果集和表中为好。
因此,使用此方法将上面创建的行插入到结果集对象和表中。
假设我们在数据库中有一个名为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 { //注册驱动程序 DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //获得连接 String url = "jdbc:mysql://localhost/testdb"; Connection con = DriverManager.getConnection(url, "root", "password"); //创建一个Statement对象 Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); //检索数据 ResultSet rs = stmt.executeQuery("select * from Employees"); //打印结果集的内容 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(); //再次检索结果集的内容 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