Scrollable Resultset in Java With Example

Scrollable Resultset in Java With Example. This Java Scrollable Resultset example describes the basic operations performed on the JDBC Scrollable Resultset database connectivity code.

Scrollable Resultset in Java With Example. This Java Scrollable Resultset example describes the basic operations performed on the JDBC Scrollable Resultset database connectivity code.In this example you will learn about how to write java code for Scrollable Resultset with oracle database.

Program:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
public class ScrollableResultSetDemo {
public static void main(String args[]){
Connection con=null;
Statement st = null;
ResultSet rs=null;
try {
Class.forName(“oracle.jdbc.driver.OracleDriver”);
con=DriverManager.getConnection(“jdbc:oracle:thin:@localhost:1521:ORCL”,”scott”,”tiger”);
st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs=st.executeQuery(“select * from emp”);
ResultSetMetaData rsmd = rs.getMetaData();
int colcount = rsmd.getColumnCount();
if(rs.first()){
System.out.println(“First Row: “);
for(int i=1;i<=colcount;i++)
System.out.print(” “+rsmd.getColumnName(i)+”: “+rs.getString(i));
}
if(rs.next()){
System.out.println(“\n Next Row: “);
for(int i=1;i<=colcount;i++)
System.out.print(” “+rsmd.getColumnName(i)+”: “+rs.getString(i));
}
if(rs.relative(4)){
System.out.println(“\n Relative Row: “);
for(int i=1;i<=colcount;i++)
System.out.print(” “+rsmd.getColumnName(i)+”: “+rs.getString(i));
}
if(rs.last()){
System.out.println(“\n Latst Row: “);
for(int i=1;i<=colcount;i++)
System.out.print(” “+rsmd.getColumnName(i)+”: “+rs.getString(i));
}
if(rs.previous()){
System.out.println(“\n Previous Row: “);
for(int i=1;i<=colcount;i++)
System.out.print(” “+rsmd.getColumnName(i)+”: “+rs.getString(i));
}
if(rs.absolute(3)){
System.out.println(“\n Absolute Row: “);
for(int i=1;i<=colcount;i++)
System.out.print(” “+rsmd.getColumnName(i)+”: “+rs.getString(i));
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Result:

First Row:
EMPNO: 7369 ENAME: SMITH JOB: CLERK MGR: 7902 HIREDATE: 12/17/1980 0:0:0 SAL: 800 COMM: null DEPTNO: 20
Next Row:
EMPNO: 7499 ENAME: ALLEN JOB: SALESMAN MGR: 7698 HIREDATE: 2/20/1981 0:0:0 SAL: 1600 COMM: 300 DEPTNO: 30
Relative Row:
EMPNO: 7698 ENAME: BLAKE JOB: MANAGER MGR: 7839 HIREDATE: 5/1/1981 0:0:0 SAL: 2850 COMM: null DEPTNO: 30
Latst Row:
EMPNO: 7934 ENAME: MILLER JOB: CLERK MGR: 7782 HIREDATE: 1/23/1982 0:0:0 SAL: 1300 COMM: null DEPTNO: 10
Previous Row:
EMPNO: 7902 ENAME: FORD JOB: ANALYST MGR: 7566 HIREDATE: 12/3/1981 0:0:0 SAL: 3000 COMM: null DEPTNO: 20
Absolute Row:
EMPNO: 7521 ENAME: WARD JOB: SALESMAN MGR: 7698 HIREDATE: 2/22/1981 0:0:0 SAL: 1250 COMM: 500 DEPTNO: 30