String sql = "select * from memberinfo order by id desc limit ?,?"; 使用MySQL啊,还用Struts了啊。我来点简单的(实际项目肯定不是这样滴啊,用Oracle)[code=java]<%@ page contentType="text/html" pageEncoding="gb2312"%>
<%@ page import="java.sql.*" %>
<html>
<head><title></title>
<style type="text/css">
<!--
.STYLE6 {font-size: 12px}
.STYLE10 {font-size: 14px; font-weight: bold; }
-->
</style>
</head>
<script language="javascript">
function changeColor(obj,color){
obj.bgColor = color ;
}
</script>
<body>
<%!
public static final String URL = "emp_list_true.jsp" ;
%>
<%!
public static final String DBDRIVER = "oracle.jdbc.driver.OracleDriver" ;
public static final String DBURL = "jdbc:o racle:thin:@ localhost:1521:你的数据库的名字" ;
public static final String DBUSER = "scott" ;
public static final String DBPASSWORD = "tiger" ;
%>
<%
Connection conn = null ;
;P reparedStatement pstmt = null ;
ResultSet rs = null ;
%>
<%
int currentPage = 1 ; // 为当前所在的页,默认在第1页
int lineSize = 3 ; // 每次显示的记录数
int allRecorders = 0 ; // 表示全部的记录数
int pageSize = 1 ; // 表示全部的页数(尾页)
int lsData[] = {1,3,5,7,9,10,15,20,25,30,50,100} ;
%>
<%
try{
currentPage = Integer.parseInt(request.getParameter("cp")) ;
} catch(Exception e) {}
try{
lineSize = Integer.parseInt(request.getParameter("ls")) ;
} catch(Exception e) {}
%>
<%
Class.forName(DBDRIVER) ;
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD) ;
String sql = "SELECT COUNT(empno) FROM emp" ;
pstmt = conn.prepareStatement(sql) ;
rs = pstmt.executeQuery() ;
if(rs.next()){ // 取得全部的记录数
allRecorders = rs.getInt(1) ;
}
%>
<center>
<h1>雇员列表</h1>
<script language="javascript">
function go(num){
document.getElementById("cp").value = num ;
document.spform.submit() ; // 表单提交
}
</script>
<%
pageSize = (allRecorders + lineSize -1) / lineSize ;
%>
<%
sql = "SELECT * FROM ( " +
" SELECT empno,ename,job,hiredate,sal,comm,ROWNUM rn " +
" FROM emp WHERE ROWNUM<=? ORDER BY empno) temp " +
" WHERE temp.rn>? " ;
pstmt = conn.prepareStatement(sql) ;
pstmt.setInt(1,currentPage * lineSize) ;
pstmt.setInt(2,(currentPage-1) * lineSize) ;
rs = pstmt.executeQuery() ;
%>
<form name="spform" action="<%=URL%>" method="post">
<input type="button" value="首页" onclick="go(1)" <%=currentPage==1?"DISABLED":""%>>
<input type="button" value="上一页" onclick="go(<%=currentPage-1%>)" <%=currentPage==1?"DISABLED":""%>>
<input type="button" value="下一页" onclick="go(<%=currentPage+1%>)" <%=currentPage==pageSize?"DISABLED":""%>>
<input type="button" value="尾页" onclick="go(<%=pageSize%>)" <%=currentPage==pageSize?"DISABLED":""%>>
跳转到第<select name="selcp" onchange="go(this.value)">
<%
for(int x=1;x<=pageSize;x++){
%>
<option value="<%=x%>" <%=x==currentPage?"SELECTED":""%>><%=x%></option>
<%
}
%>
</select>页
每页显示
<select name="ls" onchange="go(1)">
<%
for(int x=0;x<lsData.length;x++){
%>
<option value="<%=lsData[x]%>" <%=lsData[x]==lineSize?"SELECTED":""%>><%=lsData[x]%></option>
<%
}
%>
</select>
条
<input type="hidden" name="cp" id="cp" value="1"><!--定义隐藏域,用于传递参数currentPage-->
</form>
<TABLE BORDER="1" cellpadding="5" cellspacing="0" bgcolor="F2F2F2" width="100%">
<TR onMouseOver="changeColor(this,'white')" onMouseOut="changeColor(this,'F2F2F2')">
<td align="center" valign="middle"><span class="STYLE10">序号</span></td>
<td align="center" valign="middle"><span class="STYLE10">编号</span></td>
<td align="center" valign="middle"><span class="STYLE10">姓名</span></td>
<td align="center" valign="middle"><span class="STYLE10">职位</span></td>
<td align="center" valign="middle"><span class="STYLE10">雇佣日期</span></td>
<td align="center" valign="middle"><span class="STYLE10">工资</span></td>
<td align="center" valign="middle"><span class="STYLE10">奖金</span></td>
</TR>
<% int count = 0 ;
while(rs.next()){
int empno = rs.getInt(1) ;
String ename = rs.getString(2) ;
String job = rs.getString(3) ;
Date hiredate = rs.getDate(4) ;
double sal = rs.getDouble(5) ;
double comm = rs.getDouble(6) ;
%>
<TR onMouseOver="changeColor(this,'white')" onMouseOut="changeColor(this,'F2F2F2')">
<td align="center" valign="middle"><span class="STYLE10"><%=++count%></span></td>
<td align="center" valign="middle"><span class="STYLE6"><%=empno%></span></td>
<td align="center" valign="middle"><span class="STYLE6"><%=ename%></span></td>
<td align="center" valign="middle"><span class="STYLE6"><%=job%></span></td>
<td align="center" valign="middle"><span class="STYLE6"><%=hiredate%></span></td>
<td align="center" valign="middle"><span class="STYLE6"><%=sal%></span></td>
<td align="center" valign="middle"><span class="STYLE6"><%=comm%></span></td>
</TR>
<%
}
%>
</table>
<%
conn.close() ;
%>
</center>
</body>
</html>[/code]关键代码是[code=java]sql = "SELECT * FROM ( " +
" SELECT empno,ename,job,hiredate,sal,comm,ROWNUM rn " +
" FROM emp WHERE ROWNUM<=? ORDER BY empno) temp " +
" WHERE temp.rn>? " ;
pstmt = conn.prepareStatement(sql) ;
pstmt.setInt(1,currentPage * lineSize) ;
pstmt.setInt(2,(currentPage-1) * lineSize) ;
rs = pstmt.executeQuery() ;[/code]再补充一点,上面是直接发送sql查询语句的,也可以只发送参数,让数据库执行语句,可以编写存储过程(因为sql语句编译也要费时间,过程可以事先编译好)。
这是以oracle的emp表为例:
--先创建包package
create or replace package mypackage as
type v_cursor is ref cursor;
end mypackage;
--创建过程
create or replace procedure filterpage(
tablename in varchar2 --表名
pagesize in number --一页显示记录数
currpage in number --当前页面
totalcount out number --总记录数
pagecount out number --页数
p_cursor out mypackage.v_cursor--结果集
)
is
v_sql varchar2(100);
v_begin number:=(currpage-1)*pagesize + 1;
v_end number:=currpage*pagesize;
begin
v_sql:='select * from (select t1.*,rownum rn from '||tablename||'t1 where rownum <='||v_end||')where rn >='||v_begin;
open p_cursor for v_sql;
v_sql:='select count(*) from'||tablename;
execute immediate v_sql into totalcount;--立即执行并赋值
pagecount:=ceil(totalcount/pagesize); --向上取整(例如:5条记录,每页2条,就显示3页)
end;
给出一个调用的例子(核心部分)[code=java]//此处略去N个字符
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection("jdbc:o racle:thin:@ 127.0.0.1:ORCL","scott","tiger");
CallableStatement cs = conn.prepareCall("{call filterpage(?,?,?,?,?,?)}");
cs.setString(1,"emp");
cs.setInt(2,10);//每页显10条记录
cs.setInt(3,2);//显示第二页
//参数注册
cs.registerOutParameter(4,oracle.jdbc.oracleTypes.INTEGER);
cs.registerOutParameter(5,oracle.jdbc.oracleTypes.INTEGER);
cs.registerOutParameter(6,oracle.jdbc.oracleTypes.CURSOR);
//执行
cs.execute();
int totalCount = cs.getInt(4);
int pageCount = cs.getInt(5);
ResultSet rs = (ResultSet)cs.getObject(6);
while(rs.next()){
//emp表的字段:EMPNO,ENAME,JOB,MGR HIREDATE,SAL,COMM,DEPTNO
int empno = rs.getInt(1) ;
String ename = rs.getString(2) ;
String job = rs.getString(3) ;
Date hiredate = rs.getDate(5) ;
double sal = rs.getDouble(6) ;
double comm = rs.getDouble(7) ;
}
//此处略去N个字符[/code]我以前花了不少时间找到这些资料,现在整理出来,都是可运行的。
当然这些都是入门级的,以后肯定不是这些,但是作为学习,是必须要经历的(将java代码卸载jsp页面中)。
[ 本帖最后由 王亮 于 2011-07-23 14:00 编辑 ] |