deepeweb.github.io

JDBC Statement in Hindi, PreparedStatement in Hindi

JDBC Statement

JDBC (Java Database Connectivity) एक API (Application Programming Interface) है, जिसका उपयोग Java applications को database से connect करने और data को manage करने के लिए किया जाता है।

JDBC API के माध्यम से Java application SQL queries को execute कर सकता है और database से data retrieve या update कर सकता है।

JDBC में तीन प्रकार के Statements होते हैं:

Statement Interface का उपयोग basic SQL queries जैसे SELECT, INSERT, UPDATE, DELETE को execute करने के लिए किया जाता है।

Statement को execute करने के लिए executeQuery(), executeUpdate() और execute() methods का उपयोग किया जाता है।

Statement को Create करने के लिए Connection object से createStatement() method को call किया जाता है:

Statement stmt = conn.createStatement();

Example (Statement Interface का उपयोग करके SQL Query Execute करना)

Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "password");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM students");
while (rs.next()) {
    System.out.println(rs.getInt("id") + " " + rs.getString("name"));
}

Statement interface का उपयोग तब किया जाता है जब queries dynamic नहीं होतीं और बार-बार execute नहीं की जातीं।

Limitations of Statement Interface:

JDBC के माध्यम से database connection establish करने के लिए JDBC Driver, Connection, Statement, और ResultSet का उपयोग किया जाता है।



Prepared Statements in Hindi

PreparedStatement एक advanced version है Statement का, जिसे JDBC API में SQL queries को efficiently execute करने के लिए उपयोग किया जाता है।

यह precompiled SQL queries का उपयोग करता है, जिससे performance बेहतर होती है और हर बार SQL query को compile करने की जरूरत नहीं पड़ती।

SQL Injection से सुरक्षा प्रदान करता है, क्योंकि यह user input को parameters के रूप में लेता है और dynamically bind करता है।

PreparedStatement को create करने के लिए prepareStatement() method का उपयोग किया जाता है:

PreparedStatement pstmt = conn.prepareStatement("INSERT INTO students (id, name) VALUES (?, ?)");

Query में ? placeholders का उपयोग किया जाता है, जिन्हें बाद में set किया जाता है।

Example: Data Insert करना using PreparedStatement

Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "password");
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO students (id, name) VALUES (?, ?)");

pstmt.setInt(1, 101);  // पहला placeholder (id) सेट करना
pstmt.setString(2, "Rahul");  // दूसरा placeholder (name) सेट करना

int rowsInserted = pstmt.executeUpdate();
System.out.println(rowsInserted + " record inserted successfully!");

Example: Data Fetch करना using PreparedStatement

PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM students WHERE id = ?");
pstmt.setInt(1, 101);  // Placeholder को value देना

ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
    System.out.println(rs.getInt("id") + " " + rs.getString("name"));
}

Advantages of PreparedStatement:

Disadvantages:

Use Cases:




Request

अगर आपको यह article useful या interesting लगा हो, तो please इसे अपने dosto aur family ke साथ जरूर share करें। आपका एक छोटा सा कदम हमें और अच्छा content बनाने के लिए motivate करता है। Thank you!

ध्यान दें कि इस page पर आपको कुछ ads भी देखने को मिल सकते हैं। इसके लिए हम आपसे माफी चाहते हैं। हम इस content को तैयार करने में काफी मेहनत और time लगाते हैं, ताकि आपको valuable जानकारी मिल सके। इन्हीं ads की मदद से हम ये काम continue कर पाते हैं।

आपके support और understanding के लिए दिल से धन्यवाद।




Follow Us

Facebook Logo    Instagram Logo