Nearby lessons

22 of 37

JDBC - RowSets

📌 What You Will Learn
  • What metadata means and the three metadata types in JDBC
  • DatabaseMetaData — information about the database
  • ResultSetMetaData — information about query results
  • The ResultSet types: forward-only, scrollable, updatable
  • What RowSets are and how they improve on ResultSet

RowSets is a fundamental part of database programming with JDBC. This lesson explains RowSets — A Better ResultSet and Program: JdbcRowSet (connected, scrollable) with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

RowSets — A Better ResultSet

A RowSet is a more flexible, powerful ResultSet. It can be disconnected from the database (works offline), supports scrolling and updating, and can even be sent over a network.

In simple words: A RowSet is a smarter ResultSet that can keep its data even after the connection closes. It works offline, scrolls freely, and can even be sent over a network.
PointResultSetRowSet
Connected to DB?Always connectedCan be disconnected (works offline)
Scrollable?Depends on the typeAlways scrollable (JdbcRowSet)
Can be serialized over a network?NoYes
Used forBasic query resultsAdvanced, offline, web-friendly data

Program: JdbcRowSet (connected, scrollable)

Trainer's Note: The javax.sql.rowset package has several RowSet types: JdbcRowSet (connected), CachedRowSet (disconnected, the most used), WebRowSet, JoinRowSet, and FilteredRowSet. For beginners, remember: RowSet = ResultSet + offline capability + scrollability.
Example02
JCode Cell
1import javax.sql.rowset.*;
2 
3public class RowSetDemo {
4 public static void main(String[] args) throws Exception {
5 JdbcRowSet jrs = RowSetProvider.newFactory().createJdbcRowSet();
6 jrs.setUrl("jdbc:mysql://localhost:3306/company");
7 jrs.setUsername("root");
8 jrs.setPassword("password");
9 jrs.setCommand("SELECT * FROM employee");
10 jrs.execute();
11 
12 while (jrs.next()) {
13 System.out.println(jrs.getInt(1) + " " + jrs.getString(2));
14 }
15 }
16}
Output
101 Rahul 102 Priya
📝 Key Takeaways
  • Metadata = data about data; JDBC has three kinds (Database, ResultSet, Parameter).
  • DatabaseMetaData (con.getMetaData()) describes the database and its tables.
  • ResultSetMetaData (rs.getMetaData()) describes the columns of a result.
  • ResultSet types: FORWARD_ONLY, SCROLL_INSENSITIVE, SCROLL_SENSITIVE.
  • Concurrency: READ_ONLY or UPDATABLE.
  • RowSets are flexible, scrollable, and can work disconnected from the database.

🧠 Test Your Knowledge

4 Questions
Progress: 0 / 4