一.封装dao方法 通用查询
/**
* 通用的曾删改
*
* @param sql
* @param objct
* @return
*/
public int executeUpdate(String sql, Object... objct) {
int n=0;
conn=DBHelper.getConn();
try {
ps=conn.prepareStatement(sql);
for (int i = 0; i < objct.length; i++) {
ps.setObject(i+1, objct[i]);
}
n=ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DBHelper.myclose(conn, ps, rs);
}
return n;
}
/**
* 通用查询方法
* @param sql
* @param objct
* @return
*/
public ResultSet executeQuery(String sql, Object... objct) {
conn=DBHelper.getConn();
try {
ps=conn.prepareStatement(sql);
for (int i = 0; i < objct.length; i++) {
ps.setObject(i+1, objct[i]);
}
rs=ps.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
二分页sql语句定义oracle 语句dao 方法
@Override
public List<Goods> getGoodsAll(Integer pagMax, Integer pagindex, String strName, Object... objects) {
Integer pagcount = 4;// 默认每页显示4条
if (objects.length != 0) {
try {// 如果传入字符串 报错 直接改为默认4条
pagcount = (Integer) objects[0];
} catch (Exception e) {
pagcount = 4;
}
}
Integer Maxindex = pagMax % pagcount == 0 ? pagMax / pagcount : pagMax / pagcount + 1;
// 判断如果分页 页数大于了总分页条数 直接返回 不进数据库
if (pagindex > Maxindex) {
return null;
}
Integer start = (pagindex - 1) * pagcount + 1;
Integer end = pagcount * pagindex;
String sql = "select gid,gname,gtype,gimage,gprice,gkc,ginfo from \r\n"
+ "(select a.gid,a.gname,a.gtype,a.gimage,a.gprice,a.gkc,a.ginfo,rownum as rid from \r\n"
+ "(select gid,gname,gtype,gimage,gprice,gkc,ginfo from tb_goods where gname like '%" + strName
+ "%' or ginfo like '%" + strName + "%')a\r\n" + " )b where b.rid between " + start + " and " + end
+ "";
ResultSet rs = this.executeQuery(sql);
List<Goods> list = new ArrayList<Goods>();
try {
while (rs.next()) {
list.add(new Goods(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getInt(5),
rs.getInt(6), rs.getString(7)));
}
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
} finally {
//数据库 工具类
DBHelper.myclose(conn, ps, rs);
}
return list;
}
三 Servlet界面获取数据代码 分页与提示共用界面 以及加载主页数据
String parameter = request.getParameter("zt");
IGoodsBiz goods=new GoodsBizImpl();
//上方提示框制作 提示框 需要用到包含分类 商品名 与介绍 进行排分 提示框 后面跟着名字或者介绍
String str=request.getParameter("strname1");
if(str!=null) {
List<Goods> goosdao =null;
goosdao = goods.getvagueGoods(str);
// System.out.println(goosdao.size());
//计算搜索框条数
Integer index=0;
for (Goods goods2 : goosdao) {
if(goods2.getGname().contains(str)) {
index+=1;
}
if(goods2.getGinfo().contains(str)) {
index+=1;
}
}
Map< String , Object> map=new HashMap<String, Object>();
map.put("goodslist", goosdao);
map.put("index", index);
//json 转换对象
ObjectMapper oMapper=new ObjectMapper();
String writeValueAsString = oMapper.writeValueAsString(map);
//返回给客户端
PrintWriter out = response.getWriter();
out.write(writeValueAsString);
out.flush();
out.close();
//如果运行的是提示搜索 则后面代码不运行
return;
}else {
//获取当前页数
String indexs=request.getParameter("index");
Integer index=1;
if(indexs!=null) {
index=Integer.parseInt(indexs);
}
//获取模糊查询字段
String strName=request.getParameter("strName");
if(strName==null) {
strName="";
}
List<Goods> getvagueGoods = goods.getvagueGoods(strName);
//计算最大页数
Integer pagSum=4;
//Integer pagMax=getvagueGoods.size()%pagSum==0?getvagueGoods.size()/pagSum:getvagueGoods.size()/pagSum+1;
List<Goods> goodsAll = goods.getGoodsAll(getvagueGoods.size(), index, strName,pagSum);
//创建map集合保存 数据
Map<String, Object> map=new HashMap<String, Object>();
map.put("pagSum", pagSum);
map.put("pagMax", getvagueGoods.size());
map.put("goodsAll",goodsAll);
map.put("index",index);
//json 转换对象
ObjectMapper oMapper=new ObjectMapper();
String writeValueAsString = oMapper.writeValueAsString(map);
//返回给客户端
PrintWriter out = response.getWriter();
out.write(writeValueAsString);
out.flush();
out.close();
}
}
}