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

JDBC Update ResultSet Example

The following is an example of JDBC update result set using the description in the Result Set tutorialResultSet.CONCUR_UPDATABLEandResultSet.TYPE_SCROLL_INSENSITIVEExample. This example will demonstrate the INSERT, UPDATE, and DELETE operations on the table.

It should be noted that the table you are dealing with should be properly set up with a primary key.

This example code is written based on the environment and database settings in the previous chapters.

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

//Steps1.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/EMP";
   //  Database credentials
   static final String USER = "username";
   static final String PASS = "password";
   
 public static void main(String[] args) {
   Connection conn = null;
   try{
      //Steps2: Register JDBC driver
      Class.forName("com.mysql.jdbc.Driver");
      //Steps3:建立连接
      System.out.println("Connecting to database...");
      conn = DriverManager.getConnection(DB_URL,USER,PASS);
      //Steps4:执行查询以创建陈述
      // RS示例的必需参数。
      System.out.println("Creating statement...");
      Statement stmt = conn.createStatement(
                           ResultSet.TYPE_SCROLL_INSENSITIVE,
                           ResultSet.CONCUR_UPDATABLE);
     //Steps5:执行查询
      String sql = "SELECT id, first, last, age FROM Employees";
      ResultSet rs = stmt.executeQuery(sql);
      System.out.println("List result set for reference....");
      printRs(rs);
      //Steps6:循环浏览结果集,并增加5个年龄
      //移至BFR位置,以便while循环正常工作
      rs.beforeFirst();
      //Steps7:从结果集中提取数据
      while(rs.next()){
         //Search by column name
         int newAge = rs.getInt("age") + 5;
         rs.updateDouble( "age", newAge );
         rs.updateRow();
      }
      System.out.println("List result set showing new ages...");
      printRs(rs);
      // 在表中插入一条记录。
      //移动以使用updateXXX()插入行并添加列数据
      System.out.println("Inserting a new record...");
      rs.moveToInsertRow();
      rs.updateInt("id",104);
      rs.updateString("first","John");
      rs.updateString("last","Paul");
      rs.updateInt("age",40);
      //提交行
      rs.insertRow();
      System.out.println("List result set showing new set...");
      printRs(rs);
      
      // Delete the second record from the table.
      // First, set the position to the second record
      rs.absolute( 2 );
      System.out.println("List the record before deleting...");
      //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);
     //Delete row
      rs.deleteRow();
      System.out.println("List result set after ",
                deleting one records...
      printRs(rs);
      //Steps8Cleanup environment
      rs.close();
      stmt.close();
      conn.close();
   }
      //Handle JDBC error
      se.printStackTrace();
   }
      //Handle Class.forName error
      e.printStackTrace();
   }
      //Used to close resources
      try{
         if(conn!=null)
            conn.close();
      }
         se.printStackTrace();
      }
   }
   System.out.println("Goodbye!");
}//End main
   public static void printRs(ResultSet rs) throws SQLException{
      //Ensure we start from the first row
      rs.beforeFirst();
      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);
     }
     System.out.println();
   }//End printRs()
}//End JDBCExample

Now let's compile the above example as follows:

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

Running timeJDBCExampleIt will produce the following result-

C:\>java JDBCExample
Connecting to database...
Creating statement...
List result set for reference....
ID: 100, Age: 33, First: Zara, Last: Ali
ID: 101, Age: 40, First: Mahnaz, Last: Fatma
ID: 102, Age: 50, First: Zaid, Last: Khan
ID: 103, Age: 45, First: Sumit, Last: Mittal
List result set showing new ages...
ID: 100, Age: 38, First: Zara, Last: Ali
ID: 101, Age: 45, First: Mahnaz, Last: Fatma
ID: 102, Age: 55, First: Zaid, Last: Khan
ID: 103, Age: 50, First: Sumit, Last: Mittal
Inserting a new record...
List result set showing new set...
ID: 100, Age: 38, First: Zara, Last: Ali
ID: 101, Age: 45, First: Mahnaz, Last: Fatma
ID: 102, Age: 55, First: Zaid, Last: Khan
ID: 103, Age: 50, First: Sumit, Last: Mittal
ID: 104, Age: 40, First: John, Last: Paul
List the record before deleting...
ID: 101, Age: 45, First: Mahnaz, Last: Fatma
List result set after deleting one record...
ID: 100, Age: 38, First: Zara, Last: Ali
ID: 102, Age: 55, First: Zaid, Last: Khan
ID: 103, Age: 50, First: Sumit, Last: Mittal
ID: 104, Age: 40, First: John, Last: Paul
Goodbye!
C:\>