servlet中部分代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
String ptype1 = request.getParameter("ptype"); int ptype = 10; if (ptype1 != null) { ptype = Integer.parseInt(request.getParameter("ptype")); } String cid1 = request.getParameter("cid"); int cid = 10; if (cid1 != null) { cid = Integer.parseInt(cid1); } ArrayList<Goods> goodsList; try { goodsList = GoodsDAO.getGoodsList(currentpageno, ptype,cid); request.setAttribute("goodsList", goodsList); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } request.getRequestDispatcher("/index.jsp").forward(request, response); } |
dao中部分代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
boolean valid=false; //判断参数为空? StringBuffer sb =new StringBuffer("select * from goods where 1=1"); if(cid != 10) { sb.append(" and gtype="+cid); valid=true; } if(ptype != 10) { sb.append(" and ptype="+ptype); valid=true; } if(cid == 10&&ptype == 10) { sb.append(""); valid=true; } if(valid){ String s = sb.toString(); System.out.println(s); rs = db.executeQuery(s); try { while (rs.next()) { Goods goods = new Goods(); goods.setGid(rs.getInt("gid")); goods.setGtitle(rs.getString("gtitle")); goods.setGdetail(rs.getString("gdetail")); goods.setGpic(rs.getString("gpic")); goods.setGtype(rs.getInt("gtype")); goods.setGtag(rs.getString("gtag")); goods.setGprice(rs.getString("gprice")); goods.setPuser(rs.getString("puser")); goods.setPtype(rs.getInt("ptype")); goods.setPdate(rs.getDate("pdate").toString()); //System.out.println(rs.getString("gpic")); goodsList.add(goods); } } catch (SQLException e) { e.printStackTrace(); } return goodsList; |