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

Java JDBC Example: Update Data in MySQL Database

In this tutorial, you'll learn how to update existing records in a MySQL table using Java JDBC. We'll use the PreparedStatement to safely change a student's name based on their ID.

इस ट्यूटोरियल में आप सीखेंगे कि Java JDBC का उपयोग करके MySQL टेबल में पहले से मौजूद रिकॉर्ड को कैसे अपडेट करें। हम PreparedStatement का उपयोग करके दिए गए ID के आधार पर छात्र का नाम बदलेंगे।

Prerequisites

  • MySQL server running on your system
  • A database named studentdb
  • A table named students with columns id (INT, AUTO_INCREMENT) and name (VARCHAR)
  • आपके सिस्टम पर चल रहा MySQL सर्वर
  • studentdb नाम का डेटाबेस
  • students नाम की टेबल जिसमें id (INT, AUTO_INCREMENT) और name कॉलम हों
Example existing data:
उदाहरण डेटा:
+----+--------+
| id | name   |
+----+--------+
| 1  | Aryan  |
| 2  | Riya   |
| 3  | Amit   |
+----+--------+

Java Code to Update Data

This Java code connects to MySQL and updates a student's name using PreparedStatement.

यह Java कोड MySQL से कनेक्ट करता है और PreparedStatement का उपयोग करके छात्र का नाम अपडेट करता है।

import java.sql.*;

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

    try {
      // Load JDBC driver
      Class.forName("com.mysql.cj.jdbc.Driver");

      // Create connection
      Connection con = DriverManager.getConnection(url, user, password);

      // Prepare SQL UPDATE statement
      String sql = "UPDATE students SET name = ? WHERE id = ?";
      PreparedStatement pstmt = con.prepareStatement(sql);

      // Set the new name and ID to update
      pstmt.setString(1, "Rahul");
      pstmt.setInt(2, 2);  // Update student with id=2

      // Execute the update
      int rowsAffected = pstmt.executeUpdate();
      System.out.println(rowsAffected + " record(s) updated.");

      // Close connection
      con.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

Explanation

  • Class.forName: Loads the MySQL JDBC driver.
  • DriverManager.getConnection: Connects to the database.
  • PreparedStatement: Helps execute parameterized SQL safely.
  • pstmt.setString(1, "Rahul"): Sets the new name value.
  • pstmt.setInt(2, 2): Sets the ID of the record to update.
  • executeUpdate(): Executes the UPDATE query.
  • Class.forName: MySQL JDBC ड्राइवर लोड करता है।
  • DriverManager.getConnection: डेटाबेस से कनेक्शन बनाता है।
  • PreparedStatement: पैरामीटराइज्ड SQL सुरक्षित रूप से execute करता है।
  • pstmt.setString(1, "Rahul"): नया नाम सेट करता है।
  • pstmt.setInt(2, 2): अपडेट करने के लिए ID सेट करता है।
  • executeUpdate(): UPDATE query को चलाता है।
Expected Output
1 record(s) updated.

This confirms that one record has been updated in the students table.

यह पुष्टि करता है कि students टेबल में एक रिकॉर्ड अपडेट किया गया है।

Check in mysql DataBase
1 record(s) updated.

+----+--------+
| id | name   |
+----+--------+
| 1  | Aryan  |
| 2  | Rahul  |
| 3  | Amit   |
+----+--------+

This confirms that one record has been updated in the students table, changing Riya to Rahul.

यह पुष्टि करता है कि students टेबल में एक रिकॉर्ड अपडेट किया गया है, जिसमें Riya को Rahul में बदल दिया गया है।