#5, First Floor, 4th Street , Dr. Subbarayan Nagar Kodambakkam, Chennai-600 024 pro@slogix.in

Office Address

  • #5, First Floor, 4th Street Dr. Subbarayan Nagar Kodambakkam, Chennai-600 024 Landmark : Samiyar Madam
  • pro@slogix.in
  • +91- 81240 01111

Social List

How to update ResultSet in database in java?

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.

Sample Code
  • 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);

}

}

}


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