如何在JDBC中将数据插入CachedRowSet?说明?
CachedRowSet是断开连接的行集的基本实现。它连接到数据源,从中读取数据,与数据源断开连接并处理检索到的数据,重新连接到数据源并写入修改。
创建一个CachedRowSet
您可以使用RowSetFactory的createCachedRowSet()方法创建一个CachedRowSet对象。
您可以创建一个使用一个RowSetFactory中对象newfactory()的方法RowSetProvider方法。
使用上述方法创建CachedRowSet对象,如下所示-
//创建RowSet对象 RowSetFactory factory = RowSetProvider.newFactory(); CachedRowSet rowSet = factory.createCachedRowSet();
连接到数据源
创建RowSet对象后,您需要将其连接到所需的DataSource。
您可以通过将诸如用户名,密码,URL和数据源名称之类的属性的值设置为−来连接到数据源-
//设置URL- String mysqlUrl = "jdbc:mysql://localhost/SampleDB"; rowSet.setUrl(mysqlUrl); //设置用户名 rowSet.setUsername("root"); //设置密码 rowSet.setPassword("password");
准备并执行命令语句
缓存的行集具有命令属性,您可以将查询传递给该命令属性。使用setCommand()
方法将必需的查询设置为此属性。
rowSet.setCommand("select * from ProductSales");
您可以使用execute()
方法执行RowSet对象保存的查询。
rowSet.execute();
在CachedRowSet中插入一行
CachedRowSet接口的moveToInsertRow()方法将光标导航到需要插入下一条记录的位置。
因此,使用此方法将光标移动到适当的位置以插入一行。
CachedRowSet接口的updateXXX()方法允许您将值插入/更新到RowSet对象中。
使用这些方法将值添加到新行中,例如,如果需要在第1列插入整数值,在第2列插入字符串值,则可以使用updateInt()
andupdateString()
方法将其添加为-
rowSet.updateInt(1, integerValue); rowSet.updateString(2, "stringValue");
该insertRow()
方法将行插入到CachedRowSet以及表中。
因此,使用此方法将上面创建的行插入CachedRowSet对象和表中。
示例
假设我们在数据库中有一个名为ProductSales的表,其内容如下:
+----+-------------+--------------+--------------+--------------+-------+----------------+ | ID | ProductName | CustomerName | DispatchDate | DeliveryTime | Price |Location | +----+-------------+--------------+--------------+--------------+-------+----------------+ | 1 | Key-Board | Raja | 2019-09-01 | 05:30:00 | 7000 |Hyderabad | | 2 | Earphones | Roja | 2019-05-01 | 05:30:00 | 2000 |Vishakhapatnam | | 3 | Mouse | Puja | 2019-03-01 | 05:29:59 | 3000 |Vijayawada | | 4 | Mobile | Vanaja | 2019-03-01 | 04:40:52 | 9000 |Chennai | | 5 | Headset | Jalaja | 2019-04-06 | 18:38:59 | 6000 |Goa | +----+-------------+--------------+--------------+--------------+-------+----------------+
下面的示例将上述表的内容检索到CachedRowSet对象中,并向其中插入新记录。
import java.sql.Date; import java.sql.DriverManager; import java.sql.Time; import javax.sql.rowset.CachedRowSet; import javax.sql.rowset.RowSetFactory; import javax.sql.rowset.RowSetProvider; public class CachedRowSetExample { public static void main(String args[]) throws Exception { //注册驱动程序 DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //创建RowSet对象 RowSetFactory factory = RowSetProvider.newFactory(); CachedRowSet rowSet = factory.createCachedRowSet(); //设置URL- String mysqlUrl = "jdbc:mysql://localhost/SampleDB"; rowSet.setUrl(mysqlUrl); //设置用户名 rowSet.setUsername("root"); //设置密码 rowSet.setPassword("password"); //设置查询/命令 rowSet.setCommand("select * from ProductSales"); rowSet.execute(); System.out.println("Contents of the row set"); while(rowSet.next()) { System.out.print("ID: "+rowSet.getInt("ID")+", "); System.out.print("Product Name: "+rowSet.getString("ProductName")+", "); System.out.print("Customer Name: "+rowSet.getString("CustomerName")+", "); System.out.print("Dispatch Date: "+rowSet.getDate("DispatchDate")+", "); System.out.print("Delivery Time: "+rowSet.getTime("DeliveryTime")); System.out.print("Price: "+rowSet.getString("Price")+", "); System.out.print("Location: "+rowSet.getString("Location")); System.out.println(""); } //将数据插入RowSet对象 rowSet.moveToInsertRow(); rowSet.updateInt(1, 6); rowSet.updateString(2, "Laptop"); rowSet.updateString(3, "Jagadeesh"); rowSet.updateDate(4, new Date(1551899399000L)); rowSet.updateTime(5, new Time(1551899399000L)); rowSet.updateInt(6, 50000); rowSet.updateString(7, "Mumbai"); rowSet.insertRow(); rowSet.moveToCurrentRow(); System.out.println(""); System.out.println("Contents of the row set after inserting a new row: "); System.out.println(""); rowSet.beforeFirst(); while(rowSet.next()) { System.out.print("ID: "+rowSet.getInt("ID")+", "); System.out.print("Product Name: "+rowSet.getString("ProductName")+", "); System.out.print("Customer Name: "+rowSet.getString("CustomerName")+", "); System.out.print("Dispatch Date: "+rowSet.getDate("DispatchDate")+", "); System.out.print("Delivery Time: "+rowSet.getTime("DeliveryTime")); System.out.print("Price: "+rowSet.getString("Price")+", "); System.out.print("Location: "+rowSet.getString("Location")); System.out.println(""); } } }
输出结果
Contents of the row set ID: 1, Product Name: Key-Board, Customer Name: Raja, Dispatch Date: 2019-09-01, Delivery Time: 05:30:00Price: 7000, Location: Hyderabad ID: 2, Product Name: Earphones, Customer Name: Roja, Dispatch Date: 2019-05-01, Delivery Time: 05:30:00Price: 2000, Location: Vishakhapatnam ID: 3, Product Name: Mouse, Customer Name: Puja, Dispatch Date: 2019-03-01, Delivery Time: 05:29:59Price: 3000, Location: Vijayawada ID: 4, Product Name: Mobile, Customer Name: Vanaja, Dispatch Date: 2019-03-01, Delivery Time: 04:40:52Price: 9000, Location: Chennai ID: 5, Product Name: Headset, Customer Name: Jalaja, Dispatch Date: 2019-04-06, Delivery Time: 18:38:59Price: 6000, Location: Goa Contents of the table after inserting a new row: ID: 1, Product Name: Key-Board, Customer Name: Raja, Dispatch Date: 2019-09-01, Delivery Time: 05:30:00Price: 7000, Location: Hyderabad ID: 2, Product Name: Earphones, Customer Name: Roja, Dispatch Date: 2019-05-01, Delivery Time: 05:30:00Price: 2000, Location: Vishakhapatnam ID: 3, Product Name: Mouse, Customer Name: Puja, Dispatch Date: 2019-03-01, Delivery Time: 05:29:59Price: 3000, Location: Vijayawada ID: 4, Product Name: Mobile, Customer Name: Vanaja, Dispatch Date: 2019-03-01, Delivery Time: 04:40:52Price: 9000, Location: Chennai ID: 5, Product Name: Headset, Customer Name: Jalaja, Dispatch Date: 2019-04-06, Delivery Time: 18:38:59Price: 6000, Location: Goa ID: 6, Product Name: Laptop, Customer Name: Jagadeesh, Dispatch Date: 1970-01-19, Delivery Time: 04:34:59Price: 50000, Location: Mumbai