English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JDBC Update Records

This chapter provides examples on how to use JDBC applications to update records in a table. Before executing the following example, please ensure that you have the following conditions:

  • To execute the following example, you can copyUsernameAndPasswordReplace with the actual username and password.

  • Your MySQL or any database you are using has been started and is running.

Necessary steps

To create a new database using a JDBC application, you need to perform the following steps-

  • Import package:It requires you to include the package that contains the JDBC classes required for database programming. Typically, use import java.sql.* That's enough.

  • Register JDBC driver: It requires you to initialize the driver so that you can open a communication channel with the database.

  • Establish a connection:You need to use DriverManager.getConnection() A method to create a Connection object that represents a physical connection to the database server.

  • Execute the query:You need to use an object of the Statement type to generate and submit SQL statements to update records in the table. This query uses the IN and WHERE clauses to update conditional records.

  • Clean up the environment: It is necessary to explicitly close all database resources instead of relying on JVM's garbage collection.

Example code

Copy and paste the following example into JDBCExample.java, compile and run as follows-

//Step1.Import the required software packages
import java.sql.*;
public class JDBCExample {
   // JDBC driver name and database URL
   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
   static final String DB_URL = "jdbc:mysql://localhost/STUDENTS";
   //  Database credentials
   static final String USER = "username";
   static final String PASS = "password";
   
   public static void main(String[] args) {
   Connection conn = null;
   Statement stmt = null;
   try{
      //Step2: Register JDBC driver
      Class.forName("com.mysql.jdbc.Driver");
      //Step3: Establish connection
      System.out.println("Connecting to a selected database...");
      conn = DriverManager.getConnection(DB_URL, USER, PASS);
      System.out.println("Connected to database successfully...");
      
      //Step4: Execute query
      System.out.println("Creating statement...");
      stmt = conn.createStatement();
      String sql = "UPDATE Registration" +
                   "SET age =" 30 WHERE id IN (100, 101);
      stmt.executeUpdate(sql);
      // Now you can extract all records
      // View updated records
      sql = "SELECT id, first, last, age FROM Registration";
      ResultSet rs = stmt.executeQuery(sql);
      while(rs.next()){
         //Search by column name
         int id = rs.getInt("id");
         int age = rs.getInt("age");
         String first = rs.getString("first");
         String last = rs.getString("last");
         //Display Value
         System.out.print("ID: ", + id;
         System.out.print(", Age: ", + age;
         System.out.print(", First: ", + first);
         System.out.println(", Last: ", + last);
      }
      rs.close();
   catch(SQLException se){
      //Handling JDBC errors
      se.printStackTrace();
   }
      //Handling Class.forName error
      e.printStackTrace();
   }finally{
      //Used for closing resources
      try{
         if(stmt!=null)
            conn.close();
      catch(SQLException se){
      }// Nothing to do
      try{
         if(conn!=null)
            conn.close();
      catch(SQLException se){
         se.printStackTrace();
      }
   }//End try
   System.out.println("Goodbye!");
}//End main
}//End JDBCExample

Now, let's compile the above example as follows:

C:\>javac JDBCExample.java
C:\>

RuntimeJDBCExampleIt will produce the following result-

C:\>java JDBCExample
Connecting to a selected database...
Connected database successfully...
Creating statement...
ID: 100, Age: 30, First: Zara, Last: Ali
ID: 101, Age: 30, First: Mahnaz, Last: Fatma
ID: 102, Age: 30, First: Zaid, Last: Khan
ID: 103, Age: 28, First: Sumit, Last: Mittal
Goodbye!
C:\>