1,我现在写了一个方法,把分页和关键字查询合并在一起
/**
* 实现分页+查询
* @param pageNow 当前页
* @param keyword 关键字段
* @return 以分布形式显示查询结果
*/
public ArrayList getRs(int pageNow,String keyword){
ArrayList list=new ArrayList();
try{
ct=new DBConn().getConn();//连接数据库
ps=ct.prepareStatement("select top "+pageSize+" * from users where id not in(select top "+pageSize*(pageNow-1)+" id from users) or username like ? or email like ? ");
ps.setString(1, "%"+keyword+"%");
ps.setString(2, "%"+keyword+"%");
rs=ps.executeQuery();
while(rs.next()){
Users user=new Users();
user.setId(rs.getInt(1));
user.setUsername(rs.getString(2));
user.setPasswd(rs.getString(3));
user.setEmail(rs.getString(4));
user.setGrade(rs.getInt(5));
list.add(user);
}
}catch(Exception e){
e.printStackTrace();
}finally{
this.close();
}
return list;
}
jsp页面
<%@ page language="java" contentType="text/html" pageEncoding="utf-8"%>
<%@ page import="java.util.*"%>
<%@ page import="com.td.dao.*" %>
<%@ page import="com.td.vo.*" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %>
<html>
<head>
<title>www.new.cn</title>
<script type="text/javascript">
function show(){
var pageNum=document.getElementById("pageNum").value;
window.location.href="list.jsp?pageNum="+pageNum;
}
</script>
</head>
<%
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
%>
<body>
<%
String keyword=request.getParameter("keyword");
UsersBean ub=new UsersBean();
if(keyword==null){
keyword="";
}
int pageNow=1;
String s_pageNow=request.getParameter("pageNum");
if(s_pageNow!=null){
pageNow=Integer.parseInt(s_pageNow);
}
//List<Users> all=ub.findAll(keyword);
List<Users> all=ub.getRs(pageNow,keyword);
Iterator<Users> it=all.iterator();
%>
<center>
<h1>列表界面</h1>
<hr>
<%
HttpSession hs=request.getSession(true);//获得共享数据
String myName=(String)hs.getAttribute("pname");
if(myName==null){
response.sendRedirect("login.jsp?info=");
}
%>
欢迎你登录 ,<%=myName %>
<%
Date ndate=(Date)hs.getAttribute("pdate");
SimpleDateFormat tdate=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
%>
登录日期:<%=tdate.format(ndate) %><br>
<form action="list.jsp" method="post">
请输入查询关键关<input type="text" name="keyword">
<input type="submit" value="查询">
<a href="users_list.jsp">添加数据</a>
<a href="login.jsp">返回登录</a>
</form>
<table border=1 width="80%">
<tr bgcolor="gray">
<th>雇员编号</th>
<th>雇员姓名</th>
<th>雇员密码</th>
<th>雇员邮箱</th>
<th>雇员级别</th>
<th colspan=2>操作权限</th>
</tr>
<tr>
<%
//取色
String color[]={"pink","gray"};
for(int i=0;i<all.size();i++){
Users user =(Users)all.get(i);
%>
<tr bgcolor=<%=color[i%2] %>>
<td><%=user.getId() %></td>
<td><%=user.getUsername() %></td>
<td><%=user.getPasswd() %></td>
<td><%=user.getEmail() %></td>
<td><%=user.getGrade() %></td>
<td><a href="users_update.jsp?uid=<%=user.getId() %>&uname=<%=user.getUsername()%>&upass=<%=user.getPasswd()%>&umail=<%=user.getEmail()%>&ugrade=<%=user.getGrade() %>">修改</a></td>
<td><a href="users_delete.jsp?uid=<%=user.getId() %>" onclick="javascript:return window.confirm('确认删除吗?')">删除</a></td>
</tr>
<%
}
%>
</table>
<%
int pageCount=ub.getPageCount();
int pageRow=ub.getPageRow();
%>
记录数【<%=pageRow %>】页数【<%=pageNow+"/"+pageCount %>】
<%
//上一页
if((pageNow!=1)&&(pageNow-1)!=0){
out.print("<a href=list.jsp?pageNum=1>首页</a>");
out.print("<a href=list.jsp?pageNum="+(pageNow-1)+">上一页</a>");
}
for(int i=pageNow;i<=pageNow+3;i++){
out.print("<a href=list.jsp?pageNum="+i+">["+i+"]</a>");
}
//下一页
if((pageNow!=pageCount)||(pageNow+1)==pageCount){
out.print("<a href=list.jsp?pageNum="+(pageNow+1)+">下一页</a>");
out.print("<a href=list.jsp?pageNum="+pageCount+">尾页</a>");
}
out.println("显示第");
out.println("<input type=text name=pageNum size=1>页");
out.println("<input type=submit value=跳转 onclick=show()>");
%>
</center>
</body>
</html>
我希望是可以在页面上能正常分页,翻页,并且可以在查询框中输入查询结果,也能显示查询结果,我这种程序要如何调整,或有什么好办法,请给予指导。我现在这个代码,在方法中加上关键字keyword,就不能实现翻页。如果把上面的方法分成两个:一个是分页,一个是查询的话,在jsp页面中又要如何实现?