java tutorial - jdbc resultset | java resultset - java programming - learn java - java basics - java for beginners



ResultSet interface

  • The SQL statements that read data from a database query, which return the data in a result set.
  • The SELECT statement is the standard way to select rows from a database and view them in a result set.
  • java.sql.ResultSet interface represents the result set of the database query.
  • The ResultSet object maintains a cursor which points to the present row in the result set.
  • The term "result set" refers to the row and column data contained in a ResultSet object.

Syntax

Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,  
                     ResultSet.CONCUR_UPDATABLE);  
click below button to copy the code. By - java tutorial - team

Methods of the ResultSet interface

public boolean next(): It is used to move the cursor to the one row next from the current position.
public boolean previous(): It is used to move the cursor to the one row previous from the current position.
public boolean first(): It is used to move the cursor to the first row in result set object.
public boolean last(): It is used to move the cursor to the last row in result set object.
public boolean absolute(int row): It is used to move the cursor to the specified row number in the ResultSet object.
public boolean relative(int row): It is used to move the cursor to the relative row number in the ResultSet object, it may be positive or negative.
public int getInt(int columnIndex): It is used to return data of the specified column index of the current row as int.
public int getInt(String columnName): It is used to return the data of specified column name of the current row as int.
public String getString(int columnIndex): It is used to return the data of specified column index of the current row as String.
public String getString(String columnName): It is used to return the data of specified column name of the current row as String.

Sample Code

  • ResultSet interface to retrieve the data of 2nd row.
import java.sql.*;  
public class wikitechy{  
public static void main(String args[])throws Exception{  
  
Class.forName("oracle.jdbc.driver.OracleDriver");  
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");  
Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);  
ResultSet rs=stmt.executeQuery("select * from wikitechy_emp1278");  
  
//getting the record of 2rd row  
rs.absolute(2);  
System.out.println(rs.getString(1)+" "+rs.getString(2));  
  
con.close();  
}}  
click below button to copy the code. By - java tutorial - team

Related Searches to jdbc resultset | java resultset