Feedback Form

Navigating ResultSet: absolute(), relative(), first(), last(), previous()

Navigating ResultSet in JDBC: absolute(), relative(), first(), last(), previous()

Introduction to ResultSet Navigation

जब हम JDBC (Java Database Connectivity) का use करके database से data fetch करते हैं, तो query के result को ResultSet object में store किया जाता है। लेकिन सिर्फ data लाना ही काफी नहीं होता — कई बार हमें उस data को navigate यानी move करके अलग-अलग rows तक पहुंचना पड़ता है।

ResultSet navigation का मतलब है कि हम cursor (जो data rows पर move करता है) को किसी specific position पर ले जा सकें — जैसे पहली row, आखिरी row, या किसी particular row number पर। इस navigation को control करने के लिए JDBC हमें कुछ powerful methods देता है: absolute(), relative(), first(), last(), previous()

Types of ResultSet

ResultSet की navigation तभी possible होती है जब हम Scrollable ResultSet बनाते हैं। By default, ResultSet केवल forward direction में चलता है, लेकिन अगर हम उसे scrollable बनाएं तो cursor किसी भी direction में move कर सकता है।

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

Creating Scrollable ResultSet

Scrollable ResultSet बनाने के लिए हमें createStatement() method में parameters specify करने होते हैं। Example:

Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet rs = stmt.executeQuery("SELECT * FROM students");

अब यह ResultSet किसी भी direction में scroll किया जा सकता है। चलिए अब step-by-step समझते हैं कि navigation methods कैसे काम करते हैं।

1. first() Method

first() method cursor को ResultSet की पहली row पर ले जाता है। अगर data मौजूद है, तो यह true return करता है, अन्यथा false

if(rs.first()) { System.out.println("First Record: " + rs.getString("name")); }
  • इसका use तब होता है जब आपको ResultSet की पहली entry पर direct पहुंचना हो।
  • Example: किसी table का पहला student record print करना।

2. last() Method

last() method cursor को ResultSet की आखिरी row पर ले जाता है। इसका use तब किया जाता है जब आपको last record access करना हो।

if(rs.last()) { System.out.println("Last Record: " + rs.getString("name")); }
  • अगर ResultSet खाली है, तो यह false return करता है।
  • Useful जब हमें total count या last entry check करनी हो।

3. previous() Method

previous() method cursor को एक step पीछे move करता है। यह तब काम करता है जब ResultSet scrollable हो।

while(rs.previous()) { System.out.println(rs.getInt("id") + " " + rs.getString("name")); }
  • इस method का use तब होता है जब हम reverse direction में iterate करना चाहते हैं।
  • Example: Last से first तक records display करना।

4. absolute(int rowNumber) Method

absolute(int rowNumber) method cursor को किसी specific row number पर ले जाता है। अगर row exist करती है तो method true return करता है।

if(rs.absolute(3)) { System.out.println("3rd Record: " + rs.getString("name")); }
  • अगर rowNumber positive है तो counting first row से होती है।
  • अगर rowNumber negative है तो counting last row से होती है।
  • Example: rs.absolute(-1) मतलब last row पर जाना।

5. relative(int rows) Method

relative(int rows) method cursor को current position से relative move करता है — यानी जितनी rows आगे या पीछे move करना है।

rs.absolute(2); rs.relative(3); // अब cursor 5th row पर होगा
  • अगर rows positive हैं तो cursor आगे move करता है।
  • अगर rows negative हैं तो cursor पीछे move करता है।
  • Useful जब हमें dynamic navigation करनी हो।

Practical Example: ResultSet Navigation

अब देखते हैं एक complete example जिसमें हम ResultSet navigation methods को practically use करते हैं:

import java.sql.*; class ResultSetNavigationDemo { public static void main(String args[]) { try { Class.forName("com.mysql.cj.jdbc.Driver"); Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/college", "root", "password"); Statement stmt = conn.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet rs = stmt.executeQuery("SELECT * FROM students"); if(rs.first()) { System.out.println("First Record: " + rs.getString("name")); } if(rs.last()) { System.out.println("Last Record: " + rs.getString("name")); } if(rs.absolute(3)) { System.out.println("3rd Record: " + rs.getString("name")); } rs.relative(-2); System.out.println("After moving 2 back: " + rs.getString("name")); rs.previous(); System.out.println("Previous Record: " + rs.getString("name")); conn.close(); } catch(Exception e) { e.printStackTrace(); } } }

यह example दिखाता है कि कैसे हम ResultSet के cursor को flexible तरीके से control कर सकते हैं। यह exam point of view से भी important है क्योंकि अक्सर JDBC practical या viva में इन methods पर direct questions आते हैं।

Common Use Cases

  • first() और last() का use summary reports में होता है।
  • absolute() का use pagination या specific record access के लिए होता है।
  • relative() तब काम आता है जब हमें dynamic navigation चाहिए।
  • previous() का use reverse iteration के लिए किया जाता है।

Important Points to Remember

  • Scrollable ResultSet बनाने के लिए TYPE_SCROLL_INSENSITIVE या TYPE_SCROLL_SENSITIVE का use करें।
  • अगर ResultSet TYPE_FORWARD_ONLY है, तो इन methods का use नहीं किया जा सकता।
  • Database driver को भी scrollable ResultSet support करना चाहिए।
  • अगर ResultSet में कोई record नहीं है, तो methods false return करते हैं।
  • Cursor movement के बाद हमेशा valid position check करना चाहिए।

Performance Considerations

Scrollable ResultSet memory में data hold करता है, इसलिए large datasets के साथ यह performance issue create कर सकता है। अगर records बहुत ज़्यादा हैं, तो forward-only ResultSet बेहतर रहता है।

अगर आपको केवल कुछ records access करने हैं या random access चाहिए, तो scrollable ResultSet perfect choice है। Practical application में pagination या data viewer systems में इसका use किया जाता है।

Real Life Implementation

Suppose आपको एक student management system बनाना है जहाँ user “Next” और “Previous” buttons से records navigate करता है। ऐसे case में ResultSet navigation methods perfect solution हैं।

  • “Next” button → rs.next()
  • “Previous” button → rs.previous()
  • “First” button → rs.first()
  • “Last” button → rs.last()
  • “Go To Row” → rs.absolute(rowNumber)

इस तरह user easily records के बीच move कर सकता है, और developer को complex logic नहीं लिखना पड़ता।

Exam Tips (For Students)

  • हर method का return type और working याद रखें।
  • Scrollable ResultSet बनाने का syntax ज़रूर लिखें।
  • Practical exam में example code छोटे और clean रखें।
  • Question जैसे — “Explain absolute() and relative() methods with example” — बहुत common है।
  • यह topic theory + practical दोनों में पूछे जाते हैं।

Summary of Navigation Methods

Method Purpose Return Type
first() Moves cursor to first row boolean
last() Moves cursor to last row boolean
previous() Moves cursor one step backward boolean
absolute(int row) Moves cursor to specific row boolean
relative(int rows) Moves cursor relative to current position boolean

Final Notes (Quick Revision)

  • इन पाँच methods से हम ResultSet के cursor को पूरी तरह control कर सकते हैं।
  • Scrollable ResultSet के बिना ये methods काम नहीं करेंगे।
  • Exam में अगर short note पूछा जाए — तो सिर्फ purpose और example लिखना काफी है।
  • इन methods को practical में implement करने की practice करें ताकि concept clear हो।