Recent Posts

Tuesday, 13 November 2018

JDBC - Update a record in the table

     This tutorial provides an example on how to update a record in the table using JDBC application.

Note
     Before going into the program, it is advised to go through JDBC Programming Basic Steps where the meaning of Connection, Statement etc. are discussed.
import java.sql.*;
/**
 * 
 * @author ashok.mariyala
 *
 */
public class UpdateRecordDemo { 
   public static void main(String[] args) throws Exception {
      String sqlQuery = "update emp set esal=50000 where ename='Ashok Kumar"; 
      Class.forName("oracle.jdbc.OracleDriver"); 
      Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","scott","tiger"); 
      Statement st = con.createStatement();
      int updateCount = st.executeUpdate(sqlQuery);
      System.out.println("The number of rows updated : "+ updateCount);
      con.close();
   }
}
Update Multiple Records in the Table
import java.sql.*;
/**
 * 
 * @author ashok.mariyala
 *
 */
public class UpdateRecordDemo { 
   public static void main(String[] args) throws Exception {
      Class.forName("oracle.jdbc.OracleDriver"); 
      Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","scott","tiger"); 
      Statement st = con.createStatement();
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter Bonus Amount:");
      double bonus = sc.nextDouble();
      System.out.println("Enter Salary Range:");
      double salRange = sc.nextDouble();
      String sqlQuery=String.format("update emp set esal = esal+%f where esal<%f", bonus,salRange);
      int updateCount = st.executeUpdate(sqlQuery);
      System.out.println("The number of rows updated : "+ updateCount);
      con.close();
   }
}

Next Tutorial : JDBC - Delete a record in the table

Previous Tutorial : JDBC - Insert a record into table 

No comments:

Post a Comment