- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is a RowSet object explain using a JDBC program?
A RowSet is a wrapper around a ResultSet Object. It can be connected, disconnected from the database and can be serialized. It maintains a JavaBean component by setting the properties. You can pass a RowSet object over the network. By default, RowSet object is scrollable and updatable and it is used to make a ResultSet object scrollable and updatable.
You Can get a RowSet using the
RowSetProvider.newFactory().createJdbcRowSet() method.
Example
Assume we have a table named dataset in the database as:
+--------------+-----------+ | 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 | +--------------+-----------+
Following JDBC example creates a RowSet object retrieves the contents of the table named dataset using this object:
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(""); } } }
Output
Contents of the table Brand: Iphone, Sale: 3000 Brand: Samsung, Sale: 4000 Brand: Nokia, Sale: 5000 Brand: Vivo, Sale: 1500 Brand: Oppo, Sale: 900 Brand: MI, Sale: 6400 Brand: MotoG, Sale: 4360 Brand: Lenovo, Sale: 4100 Brand: RedMi, Sale: 4000 Brand: MotoG, Sale: 4360 Brand: OnePlus, Sale: 6334
- Related Articles
- What is RowSet? How to retrieve contents of a table using RowSet? Explain?
- Explain the difference between RowSet and ResultSet in JDBC?
- What is RowId object in JDBC Explain?
- What is a CachedRowSet in JDBC? Explain?
- Is RowSet Scrollable? Explain with an example?
- How to insert a row into a ResultSet object using JDBC?
- How to insert a DATALINK object into a table using JDBC?
- How to retrieve a DATALINK object from a table using JDBC?
- What is JDBC SQL Escape Syntax Explain?
- What is CONCUR_UPDATABLE ResultSet in JDBC? Explain?
- What is CONCUR_READ_ONLY ResultSet in JDBC? Explain?
- How to delete a row from ResultSet object using JDBC?
- How to convert a String into a Date object using JDBC API?
- How to Navigate through a ResultSet using a JDBC program?
- How to convert a timestamp object in to Date in JDBC program?

Advertisements