java程序片
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="java.sql.*"%>
<%
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>学生列表</title>
</head>
<body>
<form action="list.jsp" method="post"> <!-- post方式来进行跳转 避免乱码-->
<center>
姓名的查询:
<input type="text" name="searchName" />
<input type="submit" value="查询" />
</center>
<table align="center" border="1">
<tr>
<td>
id
</td>
<td>
name
</td>
<td>
sex
</td>
<td>
age
</td>
<td>
birthday
</td>
<td>
操作
</td>
</tr>
<%
//top
//limit
//对于分页:
//1.基于SQL语句 通过点击控件完成参数的改变
//2.通过控件去修改当前页的编号
//3.获得跳转后修改的页面编号
//1.对于搜索 select * from stu where name like '%他他他%' limit 0,3; 第一条记录的索引值为0;
//2.模糊查询name,查询带分页
//用户第一次进来 依然显示所有的数据
//3.保证翻页后查询的条件依然存在
//4.在查询之后 将值保存在session中
//查询后翻页每次的条件从session中取得
request.setCharacterEncoding("UTF-8");
String searchName = request.getParameter("searchName");
//判断第一次进入
System.out.println("searchName:"+searchName);
if (session.getAttribute("searchName") == null&& searchName == null) {
searchName = "";
}
//查询后找寻的值从session中取得 当点击下一页或者上一页的时候,查询的语句不能改变的
if (session.getAttribute("searchName") != null
&& searchName == null) {
searchName = session.getAttribute("searchName").toString();
}
int pageNo = 1; //当前页编号
int pageSize = 3;//每页显示的数据
int totalPage = 0;//最大页数
String str_pageNo = request.getParameter("pageNo");//获取上一次页面传过来的值
//System.out.println("4444444444");
if (str_pageNo == null) { //如果是第一次登录时,pageNo就没传过来,就为null值
str_pageNo = "1"; //这时显示就为第一页
}
//将修改后的值赋给pageNo
pageNo = Integer.parseInt(str_pageNo);
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/wepull", "root", "aaa");
//获得预编译SQL对象 根据当前页数 和每页显示的条数定义分页起始位置
String sql = "select * from stu limit " + (pageNo - 1) * pageSize
+ "," + pageSize + "";
String countSQL = "select count(*) from stu";
//当用户有提交查询内容时 值存入session 并改变SQL语句
if (searchName != null) { //当输入了查询的内容时
session.setAttribute("searchName", searchName);//!将放入内容查询的session中,当session中没值的时候,用get取的时候为null值
sql = "select * from stu where name like '%" + searchName
+ "%' limit " + (pageNo - 1) * pageSize + ","
+ pageSize + "";
countSQL = "select count(*) from stu where name like '%"
+ searchName + "%'";//‘’号里面引用双引号
}
System.out.println(sql);
System.out.println(countSQL);
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
%>
<tr>
<td><%=rs.getInt("id")%></td>
<td><%=rs.getString("name")%></td>
<td><%=rs.getString("sex")%></td>
<td><%=rs.getString("age")%></td>
<td><%=rs.getString("birthday")%></td>
<td>
<a href="toEdit.jsp?id=<%=rs.getInt("id")%>">编辑</a>
<a href="delete.jsp?id=<%=rs.getInt("id")%>">删除</a>
</td>
</tr>
<%
}
//获得最大的条数
ps = conn.prepareStatement(countSQL);
rs = ps.executeQuery();
if (rs.next()) {
int totalCount = rs.getInt(1); //得到总的条数
//判断整除和非整除
if (totalCount % pageSize == 0) {
totalPage = totalCount / pageSize; //整除的时候
System.out.println(totalPage);
} else {
totalPage = totalCount / pageSize + 1; //当不能整除时,将页面数+1
}
}
conn.close(); //关闭连接
%>
<tr>
<td colspan="7">
<input type="button" value="添加"
οnclick="window.location.href='toAdd.jsp'" />
<a href="list.jsp">首页</a>
<%--当是第一页时,不能点击上一页,默认的为第1页 --%>
<a href="list.jsp?pageNo=<%=pageNo - 1%>" <%if(pageNo<=1){%>
disabled οnclick="return false;" <%}%>>上一页</a>
<a href="list.jsp?pageNo=<%=pageNo + 1%>"
<%if(pageNo>=totalPage){%> disabled οnclick="return false;"
<%} %>>下一页</a>
<a href="list.jsp?pageNo=<%=totalPage%>">尾页</a>
<!--
1.触发onchange事件
2.在每次修改时 将option的value值传到pageNo作为当前页的数据
-->
<select
οnchange="window.location.href='list.jsp?pageNo='+this.value">
<!--
当当前页和 i值相等时 给予selected属性
-->
<%
for (int i = 1; i < totalPage + 1; i++) {
%>
<option <%if(pageNo==i){ %> selected <% }%> value="<%=i%>"><%=i%></option>
<%
}
%>
</select>
</td>
</tr>
</table>
</form>
<%-- d--%>
</body>
</html>