import
java.sql.Connection;
import
java.sql.PreparedStatement;
import
java.sql.ResultSet;
import
java.sql.SQLException;
import
jdbc.util.DBUtil;
public
class
PageDemo {
public
static
void
main(String[] args) {
PageDemo demo =
new
PageDemo();
demo.printPage(
5
,
3
);
}
public
void
printPage(
int
pageSize,
int
page){
int
begin = (page -
1
)*pageSize +
1
;
int
end = page*pageSize ;
String query =
"select * from dept"
;
String sql =
"select * from (select a.*, rownum rn from ("
+query+
") a where rownum<= ?) where rn>= ?"
;
System.out.println(sql);
Connection conn =
null
;
try
{
conn = DBUtil.getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(
1
, end);
ps.setInt(
2
, begin);
ResultSet rs = ps.executeQuery();
while
(rs.next()){
System.out.println(rs.getString(
"dname"
));
}
rs.close();
ps.close();
}
catch
(Exception e){
e.printStackTrace();
}
finally
{
DBUtil.close(conn);
}
}
}