简洁Java JDBC 分页查询一个JSP搞定

<%@page import="java.io.IOException"%>
<%@page import="java.util.Properties"%>
<%@page import="res.HttpClientVisitTest"%>
<%@page import="java.io.InputStream"%>
<%@ page contentType="text/html; charset=utf-8" %> <%-- 设置页面编码格式 --%>
<%@ page import="java.sql.*" %> <%-- 导入 SQL 包 --%>
<html>
<head>
<title>分页显示</title>
<style type="text/css">
tab {
border: 1px dotted #aaa;
}
td {
border-top: 1px dotted #aaa;
}
</style>
</head>
<body>
<center>
<h1>服务器请求信息列表</h1>
<hr />
<br />
<%
request.setCharacterEncoding("utf-8"); // 统一编码格式为 GBK
%>
<%!
final String jspUrl = "fenye.jsp";
%>
<%
// 真分页:只是取出需要的数据并显示在页面上,是从程序的分离的角度划分的,分散了程序的负载,提升了程序的整体性能,性能较高。
// 分页实现由业务程序和数据库分页函数协调配合共同完成。
// 定义如下分页变量
int lineSize = 10; // 定义每页要显示的记录数
int currentPage = 1; // 定义当前是第几页
int pageSize = 0; // 总页数 = 总记录数 ÷ 每页显示的记录数
int allRecorders = 27;
String keyWord = null; // 查询关键字
%>
<%
keyWord = request.getParameter("kw"); // 接收用户输入的关键字
// 接收传过来的当前页
try
{
currentPage = Integer.parseInt(request.getParameter("cp"));
}
catch(Exception e){}
%>
<%
//本机服务器连接
//final String DBDRIVER = "com.mysql.jdbc.Driver"; // MySQL 驱动类
//final String DBURL = "jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf-8"; // 数据库地址
//final String DBUSER = "root"; // 连接数据库的用户名
//final String DBPASSWORD = "root"; // 连接数据库的密码

//服务器数据库
final String DBDRIVER = "com.mysql.jdbc.Driver"; // MySQL 驱动类
Properties prop = null;
InputStream is = HttpClientVisitTest.class
.getResourceAsStream("//config//config.properties");
prop = new Properties();
try {
prop.load(is);
} catch (IOException e) {
e.printStackTrace();
}
//final String DBURL = "jdbc:mysql://218.206.179.114:3306/res?useUnicode=true&characterEncoding=utf-8"; // 数据库地址
final String DBURL = prop.getProperty("url"); // 数据库地址
final String DBUSER = prop.getProperty("username"); // 连接数据库的用户名
final String DBPASSWORD = prop.getProperty("password"); // 连接数据库的密码
Connection conn = null; // 建立与数据库连接的对象
%>
<%
try
{
Class.forName(DBDRIVER); // 注册 MySQL 驱动
conn = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD); // URL地址,注册用户名,用户名密码
PreparedStatement pstmt = null; // 预处理
String sql = null;
if(keyWord == null || "".equals(keyWord))
// if(true)
{
// 如果为空,则没有查询,表示查询全部
sql = "SELECT COUNT(id) FROM res"; // 构建 SQL 语句s
}
else
{
sql = "SELECT COUNT(id) FROM res WHERE userid LIKE ? OR phone LIKE ?"; // 构建 SQL 语句
}
pstmt = conn.prepareStatement(sql) ; // 加载 SQL 语句到
// 如果有查询关键字,则进行设置
if(!(keyWord == null || "".equals(keyWord)))
{
pstmt.setString(1, "%" + keyWord + "%");
pstmt.setString(2, "%" + keyWord + "%");
}
ResultSet rs = pstmt.executeQuery(); // 执行 SQL 语句
if(rs.next())
{
allRecorders = rs.getInt(1); // 取得表中总记录数
}
rs.close(); // 关闭结果集
pstmt.close(); // 关闭预处理

pageSize = (allRecorders + lineSize - 1) / lineSize; // 计算总页数

