求jsp分页代码

justsosos 2007-09-09 12:20:11
请附能用的代码和调用方式。本人jsp初学者,希望直接放到jsp或者include能用的,如果是servlet什么的我就不会调用了,不知道放哪该不该编译等等。
万分感谢!
...全文
1081 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
yangwxh520 2008-09-22
  • 打赏
  • 举报
回复
jiushi
chihaifeng 2008-05-30
  • 打赏
  • 举报
回复
别说盗版,我看只是相互学习!
dr_lou 2007-09-09
  • 打赏
  • 举报
回复
懒人越来越多,怪不得盗版多
jdl2000 2007-09-09
  • 打赏
  • 举报
回复
对了,强烈建议使用ECLIPSE,和MyEclipse查件,可以节省很多时间和不必要的配置问题,别忘记导入数据库包CLASS12
jdl2000 2007-09-09
  • 打赏
  • 举报
回复
暂时就给你这个吧,只是很简单的基于缓存技术写的,没有使用SERELT等,希望好好自学,估计初学者,多多加油吧!已经详细注释,应该能看的懂

package java.ss
import java.sql.*;

public class CachedPageBean {
static String serverName = "localhost";
static String sDBDriver = "oracle.jdbc.driver.OracleDriver";
static String dbInstance = "nitpro";
static String sConnStr = "jdbc:oracle:thin:@"
+serverName+":1521:"+dbInstance;

static String dbUser = "system";
static String userPwd = "manager";

/**
*得到一个Connection对象
*@return java.sql.Connection
*/
public static Connection getConnection()
{
Connection conn = null;
try
{
Class.forName(sDBDriver);
conn = DriverManager.getConnection(sConnStr,
dbUser, userPwd);
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
catch(SQLException e1)
{
e1.printStackTrace();
}
return conn;
}
/**
*关闭指定的结果集
*@param rs 要关闭的结果集
*/
public static void closeResultSet(ResultSet rs)
{
if( rs != null)
{
try
{
rs.close();
}
catch(SQLException e)
{
}
}
}
/**
*关闭指定的Statement
*@param stmt 要关闭的Statement
*/
public static void closeStatement(Statement stmt)
{
if( stmt != null)
{
try
{
stmt.close();
}
catch(SQLException e)
{
}
}
}
/**
*关闭连接
*@param conn 要关闭的连接
*/
public static void closeConnection(Connection conn)
{
if( conn != null)
{
try
{
conn.close();
}
catch(SQLException e)
{
}
}
}
/**
获得总的记录数目
*/
public static int getRowNumber()
{
Connection conn = getConnection();
int num = 0;
try
{
Statement stmt = conn.createStatement();
String sql = "select count(*) as rowNumbers from student";
ResultSet rs = stmt.executeQuery(sql);
rs.next();
num = rs.getInt("rowNumbers");
}
catch(SQLException e)
{
e.printStackTrace();
}
finally
{
closeConnection(conn);
}
return num;
}
/**
根据指定的页面大小,获得页面数目
*/
public static int getTotalPage(int pageSize)
{
int totalPage = 1;
int tmpPage = 0;
int rowNum = getRowNumber();
tmpPage = rowNum % pageSize;
if(tmpPage == 0)
{
totalPage = rowNum / pageSize;
}else{
totalPage = (int) (Math.floor(rowNum/pageSize)+1);
}
if (totalPage == 0)
{
totalPage = 1;
}

return totalPage;
}
public static ResultSet getAllResults() {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String sql = "select * from student order by id";
try {
conn = getConnection();
//注意,返回的是可滚动的ResultSet
stmt = conn.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
}
catch(SQLException e) {
e.printStackTrace();
}
return rs;
}
}
JSP页面部分
<%@ page contentType="text/html;charset=GBK" %>
<%@ page import="java.sql.*,java.ss.CachedPageBean"%>
<%!
ResultSet rs = null;
//页面大小
int pageSize = 5;
//第几页
int pages = 1;
//总页数
int totalPage = 0;

String str = "";

public String showOnePage(ResultSet rs, int pages, int pageSize) {
str = "";
// 将记录指针定位到相应的位置
try {
rs.absolute( (pages-1) * pageSize + 1);
}catch(SQLException e) {
}

for(int i=1; i<=pageSize; i++) {
str += displayOneResult(rs);
try {
if(!rs.next()) break;
}catch(Exception e) {
e.printStackTrace();
}
}
return str;
}

// 显示单行记录方法
public String displayOneResult( ResultSet rs ) {
String text= "";
try {
text += "<tr>";
text += "<td>" + rs.getString("id") + "</td>";
text += "<td>" + rs.getString("name") + "</td>";
text += "<td>" + rs.getString("course") + "</td>";
text += "</tr>";
}catch(Exception e) {
e.printStackTrace();
}
return text;
}
%>

<%
try {
rs = CachedPageBean.getAllResults();
}catch(Exception e) {
out.println("访问数据库出错!");
}
%>
<html>

<head>
<title>分页浏览</title>
</head>

<body bgcolor="#FFFFFF">

<h2 align="center">分页显示</h2>

<hr>
<center>
<table border>
<tr bgcolor=lightblue>
<th>学生编号</th>
<th>学生姓名</th>
<th>课程</th>
</tr>
<%
totalPage = CachedPageBean.getTotalPage(5);


try {
if(request.getParameter("Page")==null ||
request.getParameter("Page").equals(""))
pages = 1;
else
pages = Integer.parseInt(request.getParameter("Page"));
} catch(java.lang.NumberFormatException e) {
// 处理用户从浏览器地址拦直接输入pages=ab等所造成的异常
pages = 1;
}

if(pages < 1) pages = 1;
if(pages > totalPage) pages = totalPage;

out.println(showOnePage(rs, pages, pageSize));
%>
</table>
<form Action="cachePages.jsp" method="get">
<%
if(pages != 1) {
out.println("<a href= cachePages.jsp?Page=1>第一页</A>");
out.println("<a href= cachePages.jsp?Page="
+(pages-1)+">上一页</A>");
}
if(pages != totalPage) {
out.println("<a href= cachePages.jsp?Page="
+ (pages+1) + ">下一页</A>");
out.println("<a href= cachePages.jsp?Page="
+ totalPage + ">最后一页</A>");
}
rs.close();
%>
<p>输入页数:
<input type="text" name="Page" size="3" value="<%=pages%>">
<input type="submit" value="翻页">
页数:<font color="red"><%=pages%>/<%=totalPage%></font>
</p>
</form>
</center>
<hr>
</body>
</html>


bat_net 2007-09-09
  • 打赏
  • 举报
回复
http://www.blogjava.net/blackbat
这里有一篇
joejoe1991 2007-09-09
  • 打赏
  • 举报
回复
你如果会写 查询表里的第10到第20条数据的sql语句
你就会写分页了

自己baidu一下吧

81,114

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