Navigating Resultset
By default the result set cursor points to the row before the last row of result set. A call to next retrieves the first result set row. The cursor can also be moved by one of the following result set method.

  1. beforeFirst()
  2. first()
  3. last()
  4. afterLast()
  5. absolute(pos)
  6. relative(pos)
  7. next()

 

Program on Resultset methods

import java.sql.*;
public class Sample
{
public static void main(String args[])
{
Connection con=null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:vision","scott","tiger");
Statement st=con.createStatement();             
ResultSet rs=st.executeQuery(“select * from emp”);
rs.next();
System.out.println(rs.getInt(1)+rs.getString(2)+rs.getInt(3));
rs.last();
System.out.println(rs.getInt(1)+rs.getString(2)+rs.getInt(3));
rs.previous();
System.out.println(rs.getInt(1)+rs.getString(2)+rs.getInt(3));
rs.absolute(2);
System.out.println(rs.getInt(1)+rs.getString(2)+rs.getInt(3));
rs.close();
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
catch(SQLException e)
{
e.printStackTrace();
}
finally
{
try
{
if(con!=null)
con.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
}
}

Program on ResultSet Transaction

import java.sql.*;
public class Sample
{
public static void main(String args[])
{
Connection con=null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:vision","scott","tiger");
con.setAutoCommit(false);
Statement st=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);                     
ResultSet rs=st.executeQuery(“select * from emp where eno=100”);
rs.next();
rs.updateInt(“sal”,5000);
rs.updateRow();
rs.moveToInsertRow();
rs.updateInt(“eno”,200);
rs.updateString(“na”,”Prasad”);
rs.updateInt(“sal”,6000);
rs.insertRow();
con.commit();
rs.close();
st.close();
con.close();

 

                }
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
catch(SQLException e)
{
e.printStackTrace();
}
finally
{
try
{
if(con!=null)
con.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
}
}