一 前提
希望最新的纪录在开头给你的表建立查询:
表:mytable
查询:create or replace view as mytable_view from mytable order by id desc 其中,最好使用序列号create sequence mytable_sequence 来自动增加你的纪录id号 二 源程序
<%String sConn="你的连接"
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn=DriverManager.getConnection(sConn,"你的用户名","密码");
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
Statement stmtcount=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); ResultSet rs=stmt.executeQuery("select * from mytable_view");
String sqlcount="select count(*) from mytable_view";
ResultSet rscount=stmtcount.executeQuery(sqlcount); int pageSize=你的每页显示纪录数;
int rowCount=0; //总的记录数
while (rscount
int pageCount; //总的页数
int currPage; //当前页数
String strPage;
strPage=request.getParameter("page");
if (strPage==null){
currPage=1;
}
else{
currPage=Integer.parseInt(strPage);
if (currPage<1) currPage=1;
}
pageCount=(rowCount+pageSize-1)/pageSize;
if (currPage>pageCount) currPage=pageCount; int thepage=(currPage-1)*pageSize;
int n=0;
rs.absolute(thepage+1);
while (n<(pageSize)&&!rs
%>
<%rs.close();
rscount.close();
stmt.close();
stmtcount.close();
conn.close();
%> //下面是 第几页等
<form name="sinfo" method="post" action="sbinfo_index.jsp?condition=<%=condition%>&type=<%=type%>" onSubmit="return testform(this)">
第<%=currPage%>页 共<%=pageCount%>页 共<%=rowCount%>条
<%if(currPage>1){%><a href="sbinfo_index.jsp?condition=<%=condition%>&type=<%=type%>">首页</a><%}%>
<%if(currPage>1){%><a href="sbinfo_index.jsp?page=<%=currPage-1%>&condition=<%=condition%>&type=<%=type%>">上一页</a><%}%>
<%if(currPage<pageCount){%><a href="sbinfo_index.jsp?page=<%=currPage+1%>&condition=<%=condition%>&type=<%=type%>">下一页</a><%}%>
<%if(pageCount>1){%><a href="sbinfo_index.jsp?page=<%=pageCount%>&condition=<%=condition%>&type=<%=type%>">尾页</a><%}%>
跳到<input type="text" name="page" size="4" style="font-size:9px">页
<input type="submit" name="submit" size="4" value="GO" style="font-size:9px">
</form>
解决方案 »
- jsp页面添加到数据库,数据库里面显示的全是乱码?急!!!
- 关于jquery和a标签的问题
- flex builder4与myeclipse8.5整合
- 大家讨论一下。。 struts2 拦截器。。
- WEB控件的问题
- 请教个跳转的问题~我是新手
- 在输入http://localhost:8080/manager时,会提示输入由胡明、口令,这是怎么做的?
- 奇怪的问题,acegi验证成功后,无法跳转到制定的页面
- 啊!见鬼了,怎么JSP源文件也会被修改?请加菲猫等高手快来帮我看看!!谢谢!!!!!Help!
- (spring3.0MVC+Hibernate)JS定义form的action问题
- 查得数据后,怎么么分页显示呢?有解者分全完了,纯对给分!
- 请问怎样使用JSP技术做分页显示?
String sDBDriver = \"COM.ibm.db2.jdbc.app.DB2Driver\";
String sConnStr = \"jdbc:db2:faq\";
Connection conn = null;
Statement stmt = null;
ResultSet rs=null;
try {
Class.forName(sDBDriver);
}
catch(java.lang.ClassNotFoundException e) {
out.print(\"faq(): \" + e.getMessage());
} try{
conn = DriverManager.getConnection(sConnStr,\"wsdemo\",\"wsdemo1\");
stmt = conn.createStatement();
}catch(SQLException e){
out.print(e.toString());
}
%> query.jsp <%@ page language=\"java\" import=\"java.sql.*\" %>
<%@ page contentType=\"text/html; charset=gb2312\" %>
<%@ include file=\"conn.jsp\" %>
<%
......
int pages=0;
int pagesize=10;
ResultSet result = null;
ResultSet rcount = null; pages = new Integer(request.getParameter(\"pages\")).intValue(); if (pages>0)
{ String sql=\" state=\'我不傻\'\";
int count=0;
try {
rcount = stmt.executeQuery(\"SELECT count(id) as id from user where \"+sql);
catch(SQLException ex) {
out.print(\"aq.executeQuery: \" + ex.getMessage());
}
if(rcount.next())
count = rcount.getInt(\"id\");
rcount.close();
if (count>0)
{
sql=\"select * from user where \"+sql;
try {
result = stmt.executeQuery(sql);
}
catch(SQLException ex) {
out.print(\"aq.executeQuery: \" + ex.getMessage());
}
int i;
String name;
// result.first();
// result.absolute((pages-1)*pagesize);
// 此方法jdbc2.0支持。编译通过,但执行不过,不知是不是跟驱动有关,只好用下面的笨办法。
for(i=1;i<=(pages-1)*pagesize;i++)
result.next();
for(i=1;i<=pagesize;i++) {
if (result.next()) {
name=result.getString(\"name\");
out.print(name);
}
result.close();
int n= (int)(count/pagesize);
if (n*pagesize<count) n++;
if (n>1)
{for(i=1;i<=n;i++)
out.print(\"<a href=query.jsp?pages=\"+i+\">\"+i+\" </a>\"); } } } %>
<%@ page language="java" import="java.sql.*" %>
<jsp:useBean id="workM" scope="page" class="test.faq" />
<jsp:useBean id='clock' scope='page' class='dates.JspCalendar' type="dates.JspCalendar" />
<%!
public String getStr(String str)
{
try
{
String temp_p=str;
byte[] temp_t=temp_p.getBytes("GBK");
String temp=new String(temp_t,"ISO8859_1");
return temp;
}
catch(Exception e)
{
}
return "null";
}
%>
<%!
public static String category(int id)
{
try
{
switch(id)
{
case 1:
return "计算机类";
//break;
case 2:
return "英语类";
//break;
default:
return "其他类";
//break;
}
}
catch(Exception e)
{
}
return "null";
}
%>
<TABLE border=0 cellPadding=0 cellSpacing=0 width=760 align="center">
<TBODY>
<tr><td align="left" height=25><%if(session.getAttribute("username")!=null){out.println(session.getAttribute("username"));}%> 当前位置:<a href="../index.jsp">首页</a> -> 缺货登记 </td>
<%@ include file="../member/date.inc"%>
</tr>
<TR bgColor=#3399ff>
<TD height=1 colspan="2"><IMG height=1 src="images/spacer.gif"
width=16></TD></TR>
<tr><td height=10 colspan="2"><IMG height=1 src="images/spacer.gif"
width=16></td></tr>
</TBODY></TABLE> <table align="center" border="0" width="760" cellspacing="0" cellpadding="0" height="355">
<tr>
<td width="150" height="355" valign="top">
</td>
<td width="10" height="100%"></td>
<td width="1" height="100%" bgcolor="#3399ff"></td>
<td width="10" height="100%"></td>
<td width="589" height="331" valign="top" background="images/bg1.gif">
<table border="0" width="100%" cellspacing="0" cellpadding="0">
<tr>
<td width="62%" height="20" bgcolor="#3399ff"> <font color="#ffffff">标题</font>
</td>
<td width="15%" height="20" bgcolor="#3399ff"> <font color="#ffffff">作者</font>
</td>
<td width="15%" height="20" bgcolor="#3399ff"> <font color="#ffffff">类别</font>
</td>
<td width="8%" height="20" bgcolor="#3399ff"> <font color="#ffffff">缺货</font>
</td>
</tr>
<ul>
<%///////////////算出共多少页
int t;
int mtotal;
t=0;
String strSQLsize="SELECT id FROM book where book_number<=0";
ResultSet RSsize = workM.executeQuery(strSQLsize);
while(RSsize.next()){
t=t+1;
}
//out.println(t/3);
//out.println(t%3>0);
//如果纪录总数除以每页的显示个数,余数大于0,那么
//逻辑页数应该为商+1
if((t%3)>0){
mtotal=t/3+1;
}else mtotal=t/3;
// out.println(mtotal);
%>
<%!String pageNo, mTmp;
int i, j, k;
%>
<%
pageNo = request.getParameter("pageNo");
//out.println(pageNo);=========null
if(pageNo == null){
pageNo = "1";
}
j = Integer.parseInt(pageNo);
// out.println(pageNo);=========1
// out.println(j);==========1
if(j < 1)
j = 1;
if(j > mtotal)
j = mtotal;
//out.println(j);
%>
<%
String strSQL="SELECT * FROM book where book_number<=0 order by id desc";
ResultSet RSa = workM.executeQuery(strSQL);
for(k = 0;k < (j-1)*3;k++)
{ //out.println(RSa.next()); RSa.next();
} i = 0;
k = 1;
while (RSa.next()) {
//out.println("ok"+i+"ok");
i = i + 1;
//超过3条
if(i == 4)
{
k = 0;
break;
} out.print("<tr height='23'><td><li><a href=javascript:show('book.jsp?id="+RSa.getInt("id")+"','详细资料',480,500)");
out.print(">"+getStr(RSa.getString("name"))+"</a></td><td>"+getStr(RSa.getString("author"))+"</td><td>"+(category(RSa.getInt("category")))+"</td><td>缺货</td></tr>");
}
i = i - k;
//out.println("i de zhi::"+i);
RSa.close(); %>
<%////////////////////////////////////////////////
if(j > 1)
{
%>
<a href="short_goods.jsp?pageNo=1">第一页</a>
<%
int ii = Integer.parseInt(pageNo,10);
// out.println(ii);
if(ii > 1)
ii = ii -1;
String ssTmp = Integer.toString(ii);
%>
<a href="short_goods.jsp?pageNo=<%=ssTmp%>">上一页</a>
<%
}
if(j < mtotal)
{
int ii = Integer.parseInt(pageNo,10);
if(ii < mtotal)
ii = ii + 1;
String ssTmp = Integer.toString(ii);
%>
<a href="short_goods.jsp?pageNo=<%=ssTmp%>">下一页</a>
<a href="short_goods.jsp?pageNo=<%=mtotal%>">最后页</a>
<%
}
if(mtotal < j)
j = mtotal;
%><%////////////////////////////////////////////%> <tr><td height="1" bgcolor="" colspan="2">
<hr color="#3399ff">
结果共<%=mtotal%>页,显示第<%=j%>页</td>
<td align="left" colspan="2" height="32">
<a href="post.jsp">发布书的信息</a>
</td>
</tr>
</table> </form>
商品分类:<a href="category.jsp?id=1">计算机类</a> |
<a href="category.jsp?id=2">英语类 </a> |
<a href="category.jsp?id=3">其他类 </a> | </td>
</tr>
</table>
<%@ include file="footer.inc"%>
<%@ page contentType="text/html;charset=gb2312"%><% //变量声明 java.sql.Connection sqlCon; //数据库连接对象 java.sql.Statement sqlStmt; //SQL语句对象 java.sql.ResultSet sqlRst; //结果集对象 java.lang.String strCon; //数据库连接字符串 java.lang.String strSQL; //SQL语句 int intPageSize; //一页显示的记录数 int intRowCount; //记录总数 int intPageCount; //总页数 int intPage; //待显示页码 java.lang.String strPage; int i,j,k; //设置一页显示的记录数 intPageSize = 5; //取得待显示页码 strPage = request.getParameter("page");java.lang.String Tjip;//设置跳转页数int intTjip;Tjip=request.getParameter("tjip");if(Tjip==null){
intTjip=1;
out.print("读出来空的");
}
else{
intTjip=java.lang.Integer.parseInt(Tjip);
if(intTjip<1){ intTjip=1; out.print("定位到1"); }
else out.print("不应该定位到1");
}if(strPage==null){//表明在QueryString中没有page这一个参数,此时显示第一页数据 intPage = 1; } else{//将字符串转换成整型 intPage = java.lang.Integer.parseInt(strPage);
if(intPage<1) intPage = 1;}//装载JDBC-ODBC驱动程序 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//设置数据库连接字符串 strCon = "jdbc:odbc:HairDb"; //连接数据库 sqlCon = java.sql.DriverManager.getConnection(strCon,"","");//创建SQL语句对象 sqlStmt = sqlCon.createStatement(); //获取记录总数 strSQL = "select count(*) from orders"; sqlRst = sqlStmt.executeQuery(strSQL); //执行SQL语句并取得结果集 sqlRst.next(); //记录集刚打开的时候,指针位于第一条记录之前 intRowCount = sqlRst.getInt(1); sqlRst.close(); //关闭结果集 //记算总页数 intPageCount = (intRowCount+intPageSize-1) / intPageSize; //调整待显示的页码 if(intPage>intPageCount) intPage = intPageCount; //设置获取数据SQL语句 strSQL = "select hairType,corNum from orders"; //执行SQL语句并取得结果集 sqlRst = sqlStmt.executeQuery(strSQL);//将记录指针定位到待显示页的第一条记录上 i = (intPage-1) * intPageSize; for(j=0;j<i;j++) sqlRst.next(); %> <html> <head><title>分页显示 - JDBC-ODBC</title></head> <body>
<FORM name="PageForm"
METHOD=POST ACTION=""><p align=center>分页测试</p> <table border="1" cellspacing="0" cellpadding="0" width=600 align=center> <% //显示数据 i = 0; while(i<intPageSize && sqlRst.next()){ %> <tr> <td>名称:<%=sqlRst.getString(1)%></td><td>规格:<%=sqlRst.getString(2)%></td></tr>
<% i++; } %><tr> <td colspan=2 align=center> 第<%=intPage%>页 共<%=intPageCount%>页
<%if(intPage>1){%>
<a href="testfySimple.jsp?page=1">首页</a>
<%}%><%if(intPage<intPageCount){%><a href="testfySimple.jsp?page=<%=intPage+1%>">下一页</a><%}%> <%if(intPage>1){%><a href="testfySimple.jsp?page=<%=intPage-1%>">上一页</a><%}%>
<%if(intPage<intPageCount){%><a href="testfySimple.jsp?page=<%=intPageCount%>">尾页</a><%}%></td> </tr></table>
</FORM>
</body> </html> <% //关闭结果集 sqlRst.close(); //关闭SQL语句对象 sqlStmt.close(); //关闭数据库sqlCon.close();%>
jsp+javaBeanhttp://www.52free.com/bbs/index.php?act=ST&f=8&t=148&st=0#entry298如果数据库不同只需修改javaBean中的驱动即可,页面显示数可以自己控制。