B.Tech Students: Apply for Live Programming Internship C, C++, Java, Python ,Web page Designing, PHP Java JDBC Batch Processing Example | Login Technologies

Java JDBC Batch Processing

JDBC Batch Processing allows executing multiple SQL statements in a group, improving performance and reducing network load. It is best used for bulk insert, update, or delete operations.

JDBC बैच प्रोसेसिंग कई SQL स्टेटमेंट्स को एक साथ execute करने की सुविधा देती है, जिससे प्रदर्शन बेहतर होता है और नेटवर्क लोड कम होता है। यह bulk insert, update या delete के लिए उपयुक्त है।

Advantages of Batch Processing

  • Faster execution of multiple statements
  • Fewer database calls
  • Improved performance for bulk operations
  • Easy to manage with transactions
  • कई स्टेटमेंट्स को तेजी से execute करना
  • कम डेटाबेस कॉल्स
  • bulk ऑपरेशन्स में बेहतर प्रदर्शन
  • Transactions के साथ आसानी से मैनेज करना

Before Batch Processing: Table Data

+----+--------+
| id | name   |
+----+--------+
| 1  | Neha   |
+----+--------+

The students table initially contains only one record: Neha.

students टेबल में शुरू में सिर्फ एक रिकॉर्ड होता है: Neha।

Java Code: JDBC Batch Insert Example

The following example shows how to insert multiple student names into a MySQL table using PreparedStatement and batch processing.

नीचे दिया गया उदाहरण दिखाता है कि PreparedStatement और बैच प्रोसेसिंग का उपयोग करके MySQL टेबल में कई student नाम कैसे insert करें।

import java.sql.*;

public class JdbcBatchInsert {
  public static void main(String[] args) {
    String url = "jdbc:mysql://localhost:3306/studentdb";
    String user = "root";
    String password = "your_mysql_password";

    try {
      Class.forName("com.mysql.cj.jdbc.Driver");
      Connection con = DriverManager.getConnection(url, user, password);

      String sql = "INSERT INTO students (name) VALUES (?)";
      PreparedStatement pstmt = con.prepareStatement(sql);

      con.setAutoCommit(false);

      pstmt.setString(1, "Amit");
      pstmt.addBatch();

      pstmt.setString(1, "Sunita");
      pstmt.addBatch();

      pstmt.setString(1, "Ravi");
      pstmt.addBatch();

      int[] result = pstmt.executeBatch();
      con.commit();

      System.out.println("Records inserted: " + result.length);
      con.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

Explanation

  • Creates connection to MySQL using JDBC.
  • Prepares a SQL INSERT statement with one parameter.
  • Uses setString to insert three names.
  • Each insert is added to the batch with addBatch().
  • executeBatch() runs all commands in one go.
  • All changes committed together using commit().
  • JDBC का उपयोग करके MySQL से कनेक्शन बनाता है।
  • एक parameter वाला SQL INSERT तैयार करता है।
  • तीन नाम insert करने के लिए setString का उपयोग करता है।
  • हर insert को addBatch() से जोड़ा जाता है।
  • executeBatch() सभी स्टेटमेंट्स को एक साथ चलाता है।
  • commit() से सभी बदलाव एक साथ सेव होते हैं।
Expected Output
Records inserted: 3

This confirms that 3 records were inserted successfully using batch processing.

यह पुष्टि करता है कि बैच प्रोसेसिंग से 3 रिकॉर्ड सफलतापूर्वक insert किए गए।

After Batch Processing: Table Data

+----+--------+
| id | name   |
+----+--------+
| 1  | Neha   |
| 2  | Amit   |
| 3  | Sunita |
| 4  | Ravi   |
+----+--------+

The table now includes three additional records inserted using batch processing.

अब टेबल में बैच प्रोसेसिंग के ज़रिए तीन नए रिकॉर्ड जुड़ गए हैं।