Core JDBC Interfaces: Connection, Statement, ResultSet, and DriverManager
Core JDBC Interfaces: Connection, Statement, ResultSet, and DriverManager
जब भी हम Java के माध्यम से किसी Database से connect होकर data को fetch या manipulate करते हैं, तो उस प्रक्रिया को JDBC (Java Database Connectivity) कहा जाता है। JDBC हमें database से बात करने की सुविधा देता है, जैसे — data insert करना, update करना या read करना। लेकिन JDBC का core हिस्सा इसके interfaces हैं — Connection, Statement, ResultSet, और DriverManager। इन चारों को समझना बहुत जरूरी है क्योंकि यही JDBC architecture की नींव हैं।
Connection Interface
Connection interface database से एक physical link establish करने के लिए इस्तेमाल होता है। जब आप किसी database से connect होते हैं, तो JDBC driver इस connection को handle करता है। इसे ऐसे समझो जैसे आप किसी friend को call कर रहे हो — जब call connect होती है, तो आप दोनों के बीच communication शुरू हो जाता है। उसी तरह, Connection object के बनने पर Java और Database के बीच data flow शुरू हो जाता है।
Connection Interface का काम
- Database से connection establish करना।
- Transaction management करना (commit और rollback)।
- Statement object create करना ताकि SQL queries execute हो सकें।
- Connection को close करना जब काम पूरा हो जाए।
Connection Interface का Example
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/studentdb", "root", "password");
System.out.println("Connection established successfully!");
con.close();
} catch(Exception e) {
e.printStackTrace();
}
ऊपर के code में हमने MySQL database से connection establish किया है। अगर connection सफल होता है, तो “Connection established successfully!” print होता है।
Statement Interface
Statement interface SQL commands execute करने के लिए use होता है। जैसे आप database से कहते हैं – “मुझे सारे students के नाम दो”, तो Statement वही medium है जिसके ज़रिए ये command database तक जाती है।
Statement Interface की जिम्मेदारियाँ
- SQL queries को execute करना (SELECT, INSERT, UPDATE, DELETE)।
- Database से response प्राप्त करना।
- Query execution के बाद resources को release करना।
Statement Interface का Example
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/studentdb", "root", "password");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM students");
while(rs.next()) {
System.out.println(rs.getInt(1) + " " + rs.getString(2));
}
con.close();
यहाँ executeQuery() method select statement के लिए use किया गया है, जो ResultSet object return करता है। इसी ResultSet के ज़रिए हम database का data read कर सकते हैं।
Statement के Types
| Type | Use |
|---|---|
| Statement | Simple SQL queries execute करने के लिए |
| PreparedStatement | Parameterized queries के लिए (Dynamic values) |
| CallableStatement | Stored procedures call करने के लिए |
ResultSet Interface
ResultSet interface database से आने वाले result को store करता है। जब भी हम SELECT query चलाते हैं, तो उसका output ResultSet में आता है। इसे आप एक table की तरह समझ सकते हैं जहाँ rows और columns में data stored होता है।
ResultSet Interface की मुख्य बातें
- Data को row-wise और column-wise access किया जा सकता है।
- Pointer initial position पर “before first” row पर रहता है।
- next() method से अगले record पर move किया जाता है।
- Data read करने के लिए methods जैसे getInt(), getString(), getFloat() use होते हैं।
ResultSet Example
ResultSet rs = stmt.executeQuery("SELECT * FROM students");
while(rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
System.out.println(id + " - " + name);
}
इस example में ResultSet object के माध्यम से student table की सारी rows को read किया जा रहा है। हर बार next() method अगले record पर ले जाता है।
ResultSet के Types
| Type | Description |
|---|---|
| TYPE_FORWARD_ONLY | ResultSet सिर्फ आगे move कर सकता है। |
| TYPE_SCROLL_INSENSITIVE | Scroll किया जा सकता है, लेकिन database changes reflect नहीं होते। |
| TYPE_SCROLL_SENSITIVE | Scroll किया जा सकता है और changes reflect होते हैं। |
ResultSet के Concurrency Modes
| Mode | Use |
|---|---|
| CONCUR_READ_ONLY | Data सिर्फ read किया जा सकता है। |
| CONCUR_UPDATABLE | Data को update भी किया जा सकता है। |
DriverManager Class
DriverManager class JDBC में सबसे पहले काम करने वाला component है। इसका काम है Java application और database के बीच communication के लिए सही driver चुनना और connection provide करना।
DriverManager के Functions
- Registered JDBC drivers को manage करना।
- Database connection establish करना।
- Errors और exceptions को handle करना।
DriverManager Example
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/studentdb", "root", "password");
यहाँ DriverManager.getConnection() method database connection establish करता है। यह driver को internally call करता है ताकि proper database connection setup हो सके।
JDBC Interfaces का Workflow
अब इन चारों interfaces के बीच relation समझना बहुत जरूरी है। JDBC का पूरा process कुछ इस तरह चलता है:
- DriverManager → Database driver select करता है और Connection establish करता है।
- Connection → Database के साथ link बनाता है।
- Statement → SQL queries execute करता है।
- ResultSet → Query के results को hold करता है।
JDBC Workflow Diagram (Text Form)
Java Application
↓
DriverManager
↓
Connection
↓
Statement
↓
ResultSet
Transaction Management in JDBC
JDBC में transaction का मतलब है — एक logical unit of work जो या तो पूरा successfully execute होना चाहिए या बिल्कुल नहीं होना चाहिए। उदाहरण के लिए — अगर आप bank में transfer कर रहे हैं, तो amount sender account से घटे और receiver account में जुड़ना दोनों साथ होना चाहिए।
Transaction Control Methods
- commit() – Changes को permanently database में save करता है।
- rollback() – Error आने पर changes को undo करता है।
- setAutoCommit(false) – Auto commit बंद करके manual control देता है।
Transaction Example
con.setAutoCommit(false);
stmt.executeUpdate("UPDATE accounts SET balance=balance-1000 WHERE id=1");
stmt.executeUpdate("UPDATE accounts SET balance=balance+1000 WHERE id=2");
con.commit();
अगर बीच में कोई error आ जाए, तो आप con.rollback() call कर सकते हैं ताकि previous state restore हो जाए।
Importance of Core JDBC Interfaces
इन चारों core interfaces का combination ही JDBC को powerful बनाता है। इनके बिना Java application और database के बीच direct communication possible नहीं है।
- DriverManager — Connection setup करता है।
- Connection — Data communication channel बनाता है।
- Statement — SQL queries execute करता है।
- ResultSet — Query results को access करने में मदद करता है।
Exam-Oriented Notes
- Connection Interface: Database से connection establish करता है।
- Statement Interface: SQL queries execute करता है।
- ResultSet Interface: Query results को store और read करने के लिए use होता है।
- DriverManager Class: Database drivers को manage करता है और connection provide करता है।
- Transaction Management: commit(), rollback(), setAutoCommit(false) methods से control होता है।
- PreparedStatement: Dynamic queries execute करने के लिए बेहतर है।
- ResultSet Types: Forward-only, Scroll-insensitive, Scroll-sensitive।
- Concurrency Modes: CONCUR_READ_ONLY और CONCUR_UPDATABLE।
- JDBC Flow: DriverManager → Connection → Statement → ResultSet।
- Core JDBC Interfaces: JDBC communication की foundation हैं।