<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
Insert title here
| 商品序号 | 商品名称 | 商品价格 | 操作 |
| ${g.gid } | ${g.gname } | ${g.gprice } | 修改 |
<%
//调用业务逻辑层 biz
IGoodsBiz igb=new GoodsBiz();
int pageIndex=1;
int pageSize=2;
//接收pid
String pid=request.getParameter("pid");
if(pid!=null){//说明点了x页
pageIndex=Integer.parseInt(pid);
}
//把pageIndex存起来 方便取
request.setAttribute("pageIndex", pageIndex);
//接收表单提交过来的关键字
String str=request.getParameter("gname");
if(str==null){
str="";//相当于查询全部
}
//把gname存起来
request.setAttribute("gname", str);
//获得最大页码
int max=igb.getMax("goods where gname like '%"+str+"%'", pageSize);
//把max存起来 方便取 ${max}
request.setAttribute("max", max);
//调用查询全部的方法
List ls=igb.getAllByPage(pageIndex, pageSize, str);
if(ls.size()!=0){//集合中有数据
//把集合存起来=request中
request.setAttribute("myls", ls);
//跳转到展示页面进行展示 只能转发
request.getRequestDispatcher("index.jsp").forward(request, response);
}
else{
System.out.print("集合为空-优先检查数据库中是否有数据 DBHelper以及dao/biz");
}
%>
修改界面:update.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
修改方法:doupdate.jsp
<%@page import="com.zking.entity.Goods"%>
<%@page import="com.zking.biz.GoodsBiz"%>
<%@page import="com.zking.biz.IGoodsBiz"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<
<%
//设置编码方式
request.setCharacterEncoding("utf-8");
//out.print(g.getGname());//拿到了说明可以
//调用业务逻辑层
IGoodsBiz igb=new GoodsBiz();
if(igb.updateGoods(g, g.getGid())){
response.sendRedirect("index.jsp");
}
else{
out.print("");
}
%>
调用查询单个的方法判断这个id
<%@page import="com.zking.entity.Goods"%>
<%@page import="com.zking.biz.GoodsBiz"%>
<%@page import="com.zking.biz.IGoodsBiz"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
//接收gid
String gid=request.getParameter("gid");
//调用业务逻辑层
IGoodsBiz igb=new GoodsBiz();
//调用查询单个的方法
Goods goods=igb.getGoodsByGid(gid);
//存request中
request.setAttribute("goods", goods);//${goods.gid}
//只能转发
if(goods!=null){
request.getRequestDispatcher("update.jsp").forward(request, response);
}
else{
System.out.print("对象为空");
}
%>
二、分页功能
分页功能的核心就是我们的sql语句,我们要先排序后编号
sql语句:
select * from(
select a.*,rownum as rid from(
select nid,ntitle,nauthor from tb_fb order by nid desc
)
)
当我们有了sql语句之后,就可以进行下一步,就是将sql语句写入dao方法中
代码块:
public List getPage(int pageIndex,int pageSize){
List ls=new ArrayList();
int a=(pageIndex-1)*pageSize+1;
int b=pageIndex*pageSize;
try {
//创建连接
con=DBHelper.getCon();
//定义sql语句
String sql="select * from (rn" +
" select a.*,rownum as rid from(rn" +
" select nid,ntitle,nauthor from tb_fb order by nid descrn" +
" ) arn" +
") b where b.rid between ? and ?";
//获得执行对象
ps=con.prepareStatement(sql);
//给占位符赋值
ps.setInt(1, a);
ps.setInt(2, b);
//获得结果集
rs=ps.executeQuery();
//循环遍历
while(rs.next()){
//实例化新闻对象
News n=new News();
//给对象赋值
n.setNid(rs.getInt(1));
n.setNtitle(rs.getString(2));
n.setNauthor(rs.getString(3));
//加到集合中
ls.add(n);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBHelper.myClose(con, ps, rs);
}
return ls;
}
模糊查询的分页代码:
public List getPage(int pageIndex,int pageSize,String title){
List ls=new ArrayList();
int a=(pageIndex-1)*pageSize+1;
int b=pageIndex*pageSize;
try {
//创建连接
con=DBHelper.getCon();
//定义sql语句
String sql="select * from (rn" +
" select a.*,rownum as rid from(rn" +
" select nid,ntitle,nauthor from news280 where ntitle like '%"+title+"%' order by nid descrn" +
" ) arn" +
") b where b.rid between ? and ?";
//获得执行对象
ps=con.prepareStatement(sql);
//给占位符赋值
ps.setInt(1, a);
ps.setInt(2, b);
//获得结果集
rs=ps.executeQuery();
//循环遍历
while(rs.next()){
//实例化新闻对象
News n=new News();
//给对象赋值
n.setNid(rs.getInt(1));
n.setNtitle(rs.getString(2));
n.setNauthor(rs.getString(3));
//加到集合中
ls.add(n);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBHelper.myClose(con, ps, rs);
}
return ls;
}
计算最大页码数所需要的dao方法代码块:
public int getRows(String str) {
int n=0;
try {
con=DBHelper.getCon();
String sql="select count(*) from "+str;
ps=con.prepareStatement(sql);
rs=ps.executeQuery();
if(rs.next()) {
n=rs.getInt(1);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.myClose(con, ps, rs);
}
return n;
}
dao方法有了,再就是去我们需要用到的地方,去调用dao方法
代码块:
-
<%
request.setCharacterEncoding("utf-8");
//接收title
String title=request.getParameter("title");
if(title==null){
title="";//相当于查询全部
}
NewsDao nd=new NewsDao();
int pageIndex=1;//当前页面 第几页
int pageSize=5;
//接收pid
String pid=request.getParameter("pid");
if(pid!=null){//说明了点击了上一页或者下一页等
pageIndex=Integer.parseInt(pid);//改变pageIndex的值
}
//计算最大页码
int rows=nd.getRows("news280 where ntitle like '%"+title+"%'");
int max=rows/pageSize;//13/5=2
if(rows%pageSize!=0){//如果除不尽 有余数
max++;//+1
}
//调用查询的方法
List
- "><%=n.getNtitle() %> 作者:<%=n.getNauthor() %> '>修改 ' onclick='return clickdel()'>删除 <% } %>
当前页数:[<%=pageIndex %>/<%=max %>] ">首页 1?pageIndex-1:1%>&title=<%=title%>">上一页 &title=<%=title%>">下一页 &title=<%=title%>">末页



