Delete Row from Updatable ResultSet for MySQL : ResultSet Updatable « Database SQL JDBC « Java






Delete Row from Updatable ResultSet for MySQL

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

public class DeleteRowUpdatableResultSet_MySQL {

  public static Connection getConnection() throws Exception {
    String driver = "org.gjt.mm.mysql.Driver";
    String url = "jdbc:mysql://localhost/octopus";
    String username = "root";
    String password = "root";
    Class.forName(driver);
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
  }

  public static void main(String[] args) {
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    try {
      conn = getConnection();
      String query = "select id, name from employees";
      stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
      rs = stmt.executeQuery(query);
      while (rs.next()) {
        String id = rs.getString(1);
        String name = rs.getString(2);
        System.out.println("id=" + id + "  name=" + name);
      }
      rs.first();
      rs.deleteRow();
      rs.beforeFirst();
      while (rs.next()) {
        String id = rs.getString(1);
        String name = rs.getString(2);
        System.out.println("id=" + id + "  name=" + name);
      }
    } catch (Exception e) {
      e.printStackTrace();
      System.exit(1);
    } finally {
      try {
        rs.close();
        stmt.close();
        conn.close();

      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
  }
}
           
         
  








Related examples in the same category

1.If database support updatable result sets
2.Using UpdatableResultSet to insert a new row
3.Insert Row to Updatable ResultSet from MySQL
4.Make updates in Updatable ResultSet
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