Result Set Updation

Description

This program demonstartes how to update ResultSet. Using select query execution ResultSet is obtained which is updated which in turn updates the table in database. While creating statement object, settings for ResultSet Update is given (ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE). This setting enables ResultSet update.

  • Filename: RSUpdation.java

//Updating resultset
import java.sql.*;

class RSUpdate {

public static void main(String args[]) {

try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection con = DriverManager.getConnection("jdbc:odbc:user");

Statement sm1 = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);

sm1.execute("SELECT * FROM Result WHERE Marks>35");

ResultSet rs1 = sm1.getResultSet();

int count=0;

if(rs1 != null) {

while(rs1.next()) {

count++;

rs1.absolute(count);

rs1.updateString("Result","pass");

rs1.updateRow();

System.out.println(rs1.getString(1) + "\t");

System.out.println(rs1.getInt(2) + "\t");

System.out.println(rs1.getString(3) + "\t");

}
}

System.out.println();

sm1.close();

con.close();
}

catch (Exception e) {

System.out.println("Exception in the program" + e);

}

}

}

  • Table before execution
Result Set Updation
  • Table after execution
Result Set Updation
  • Student details with marks 35 above are selected and in the retrieved ResultSet, Result column is updated with string “pass”