Feedback Form

Introduction to Advanced ResultSet: Beyond Forward-Only Read-Only

Introduction to Advanced ResultSet: Beyond Forward-Only Read-Only

जब हम JDBC (Java Database Connectivity) के साथ काम करते हैं, तो ResultSet एक बहुत ही important component होता है। यह हमें database से आने वाले data को पढ़ने की सुविधा देता है। लेकिन क्या आप जानते हैं कि ResultSet केवल forward direction में move करने तक सीमित नहीं है? जी हाँ, JDBC में कुछ advanced types of ResultSet होते हैं जो data को backward, scrollable, और updatable तरीके से भी access करने की अनुमति देते हैं। आज हम इसी topic — Advanced ResultSet: Beyond Forward-Only Read-Only — को detail में समझेंगे।

What is ResultSet?

ResultSet एक object है जो database query से प्राप्त data को hold करता है। जब हम कोई SELECT query execute करते हैं, तो database से जो records आते हैं, वो ResultSet में store होते हैं। यह cursor-based होता है, यानी इसमें एक pointer होता है जो rows के बीच move करता है।

Basic Working of ResultSet

By default, ResultSet forward-only और read-only होता है। इसका मतलब है कि आप data को केवल आगे की direction में move करके read कर सकते हैं, modify नहीं कर सकते।

Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM students");
while(rs.next()) {
  System.out.println(rs.getString("name"));
}

ऊपर दिए गए example में cursor पहली row से शुरू होकर एक-एक कर सभी rows को forward direction में पढ़ता है। लेकिन कई बार हमें data को modify या random access करने की जरूरत होती है — यही काम Advanced ResultSet करता है।

Types of ResultSet

JDBC में ResultSet के तीन important types होते हैं, जो इस बात पर depend करते हैं कि cursor कैसे move कर सकता है और data को modify किया जा सकता है या नहीं।

ResultSet Type Description
TYPE_FORWARD_ONLY Cursor केवल forward direction में move करता है (default type)।
TYPE_SCROLL_INSENSITIVE Cursor आगे-पीछे दोनों direction में move कर सकता है, लेकिन database में हुए changes reflect नहीं होते।
TYPE_SCROLL_SENSITIVE Cursor bidirectional move कर सकता है और database में हुए changes reflect होते हैं।

इन types को हम statement बनाते समय specify कर सकते हैं, जैसे:

Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

ResultSet Concurrency Types

ResultSet के साथ concurrency भी define करनी होती है — यानी data को केवल read करना है या update भी करना है। इसके दो major concurrency types हैं:

  • CONCUR_READ_ONLY: Data को केवल read किया जा सकता है, modify नहीं।
  • CONCUR_UPDATABLE: ResultSet के through data को modify भी किया जा सकता है।

जब हम updatable ResultSet बनाते हैं, तो हम rows के अंदर values change कर सकते हैं और फिर updateRow() method के द्वारा database में save कर सकते हैं।

rs.absolute(2);
rs.updateString("name", "Amit Sharma");
rs.updateRow();

Scrollable ResultSet

Scrollable ResultSet एक advanced feature है जो cursor को दोनों directions (forward और backward) में move करने की सुविधा देता है। यह random access की तरह काम करता है।

Common Cursor Navigation Methods

  • next() – अगली row पर move करता है
  • previous() – पिछली row पर move करता है
  • first() – पहली row पर move करता है
  • last() – आखिरी row पर move करता है
  • absolute(int row) – किसी specific row पर move करता है
  • relative(int rows) – current position से offset पर move करता है

Example:

Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stmt.executeQuery("SELECT * FROM students");
rs.last();
System.out.println("Last Student: " + rs.getString("name"));
rs.first();
System.out.println("First Student: " + rs.getString("name"));

Sensitive vs Insensitive ResultSet

Scrollable ResultSet दो प्रकार का होता है — Sensitive और Insensitive। दोनों के बीच main अंतर यह है कि data update होने पर ResultSet reflect करेगा या नहीं।

