How to get the current value of a particular row from a database using JDBC?



The refreshRow() method of the ResultSet interface refreshes the current row with the most recent value in the database.

rs.refreshRow()

Assume we have a table named cricketers_data with 7 records as shown below:

+----+------------+------------+---------------+----------------+-------------+ | ID | First_Name | Last_Name  | Year_Of_Birth | Place_Of_Birth | Country     | +----+------------+------------+---------------+----------------+-------------+ | 1  | Shikhar    | Dhawan     | 1981-12-05    | Delhi          | India       | | 2  | Jonathan   | Trott      | 1981-04-22    | CapeTown       | SouthAfrica | | 3  | Lumara     | Sangakkara | 1977-10-27    | Matale         | Srilanka    | | 4  | Virat      | Kohli      | 1988-11-05    | Mumbai         | India       | | 5  | Rohit      | Sharma     | 1987-04-30    | Nagpur         | India       | | 6  | Ravindra   | Jadeja     | 1988-12-06    | Nagpur         | India       | | 7  | James      | Anderson   | 1982-06-30    | Burnely        | England   | +----+------------+------------+---------------+----------------+-------------+

Following JDBC program updates the contents of the table and retrieves the new values using the refreshRow() method.

import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class ResultSet_CurrentValue {    public static void main(String args[])throws Exception {       //Registering the Driver       DriverManager.registerDriver(new com.mysql.jdbc.Driver());       //Getting the connection       String mysqlUrl = "jdbc:mysql://localhost/mydatabase";       Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");       System.out.println("Connection established......");       con.setAutoCommit(false);       //Creating a Statement object       Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,       ResultSet.CONCUR_UPDATABLE);       //Query to retrieve the contents of the employee_data table       String query = "select * from Cricketers_Data";       //Executing the query       ResultSet rs = stmt.executeQuery(query);       rs.absolute(4);       System.out.print("id: "+rs.getInt(1)+", ");       System.out.print("First name: "+rs.getString(2)+", ");       System.out.print("Last name: "+rs.getString(3)+", ");       System.out.print("Year of birth: "+rs.getDate(4)+", ");       System.out.print("Place of birth: "+rs.getString(5)+", ");       System.out.print("Country: "+rs.getString(6));       System.out.println(" ");       rs.absolute(4);       rs.updateString("Place_Of_Birth", "Delhi");       rs.refreshRow();       rs.absolute(4);       System.out.print("id: "+rs.getInt(1)+", ");       System.out.print("First name: "+rs.getString(2)+", ");       System.out.print("Last name: "+rs.getString(3)+", ");       System.out.print("Year of birth: "+rs.getDate(4)+", ");       System.out.print("Place of birth: "+rs.getString(5)+", ");       System.out.print("Country: "+rs.getString(6));       System.out.println(" ");       con.commit();       rs.close();       stmt.close();    } }

Output

Connection established...... id: 4, First name: Virat, Last name: Kohli, Year of birth: 1988-11-05, Place of birth: Mumbai, Country: India id: 4, First name: Virat, Last name: Kohli, Year of birth: 1988-11-05, Place of birth: Delhi, Country: India
Updated on: 2019-07-30T22:30:25+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements