RowSets in Java: Disconnected Data Access with JDBC
RowSets in Java: Disconnected Data Access with JDBC
RowSet क्या होता है?
जब हम JDBC (Java Database Connectivity) का use करते हैं, तो data को access करने के लिए हम ResultSet object का इस्तेमाल करते हैं। लेकिन ResultSet हमेशा database से connected रहता है, यानी जब तक result read कर रहे हैं, तब तक database connection open रहना जरूरी है।
इसी limitation को दूर करने के लिए Java ने RowSet interface introduce किया जो disconnected data access provide करता है।
RowSet basically एक advanced version है ResultSet का जो data को hold करके offline भी use करने की सुविधा देता है।
RowSet के मुख्य Advantages
- Disconnected Operation: Database से disconnect होकर भी data को modify या access किया जा सकता है।
- Lightweight Data Handling: RowSet lightweight होता है क्योंकि इसे memory में store किया जा सकता है।
- Scrollable और Updatable: RowSet को आसानी से scroll या update किया जा सकता है।
- XML Support: RowSet XML format में data को पढ़ और लिख सकता है।
- Event Notification: RowSet events को support करता है जिससे जब data change हो, तो listener को notify किया जा सके।
RowSet के प्रकार (Types of RowSet)
Java में RowSet के कई implementations होते हैं जो अलग-अलग purpose के लिए use किए जाते हैं। नीचे इनके प्रमुख types दिए गए हैं:
| RowSet Type | Description |
|---|---|
| JdbcRowSet | Connected RowSet होता है जो सीधे database connection के साथ काम करता है। |
| CachedRowSet | Disconnected RowSet जो data को memory में cache कर लेता है और बाद में offline use किया जा सकता है। |
| WebRowSet | XML based RowSet जो data को XML format में store और transfer करता है। |
| JoinRowSet | दो या अधिक RowSets को join करने के लिए इस्तेमाल होता है। |
| FilteredRowSet | Data को filter करने के लिए specific condition apply करता है। |
CachedRowSet क्या है?
CachedRowSet सबसे popular और commonly used RowSet है क्योंकि ये disconnected mode में काम करता है। जब database से data retrieve किया जाता है, तो CachedRowSet उस data को memory में store कर लेता है और connection बंद कर दिया जाता है। बाद में जब जरूरत हो, उसी cached data पर operations किए जा सकते हैं — जैसे update, delete, iterate आदि।
CachedRowSet के Features
- Disconnected mode में काम करता है।
- Lightweight और memory-efficient होता है।
- Auto-synchronization support करता है (जब दोबारा connect किया जाए तो changes update हो जाते हैं)।
- JavaBean के रूप में use किया जा सकता है।
CachedRowSet का Example
import java.sql.*;
import javax.sql.rowset.*;
import com.sun.rowset.CachedRowSetImpl;
public class CachedRowSetExample {
public static void main(String[] args) throws Exception {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/college", "root", "root");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM student");
CachedRowSet crs = new CachedRowSetImpl();
crs.populate(rs); // data को cache कर लिया गया
con.close(); // अब connection बंद
while (crs.next()) {
System.out.println(crs.getInt("id") + " " + crs.getString("name"));
}
}
}
JdbcRowSet क्या है?
JdbcRowSet एक connected RowSet है जो हमेशा database connection से जुड़ा रहता है।
यह internally ResultSet की तरह काम करता है लेकिन additional functionalities देता है जैसे event notification और JavaBean support।
JdbcRowSet के Features
- Always connected रहता है।
- Scrollable और Updatable दोनों होता है।
- Event notification support करता है।
- JavaBeans component के रूप में use किया जा सकता है।
JdbcRowSet Example
import javax.sql.rowset.JdbcRowSet;
import com.sun.rowset.JdbcRowSetImpl;
public class JdbcRowSetExample {
public static void main(String[] args) throws Exception {
JdbcRowSet jrs = new JdbcRowSetImpl();
jrs.setUrl("jdbc:mysql://localhost:3306/college");
jrs.setUsername("root");
jrs.setPassword("root");
jrs.setCommand("SELECT * FROM student");
jrs.execute();
while (jrs.next()) {
System.out.println(jrs.getInt("id") + " " + jrs.getString("name"));
}
}
}
WebRowSet क्या है?
WebRowSet एक XML आधारित RowSet है जो data को XML format में store करता है। इसका फायदा यह है कि data को easily web के माध्यम से transmit किया जा सकता है या XML file में save किया जा सकता है।
WebRowSet के प्रमुख Uses
- Data को XML में export या import करने के लिए।
- Web applications में data transfer के लिए।
- Offline data synchronization के लिए।
WebRowSet Example
import javax.sql.rowset.WebRowSet;
import com.sun.rowset.WebRowSetImpl;
import java.io.FileWriter;
public class WebRowSetExample {
public static void main(String[] args) throws Exception {
WebRowSet wrs = new WebRowSetImpl();
wrs.setUrl("jdbc:mysql://localhost:3306/college");
wrs.setUsername("root");
wrs.setPassword("root");
wrs.setCommand("SELECT * FROM student");
wrs.execute();
FileWriter fw = new FileWriter("student.xml");
wrs.writeXml(fw);
fw.close();
}
}
JoinRowSet क्या है?
JoinRowSet का use तब किया जाता है जब हमें दो या अधिक RowSets को join करना हो। यह बिना SQL join query के data को logically combine करने की सुविधा देता है।
JoinRowSet के Features
- Multiple RowSets को merge करता है।
- Offline join operation possible है।
- Complex SQL joins को simplify करता है।
JoinRowSet Example
import javax.sql.rowset.*;
import com.sun.rowset.*;
public class JoinRowSetExample {
public static void main(String[] args) throws Exception {
CachedRowSet crs1 = new CachedRowSetImpl();
crs1.setUrl("jdbc:mysql://localhost:3306/college");
crs1.setUsername("root");
crs1.setPassword("root");
crs1.setCommand("SELECT id, name FROM student");
crs1.execute();
CachedRowSet crs2 = new CachedRowSetImpl();
crs2.setUrl("jdbc:mysql://localhost:3306/college");
crs2.setUsername("root");
crs2.setPassword("root");
crs2.setCommand("SELECT id, course FROM course_table");
crs2.execute();
JoinRowSet jrs = new JoinRowSetImpl();
jrs.addRowSet(crs1, "id");
jrs.addRowSet(crs2, "id");
while (jrs.next()) {
System.out.println(jrs.getInt("id") + " " + jrs.getString("name") + " " + jrs.getString("course"));
}
}
}
FilteredRowSet क्या है?
FilteredRowSet का use तब किया जाता है जब हमें RowSet में से केवल specific data records देखने या process करने हों। यह filter apply करके selected rows को ही access करने देता है।
FilteredRowSet के लाभ
- Specific rows को filter किया जा सकता है।
- Memory optimization possible होती है।
- Custom filter logic apply किया जा सकता है।
RowSet Events और Listener
RowSet event-driven architecture को support करता है।
जब भी RowSet में data change होता है, तो संबंधित listener को automatically notify किया जा सकता है।
इसके लिए Java में RowSetListener interface use होता है।
RowSetListener के Methods
| Method | Description |
|---|---|
rowSetChanged() | जब पूरा RowSet modify होता है। |
rowChanged() | जब कोई specific row change होती है। |
cursorMoved() | जब cursor किसी दूसरी row पर move करता है। |
RowSet के उपयोग के मुख्य फायदे
- Disconnected data handling आसान बनाता है।
- Network load कम करता है क्योंकि बार-बार connection की जरूरत नहीं होती।
- Memory में fast access देता है।
- Data portability बढ़ाता है (XML support के कारण)।
Real-World Use Cases
RowSet का उपयोग कई enterprise applications और offline systems में किया जाता है जैसे:
- Offline data editing applications
- Mobile apps में lightweight data access
- Web-based XML data sharing systems
- Data synchronization tools
Exam के लिए Short Notes on RowSet
- RowSet JDBC का advanced interface है जो disconnected data access provide करता है।
- मुख्य types: JdbcRowSet, CachedRowSet, WebRowSet, JoinRowSet, FilteredRowSet।
- CachedRowSet disconnected mode में काम करता है।
- WebRowSet XML format में data store करता है।
- RowSetListener events handle करने के लिए use होता है।
- RowSet JavaBeans compatible component है।
- Exam Point: RowSet और ResultSet में अंतर जानना जरूरी है।
ResultSet vs RowSet (Comparison Table)
| Feature | ResultSet | RowSet |
|---|---|---|
| Connection | Always connected | Disconnected (CachedRowSet) |
| Memory Storage | Directly from DB | Memory में store होता है |
| XML Support | No | Yes (WebRowSet) |
| JavaBean Support | No | Yes |
| Event Handling | No | Yes (RowSetListener) |
Summary
RowSet Java में data handling का modern और efficient तरीका है। यह traditional ResultSet की limitations को दूर करता है और disconnected, portable और flexible data access देता है। चाहे database offline हो या XML के जरिए data share करना हो — RowSet हर जगह use किया जा सकता है। Exam में अगर पूछा जाए कि “RowSet क्या है?” तो आप confidently बता सकते हैं — “RowSet JDBC का advanced interface है जो disconnected data access और data manipulation की सुविधा देता है।”