请问怎样加代码可以实现分页
public String cha() throws Exception {
Connection conn = DbConnection.getConnection();
Statement stmt = null;
try{
stmt = conn.createStatement();
String sql = "select * from text where id=1 ";
ResultSet rs = stmt.executeQuery(sql);
List list= new ArrayList();
while (rs.next()) {
User dt= new User();
dt.setId(rs.getInt("id"));
dt.setTime(rs.getString("time"));
list.add(dt);
}
HttpSession session = ServletActionContext.getRequest()
.getSession();
session.setAttribute("list", list);
conn.close();
rs.close();
stmt.close();
}catch (Exception e) {
e.printStackTrace();
}
return INPUT;
}
请问怎样加代码可以实现分页
我给说思路,代码你自己完全可以实现,假设你用的是jsp的模板;
我这里给你说下用mysql数据库实现分页的方式,oracle也一样,只需改下查询的sql语句即可;
首先通过select count(*) from table获取你表中一共与多扫跳数据;把得到的值放进作用域,request最优;
然后 通过 select * from table limit startRow,length; startRow的意思是从第几行开始,
startRow这个变量你可以从前台分页连接1 2 3....提交的时候获取,如果是1 startRow = (1-1)*10,2的话startRow = (2-1)*10,一次类推,第一次遍历的时候,startRow肯定为null,写个判断语句,如果为null,startRow=0;意思就是查数据库最前面的10条记录;
而length可以先写死,就是你每页显示的数量,好比length=10;
接着在jsp中,首先通过