您的位置:首页 > Web前端 > JavaScript

在jsp中嵌入java代码实现分页代码

2011-11-06 14:33 579 查看
非常适合初学者

首先设置数据库:表名叫good



效果图:







代码部分:

<%@ page language="java" import="java.util.*,java.sql.*"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'PageShow.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body bgcolor=white>
<center>
<table width="600" border="2" cellspacing="1" cellpadding="1">
<tr align="center">
<td height="30" align="center">
<span class="goodtitle"><font size=6 face="幼圆">查询商品列表</font>
</span>
</td>
</tr>
</table>
<table width="600" border="2" cellspacing="0" cellpadding="0"
height="10">
<tr>
<td width="120" height="10" align="center">
商品名称
</td>
<td width="120" height="30" align="center">
商品类型
</td>
<td width="120" height="30" align="center">
商品价格
</td>
<td width="120" height="30" align="center">
库存数量
</td>
<td width="120" height="30" align="center">
制造商
</td>
</tr>
<%
int PageSize = 2;//一页显示几条记录
int RecordCount;//得到数据库里有多少条记录 ,总记录数
int PageCount;//页的总数
int Page = 1;//显示第几页
int i;
String SPage = request.getParameter("page");
if (SPage == null) {
Page = 1;
} else {
Page = java.lang.Integer.parseInt(SPage);
if (Page < 1)
Page = 1;
}
String sql = "select id,name,model,price,number,maker from good";
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager
.getConnection("jdbc:mysql://localhost:3306/3g?user=root&password=123&useUnicode=true&characterEncoding=UTF-8");
PreparedStatement pstmt = con.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery(sql);

rs.last();
// 将光标移动到此 ResultSet 对象的最后一行。
RecordCount = rs.getRow();//getRow() 获取当前行编号。

PageCount = (int) (RecordCount + PageSize - 1) / PageSize;
if (Page > PageCount)
Page = PageCount;
if (PageCount > 0) {
rs.absolute((Page - 1) * PageSize + 1);//absolute(int row) 将光标移动到此 ResultSet 对象的给定行编号。
i = 0;
while (i < PageSize && !rs.isAfterLast()) {//isAfterLast() 获取光标是否位于此 ResultSet 对象的最后一行之后
%>
<tr>
<td width="120" height="10" align="center"><%=rs.getString("name")%></td>
<td width="120" height="10" align="center"><%=rs.getString("model")%></td>
<td width="120" height="10" align="center"><%=rs.getFloat("price")%></td>
<td width="120" height="10" align="center"><%=rs.getInt("number")%></td>
<td width="120" height="10" align="center"><%=rs.getString("maker")%></td>
</tr>
<%
rs.next();
i++;
}
}
%>
</table>
<hr>
<h5>
<div align="center">
第<%=Page%>页 共<%=PageCount%>页
<%
if (Page < PageCount) {
%>
<a href="PageShow.jsp?page=<%=Page + 1%>">下一页</a>
<%
}
if (Page > 1) {
%>
<a href="PageShow.jsp?page=<%=Page - 1%>">上一页</a>
<%
}
%>

</div>
</h5>
</center>
</body>

</html>


是不是挺垃圾的?呵呵!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息