- 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 RowSet? How to retrieve contents of a table using RowSet? Explain?
A RowSet object is similar to ResultSet, it also stores tabular data, in addition to the features of a ResultSet a RowSet follows JavaBeans component model. This can be used as a JavaBeans component in a visual Bean development environment, i.e. in environments like IDE’s you can visually manipulate these properties.
Connecting RowSet with database
The RowSet interface provides methods to set Java bean properties to connect it to the required database −
- void setURL(String url):
- void setUserName(String user_name):
- void setPassword(String password):
Properties
A RowSet object contains properties and each property have Setter and getter methods. Using these you can set and get values from a command property.
To set and get values of different datatypes the RowSet interface provides various setter and getter methods such as setInt(), getInt(), setFloat(), getFloat(), setTimestamp, getTimeStamp() etc…
Notification
Since RowSet object follow JavaBeans event model. Whenever events like cursor/pointer movement, insertion/deletion/updation of a row, change in the RowSet contents occur. All the registered components (components that implemented the RowSetListener methods) are notified.
Example
Assume we have a table named Dispatches in the database with 5 records as shown below −
+----+-------------+--------------+--------------+--------------+-------+----------------+ | 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 | +----+-------------+--------------+--------------+--------------+-------+----------------+
Following JDBC programs retrieves the Product Name, Customer Name, Location of the delivery, of the records with price greater than 5000 and, displays the result.
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/SampleDB"; rowSet.setUrl(mysqlUrl); //Setting the user name rowSet.setUsername("root"); //Setting the password rowSet.setPassword("password"); //Setting the query/command rowSet.setCommand("select * from Dispatches"); rowSet.setCommand("SELECT ProductName, CustomerName, Price, Location from Dispatches where price > ?"); rowSet.setInt(1, 2000); rowSet.execute(); System.out.println("Contents of the table"); while(rowSet.next()) { System.out.print("Product Name: "+rowSet.getString("ProductName")+", "); System.out.print("Customer Name: "+rowSet.getString("CustomerName")+", "); System.out.print("Price: "+rowSet.getString("Price")+", "); System.out.print("Location: "+rowSet.getString("Location")); System.out.println(""); } } }
Output
Product Name: Key-Board, Customer Name: Raja, Price: 7000, Location: Hyderabad Product Name: Mouse, Customer Name: Puja, Price: 3000, Location: Vijayawada Product Name: Mobile, Customer Name: Vanaja, Price: 9000, Location: Vijayawada Product Name: Headset, Customer Name: Jalaja, Price: 6000, Location: Vijayawada