Wednesday 11 December 2013

Example to demonstrate Scrollable ResultSet

//Example to demonstrate Scrollable ResultSet

import java.sql.*;
import java.util.*;
import java.io.*;
public class ScrollableRSEx1 {
public static void main(String s[]) throws Exception {

Driver d= (Driver) ( Class.forName(
"com.mysql.jdbc.Driver").newInstance());

Properties p=new Properties ();
p.put("user","root");
p.put("password","admin");

Connection con=d.connect("jdbc:mysql://localhost:3306/test",p);

Statement st= con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

String query="select * from emp where deptno=10";

ResultSet rs= st.executeQuery(query);
//Now the cursor of resultset will at beforeFirst & the result set produced is scrollable

System.out.println("EmpNo\tName");

while (rs.next()) {
System.out.print(rs.getInt(1)+"\t");
System.out.println(rs.getString(2));
}//while
//Now the cursor of resultset will at afterLast

System.out.println("Reading Data, moving the cursor in backward direction\n");

while (rs.previous()){
System.out.print(rs.getInt(1)+"\t");
System.out.println(rs.getString(2));
}//while
con.close();
}//main
}//class
/*
EmpNo Name
2 uday kumar
Reading Data, moving the cursor in backward direction

2 uday kumar

*/

No comments:

Post a Comment