Example Of CachedRowSet For Pagination >>>>>>>>>>>>>>>>>
Use the below given code to implement pagination..
package jdbc;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Scanner;
import javax.sql.rowset.CachedRowSet;
import com.sun.rowset.CachedRowSetImpl;
public class ExampleOfCachedRowSetForPagination {
private static final String query = "select * from USERS where DEPT='IT'";
public static void main(String[] args) throws ClassNotFoundException,SQLException {
int pageSize=10;
int index = 0;
Scanner sc= new Scanner(System.in);
System.out.println("Enter page index: ");
index=sc.nextInt();
int startIndex=((pageSize*index)-pageSize)+1;
int endIndex=(pageSize*index);
System.out.println("StartOfPage: "+startIndex+" EndOfPage: "+endIndex);
Connection conn = ConnectionManager.getConnection();
CachedRowSet crs = new CachedRowSetImpl();
populateTableData(conn,startIndex,endIndex,crs);
}
public static void populateTableData(Connection conn,int startIndex,int endIndex,CachedRowSet crs) throws SQLException {
crs.setPageSize(endIndex);
crs.setCommand(query);
crs.execute(conn);
System.out.println("CachedRowSet: " + crs.size());
if (crs.absolute(startIndex)) {
crs.previous();
while (crs.next()) {
String userName=crs.getString("USERNAME");
String dept=crs.getString("DEPT");
String wrkHrs=crs.getString("WORK_HOURS");
System.out.println(crs.getRow() + " - " + " USERNAME: "
+ userName);
System.out.println(crs.getRow() + " - " + " Dept: "
+ dept);
System.out.println(crs.getRow() + " - " + " WorkHours: "
+ wrkHrs + "\n");
//we can also update or insert columns using this code block.
/*if(cardStatusAction.equalsIgnoreCase("9-7")) {
crs.updateString("WORK_HOURS","10-6");
crs.updateRow();
crs.acceptChanges(conn);
}*/
}
}
}
}
Leave your comments/suggestions below, Otherwise the next time :)
Use the below given code to implement pagination..
package jdbc;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Scanner;
import javax.sql.rowset.CachedRowSet;
import com.sun.rowset.CachedRowSetImpl;
public class ExampleOfCachedRowSetForPagination {
private static final String query = "select * from USERS where DEPT='IT'";
public static void main(String[] args) throws ClassNotFoundException,SQLException {
int pageSize=10;
int index = 0;
Scanner sc= new Scanner(System.in);
System.out.println("Enter page index: ");
index=sc.nextInt();
int startIndex=((pageSize*index)-pageSize)+1;
int endIndex=(pageSize*index);
System.out.println("StartOfPage: "+startIndex+" EndOfPage: "+endIndex);
Connection conn = ConnectionManager.getConnection();
CachedRowSet crs = new CachedRowSetImpl();
populateTableData(conn,startIndex,endIndex,crs);
}
public static void populateTableData(Connection conn,int startIndex,int endIndex,CachedRowSet crs) throws SQLException {
crs.setPageSize(endIndex);
crs.setCommand(query);
crs.execute(conn);
System.out.println("CachedRowSet: " + crs.size());
if (crs.absolute(startIndex)) {
crs.previous();
while (crs.next()) {
String userName=crs.getString("USERNAME");
String dept=crs.getString("DEPT");
String wrkHrs=crs.getString("WORK_HOURS");
System.out.println(crs.getRow() + " - " + " USERNAME: "
+ userName);
System.out.println(crs.getRow() + " - " + " Dept: "
+ dept);
System.out.println(crs.getRow() + " - " + " WorkHours: "
+ wrkHrs + "\n");
//we can also update or insert columns using this code block.
/*if(cardStatusAction.equalsIgnoreCase("9-7")) {
crs.updateString("WORK_HOURS","10-6");
crs.updateRow();
crs.acceptChanges(conn);
}*/
}
}
}
}
Leave your comments/suggestions below, Otherwise the next time :)
No comments:
Post a Comment
Thanks for your comments/Suggestions.