if(keyWord == null || "".equals(keyWord))
//if(true)
{
// // 如果为空,则没有查询,表示查询全部
sql = "SELECT id,sessionid, userid, phone,email,status,resTime FROM res limit " + (currentPage - 1) * lineSize + "," + lineSize;
}
else
{
sql = "SELECT id,sessionid, userid, phone,email,status,resTime FROM res WHERE userid LIKE ? OR phone LIKE ? LIMIT " + (currentPage - 1) * lineSize + "," + lineSize;
}
pstmt = conn.prepareStatement(sql) ; // 加载 SQL 语句到
// 如果有查询关键字,则进行设置
if(!(keyWord == null || "".equals(keyWord)))
{
pstmt.setString(1, "%" + keyWord + "%");
pstmt.setString(2, "%" + keyWord + "%");
}
rs = pstmt.executeQuery(); // 执行 SQL 语句
%>
<script language="javaScript">
function openPage(curPage)
{
document.spage.cp.value = curPage ;
// alert(cupage) ;
document.spage.submit() ;
}
function selOpenPage()
{
document.spage.cp.value = document.spage.selpage.value;
document.spage.submit();
}
</script>
<form name="spage" action="<%= jspUrl %>">
输入查询的关键字:<input type="text" name="kw" value="<%= keyWord == null ? "" : keyWord %>" />
<input type="submit" value="查询" />
<br /><br />
<%
if(allRecorders > 0)
{
%>
<input type="button" value="首页" onClick="openPage(1)" <%= currentPage == 1 ? "disabled" : "" %> />
<input type="button" value="上一页" onClick="openPage(<%= currentPage - 1 %>)" <%= currentPage == 1 ? "disabled" : "" %> />
<input type="button" value="下一页" onClick="openPage(<%= currentPage + 1 %>)" <%= currentPage == pageSize ? "disabled" : "" %> />
<input type="button" value="尾页" onClick="openPage(<%= pageSize %>)" <%= currentPage == pageSize ? "disabled" : "" %> />
<input type="hidden" name="cp" value="" />
<font color="red" size="5"><%= currentPage %></font>
/
<font color="red" size="5"><%= pageSize %></font>
跳转到
<select name="selpage" onChange="selOpenPage()">
<%
for(int j = 1; j <= pageSize; j++)
{
%>
<option value="<%= j %>" <%= currentPage == j ? "selected" : "" %>> <%= j %> </option>
<%
}
%>
</select>

<%
}
%>
</form>
<table width="80%" cellpadding="1" cellspacing="0">
<tr align="center">
<th>编号</th>
<th>用户ID</th>
<th>用户名</th>
<th>电话</th>
<th>Email</th>
<th>请求状态</th>
<th>请求时间</th>
<!-- <th colspan="2">操作</th>-->
</tr>
<%
int i = 0; // 判断是否有数据库的标记
while(rs.next())
{
i++;
int id = rs.getInt(1);
String sessionid=rs.getString(2);
if("".equals(sessionid) || null == sessionid){
sessionid="无值";
}
String userid = rs.getString(3);
if("".equals(userid) || null == userid){
userid="无值";
}
String phone = rs.getString(4);
if("".equals(phone) || null == phone){
phone="无值";
}
String email = rs.getString(5);
if("".equals(email) || null == email){
email="无值";
}
String status = rs.getString(6);
if("".equals(status) || null == status){
status="无值";
}
Date resTime = rs.getDate(7);

%>
<tr align="center">
<td><%= id %></td>
<td><%= sessionid %></td>
<td><%= userid %></td>
<td><%= phone %></td>
<td><%= email %></td>
<td><%= status %></td>
<td><%= resTime %></td>
<!-- <td>更新</td>
<td>删除</td> -->
</tr>
<%
}
rs.close(); // 关闭结果集
pstmt.close(); // 关闭预处理
if(i == 0)
{
%>
<tr align="center">
<td colspan="6">不存在数据! </td>
</tr>
<%
}

%>
<table>
<%
}
catch(Exception e)
{
out.println(e);
%>
<h2>程序出错啦!</h2>
<%
}
finally
{
conn.close();
}
%>
</center>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值