
RowSet 是 ResultSet 对象的包装器。它可以与数据库连接、断开并且可以序列化。它通过设置属性来维护 JavaBean 组件。您可以通过网络传递 RowSet 对象。默认情况下,RowSet 对象是可滚动和可更新的,它用于使 ResultSet 对象可滚动和可更新。
您可以使用
RowSetProvider.newFactory( ).createJdbcRowSet() 方法。
WeShop唯象
WeShop唯象是国内首款AI商拍工具,专注电商产品图片的智能生成。
113 查看详情
示例
假设我们在数据库中有一个名为 dataset 的表:
+--------------+-----------+| mobile_brand | unit_sale |+--------------+-----------+| Iphone | 3000 || Samsung | 4000 || Nokia | 5000 || Vivo | 1500 || Oppo | 900 || MI | 6400 || MotoG | 4360 || Lenovo | 4100 || RedMi | 4000 || MotoG | 4360 || OnePlus | 6334 |+--------------+-----------+
以下 JDBC 示例创建一个 RowSet 对象,并使用该对象检索名为 dataset 的表的内容:
import java.sql.DriverManager;import javax.sql.RowSet;import javax.sql.rowset.RowSetProvider;public class RowSetExample { public static void main(String args[]) throws Exception { //Registering the Driver DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //Creating the RowSet object RowSet rowSet = RowSetProvider.newFactory().createJdbcRowSet(); //Setting the URL String mysqlUrl = "jdbc:mysql://localhost/TestDB"; rowSet.setUrl(mysqlUrl); //Setting the user name rowSet.setUsername("root"); //Setting the password rowSet.setPassword("password"); //Setting the query/command rowSet.setCommand("select * from Dataset"); System.out.println("Contents of the table"); while(rowSet.next()) { System.out.print("Brand: "+rowSet.getString(1)+", "); System.out.print("Sale: "+rowSet.getString(2)); System.out.println(""); } }}
输出
Contents of the tableBrand: Iphone, Sale: 3000Brand: Samsung, Sale: 4000Brand: Nokia, Sale: 5000Brand: Vivo, Sale: 1500Brand: Oppo, Sale: 900Brand: MI, Sale: 6400Brand: MotoG, Sale: 4360Brand: Lenovo, Sale: 4100Brand: RedMi, Sale: 4000Brand: MotoG, Sale: 4360Brand: OnePlus, Sale: 6334
以上就是使用 JDBC 程序解释什么是 RowSet 对象?的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/227843.html
微信扫一扫
支付宝扫一扫