Make updates in Updatable ResultSet : ResultSet Updatable « Database SQL JDBC « Java






Make updates in Updatable ResultSet

  
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Main {
  public static void main(String[] args) throws Exception {
    Class.forName("com.mysql.jdbc.Driver");
    Connection connection = DriverManager
        .getConnection("jdbc:mysql://localhost/testdb", "root", "");

    Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
        ResultSet.CONCUR_UPDATABLE);

    String query = "SELECT id, code, name, quantity, price FROM products";
    ResultSet uprs = statement.executeQuery(query);

    while (uprs.next()) {
      System.out.println(uprs.getString("id") + ":" + uprs.getString("code") + ":"
          + uprs.getString("name") + ":" + uprs.getInt("quantity") + ":" + uprs.getDouble("price"));
    }
    uprs.first();
    uprs.updateString("name", "Java");
    uprs.updateRow();
    uprs.next();
    uprs.deleteRow();

    uprs.moveToInsertRow();
    uprs.updateString("code", "1");
    uprs.updateString("name", "Data Structures");
    uprs.updateInt("quantity", 1);
    uprs.updateDouble("price", 5.99);
    uprs.insertRow();

    uprs.beforeFirst();
    while (uprs.next()) {
      System.out.println(uprs.getString("id") + "\t" + uprs.getString("code") + "\t"
          + uprs.getString("name") + "\t" + uprs.getInt("quantity") + "\t"
          + uprs.getDouble("price"));
    }
    connection.close();
  }
}

 

   
    
  








Related examples in the same category

1.If database support updatable result sets
2.Using UpdatableResultSet to insert a new row
3.Delete Row from Updatable ResultSet for MySQL
4.Insert Row to Updatable ResultSet from MySQL
5.Demo Updatable ResultSet
6.Updatable resultset with Oracle Driver
7.Determining If a Database Supports Updatable Result Sets: An updatable result set allows modification to data in a table through the result set.
8.Creating an Updatable Result Set
9.Determining If a Result Set Is Updatable
10.Updating a Row in a Database Table Using an Updatable Result Set
11.Cancelling Updates to an Updatable Result Set
12.Inserting a Row into a Database Table Using an Updatable Result Set
13.Deleting a Row from a Database Table Using an Updatable Result Set
14.Refreshing a Row in an Updatable Result Set