Feature TYPE_SCROLL_INSENSITIVE TYPE_SCROLL_SENSITIVE
Data Update Reflection Database में changes reflect नहीं होते Database changes reflect होते हैं
Performance Fast Slow (क्योंकि dynamic data check करता है)
Use Case Static data reports, read-only views Real-time dashboards, live tables

Updatable ResultSet

Updatable ResultSet आपको data modify करने की अनुमति देता है। आप insert, update या delete operations perform कर सकते हैं, directly ResultSet के माध्यम से।

Methods for Updating Data

  • updateString() – String column update करने के लिए
  • updateInt() – Integer column update करने के लिए
  • updateRow() – Update apply करने के लिए
  • insertRow() – नई row insert करने के लिए
  • deleteRow() – Current row delete करने के लिए

Example:

Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery("SELECT * FROM students");
rs.moveToInsertRow();
rs.updateInt("id", 106);
rs.updateString("name", "Karan");
rs.insertRow();

ऊपर दिए गए example में हमने ResultSet के जरिए एक नई row insert की है। यह directly database में add हो जाएगी।

Advantages of Advanced ResultSet

  • Backward और random navigation possible है।
  • Live data reflection possible है (TYPE_SCROLL_SENSITIVE)।
  • Data modification (update, insert, delete) आसान बनता है।
  • Less dependency on SQL re-execution।
  • Real-time applications के लिए suitable।

Limitations of Advanced ResultSet

  • Performance थोड़ा slow हो सकता है क्योंकि cursor movement complex होता है।
  • Database support पर depend करता है (हर driver support नहीं करता)।
  • Memory consumption अधिक होती है क्योंकि ResultSet dynamic data maintain करता है।

Best Practices for Using Advanced ResultSet

  • अगर केवल reading करनी है, तो TYPE_FORWARD_ONLY use करें।
  • Frequent updates की जरूरत हो, तो TYPE_SCROLL_SENSITIVE के साथ CONCUR_UPDATABLE prefer करें।
  • Performance critical applications में ResultSet को बंद करना ना भूलें (rs.close(), stmt.close(), conn.close())।
  • Batch operations के लिए PreparedStatement का use करें ताकि performance optimize रहे।

Real-Life Example

मान लीजिए हमारे पास एक student management system है, जिसमें हमें किसी भी student का record update करना है बिना पूरी query फिर से चलाए। इस case में हम updatable और scrollable ResultSet का use करेंगे।

Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/school","root","password");
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery("SELECT * FROM students");
rs.absolute(3); // तीसरे student पर move करें
rs.updateString("name", "Rahul Verma");
rs.updateRow();
System.out.println("Record Updated Successfully!");

इस तरह आप बिना नई query लिखे existing data को directly ResultSet से update कर सकते हैं।

Summary Table

Feature Forward-Only Scroll-Insensitive Scroll-Sensitive
Cursor Movement Forward only Forward + Backward Forward + Backward
Database Changes Visible No No Yes
Updatable No Yes (if CONCUR_UPDATABLE) Yes (if CONCUR_UPDATABLE)
Performance High Moderate Low

Exam-Oriented Notes

  • ResultSet database से आने वाले data को hold करने वाला object है।
  • Default ResultSet: TYPE_FORWARD_ONLY + CONCUR_READ_ONLY।
  • Scrollable ResultSet data को forward और backward दोनों directions में access करता है।
  • Updatable ResultSet के through data को update, insert और delete किया जा सकता है।
  • TYPE_SCROLL_SENSITIVE ResultSet database में हुए changes को reflect करता है।
  • absolute(int) method किसी specific row पर cursor move करता है।
  • updateRow() method ResultSet में किए गए changes को database में apply करता है।
  • Performance के लिए केवल जरूरी type और concurrency का use करें।
  • Exam में हमेशा याद रखें: TYPE_SCROLL_INSENSITIVE changes reflect नहीं करता, TYPE_SCROLL_SENSITIVE करता है।