Welcome to

w3study.github.io




Topics

JDBC Statement

PreparedStatement



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 किया जाता है:

java

Statement stmt = conn.createStatement();

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

java

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 का उपयोग किया जाता है:

java

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

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

Example: Data Insert करना using PreparedStatement

java

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

java

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:



  • Advanced Java Notes in Hindi

  • Development of Android Applications Notes in Hindi


  • Request:

    हैलो दोस्तों! उम्मीद करता हूं आपको हमारा यह content/post पसंद आया होगा। अगर आपको हमारा ये content/post पसंद आई हो तो अपने दोस्तों के पास भी share करे। और अगर आपको कोई problem या कोई specific content हिन्दी में चाहिए है तो आप हमें नीचे दिए गए Email या whatsapp number के जरिए बता सकते है।

    अगर आप CCC/diploma/polytechnic/MCA/BCA etc कर रहे है तो ये website स्पेशली आपके लिए ही है, जो student हिंदी में पढ़ाई करते है।

    Contact Us

    Email: deepanshuranjan8057@gmail.com

    Whatsapp: +91 8057754706



    Follow Us

    Facebook Logo    Instagram Logo