jsp分页一定要把调用数据库的语句放jsp上吗
时间:2008-05-12 17:46:35
来源:论坛整理 作者: 编辑:chinaitzhe
网友回复:....根本不用放页面上的...
假如用hibernate
public List find(int page, int limit, String query, Object... values) {
Query q = getHibernateTemplate().getSessionFactory()
.getCurrentSession().createQuery(query);
//q.setCacheable(true);
q.setFirstResult((page - 1) * limit);
q.setMaxResults(limit);
for (int i = 0; i < values.length; i)
q.setParameter(i, values[i]);
List list = q.list();
return list;
}
假如只是jdbc的话要看用什么数据库写不同的sql了
网友回复:
- Java code
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ public List find(int page, int limit) { String sql = "select * from tableName limit (" (page-1)*limit "," limit ")";//mysql return stmt.executeQuery(sql); }
网友回复:上面的有点错..自己改改
网友回复:怎么可能呢..用struts hibernate做就不需要啊...
给你个例子看看:
- Java code
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ /** * showConfirmorders method mainly in the pages to display data * * @param mapping * @param form * @param request * @param response * @author wuwenqiang */ public ActionForward showConfirmorders(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { String method = "showConfigmoders"; String userId = (String) request.getSession().getAttribute("Portal"); int row; int startPage; int currentPage; int counts; int pageNumPerPag; String flag = "other_time"; List<OrderHistory> list_all = new LinkedList<OrderHistory>(); try { String current = request.getParameter("currentPage"); List globalSettings = _am.queryALlGlobalSettings(); int figure = DateManager.getInstance().compareTime(globalSettings); if (figure == Constant.BREAKFAST_TIME_FIGURE) { flag = "bf_time"; } else if (figure == Constant.LUNCH_TIME_FIGURE) { flag = "lunch_time"; } else if (figure == Constant.DINNER_TIME_FIGURE) { flag = "dinner_time"; } DateManager.getInstance().compareTime(_am.queryALlGlobalSettings()); long timeMillisStart = DateManager.getInstance() .get_timeMillisStart(); long timeMillisEnd = DateManager.getInstance().get_timeMillisEnd(); if (current == null || current.trim().equals("")) { currentPage = Constant.CURRENT_PAGE; } else { currentPage = Integer.parseInt(current); } counts = _am.countOrderHistory(timeMillisStart, timeMillisEnd); pageNumPerPag = Constant.PAGE_NUMPERPAG; row = Constant.ROW; startPage = (currentPage - 1) * row; List list = _am.queryOrderHistoryByPage(timeMillisStart, timeMillisEnd, startPage, row); List cancel_List = _am.cancelOrderHistory(timeMillisStart); if (list == null || list.size() <= 0) { request.setAttribute("list_all", list); this.debug(userId, method, Constant.LOG_QUERY_FAIL); return mapping.findForward("list"); } else { list_all = _ohOperation.addOrderHistory(list); Pagination page = new Pagination(list_all, counts, currentPage, row, pageNumPerPag); request.setAttribute("webString", page.getWebString()); request.setAttribute("list_all", list_all); request.setAttribute("cancel_list", cancel_List); request.setAttribute("flag", flag); this.debug(userId, method, Constant.LOG_QUERY_SUCCESS); return mapping.findForward("list"); } } catch (ManagerException e) { e.printStackTrace(); this.error(userId, method, "hibernate exception", e); } this.debug(userId, method, Constant.LOG_QUERY_FAIL); return mapping.findForward("return_error"); }
网友回复:
先去试试
网友回复:因为是初学,对你说的技术都不太了解,呵呵,我还是先用jsp serlvet java做出来了再学其他!
网友回复:一般都不会把 SQL 连接放在 jsp页面 不管是直连 还是桥连
要是放在上面的,你每点一次页面就 连接 一次 那还得了;
建议你 连接写在 Class 上 调用写在 Servlet上 JSP最好只写代码 除非非凡情况
网友回复:的确,写在jsp上后,页面的反应超慢的!
网友回复:JSP在MVC框架中,只是起到了一个显示作用,所以你可以用JAVABEAN来返回一个列表集合,然后用标签来显示
网友回复:将分页代码封装成javabean,页面调用javabean的方法
网友回复:我从来不放在jsp页面上,在java类中将被调用的方法,在创建时,设置好所要传递的参数不就OK了,
比如:public List test(String sqlcond,int curpge,int unitpage){
String sql=""
}
网友回复:直接写个通用的更好使,以后使用只要把通用类加入该工程就可以使用了,方便之极呀,本人上传的资源中有现成的,可以下来看一下
网友回复:现在谁还把分页放JSP页面上啊
都有数据层了哦
呵呵
假如是刚开始学的话,那就放在上面
网友回复:看你是使用的什么框架了比如Hibernate Spring就很好啊
public List getUsers(int pageSize, int startRow) throws HibernateException {
final int pageSize1 = pageSize;
final int startRow1 = startRow;
return this.getHibernateTemplate().executeFind(new HibernateCallback() {
public List doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery("from Users");
query.setFirstResult(startRow1);
query.setMaxResults(pageSize1);
return query.list();
}
});
}
public int getUserCount() {
return ((Long) getSession().createQuery("select count(*) from Users")
.list().get(0)).intValue();
}
网友回复:不用的··我空间里有把分页写到包含页的!
网友回复:用mysql的limit做出来了,等完成这任务了再来试其他方法,先就这样做吧,呵呵,多谢各位了!
网友回复:
- Java code
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ public ArrayList<Article> getArticleList(int fk_article, int account, int perPage) { ... String sql = "select * from article where fk_blog = ? limit " (account - 1) * perPage "," perPage;//account表示现在已经是第几条了,perPage是每页显示的数目 ... }
关键字:jsp,分页,一定要,调用,数据库,语句,
上一篇:以前碰到的一道笔试题
下一篇:下面没有链接了











文章评论
共有 0 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面