标签:
1.目标的 分析,首先 用户可以选择登录和注册 ------登录就看到所有的 商品的信息-----可以对商品进行 删除 和 更改和 增加
》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
2. 由此可以 确定出 的 表 有两张,所有 在 sql中建立表,对应的javabean 就是如下
<span style="font-size:18px;"><span style="background-color: rgb(51, 204, 0); ">public class UserInfo { private int id; private String uname; private String upwd; private String marker;}// 这是备注</span></span>
<strong><span style="font-size:18px;background-color: rgb(255, 204, 0); ">public class Product { private int id; private String name; private String unit;// 货物的单位 private double price; private int count; 数量}</span></strong>》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
3.确定了 表 我们就 开始写 与数据库向联系的 DBM类,相比以前就是 把 Resultset 转化为 Result ,
public Result GetResult(String sql,Object ...objs) throws SQLException{ Connection sqlcon=this.getConnection(); PreparedStatement pst=sqlcon.prepareStatement(sql);// <span style="font-size:24px;background-color: rgb(51, 204, 0); "><strong>先得到的 pst,pst 的 意识就是 含有 已经编译的 具有 输入参数的SQL语句,用了?代替占位符</strong></span> <strong>if(objs!=null){ if(objs.length>0){ int idx=1; for(Object obj : objs){ pst.setObject(idx, obj); idx++; } } }</strong>
<strong> ResultSet rst = pst.executeQuery(); Result rs=ResultSupport.toResult(rst); <span style="font-size:24px;">这边 还有一些 需要关闭的的 </span></strong>
<strong> return rs;}</strong>
<span style="font-size:18px;"><strong>int iline=pst.executeUpdate(); //释放服务器数据库资源 pst.close(); sqlcon.close(); 其他的 没有很大的 不同 return iline; </strong></span>》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
4.然后就是 编写 需要用的 的 接口,
public interface IUserInfoDao { * 实现用户登录 public UserInfo MyLogin(String uname,String upwd) throws SQLException; * 注册操作 public int Register(UserInfo entity) throws SQLException;其次是 实现 这个 接口
<span style="font-size:18px;">public class UserInfoDaoImpl implements IUserInfoDao { DBManager db=new DBManager(); public <span style="color:#6666cc;">UserInfo</span> MyLogin(String uname, String upwd) throws SQLException { String sql=<span style="background-color: rgb(51, 255, 51); ">"select * from userinfo where uname=? and upwd=?";</span>//<strong><span style="color:#3333ff;">就是 根据 用户名和密码 把信息全部 查出来,那么 把用户名和 密码 都验证了,</span></strong> Object [] objs={uname,upwd}; Result rs = db.GetResult(sql, objs); <strong><span style="color:#3333ff;">UserInfo entity=null;// 这个 是 写在外面的</span></strong> if(rs!=null){ if(rs.getRowCount()>0){ Map row=(Map)rs.getRows()[0]; int id = Integer.parseInt(row.get("id").toString()); String name=row.get("uname").toString(); String pwd=row.get("upwd").toString(); String marker=row.get("marker").toString(); entity=new UserInfo(id, name, pwd, marker); } } return entity; } 实现 插入时,使用的 是 插入一个表, public int Register(<strong><span style="color:#3333ff;">UserInfo entity</span>) </strong>throws SQLException { String sql="insert into userinfo values(?,?,?)"; int iline = db.MyExecuteUpdate(sql, entity.getUname(),entity.getUpwd(),entity.getMarker()); return iline; } }</span>
<span style="font-size:18px;">public interface IProductDao { * 实现增加商品 public int Add(Product entity) throws SQLException; * 实现修改商品 public int Update(Product entity) throws SQLException; /** * 删除商品信息 public int DeleteById(int id) throws SQLException; /** * 根据商品编号得到一个商品信息 public Product SelectById(int id) throws SQLException; /** * 获取所有的商品信息列表 public List<Product> GetAll() throws SQLException; /** * 对数据进行分页显示 public List<Product> GetPageList(int pageSize,int pageNo) throws SQLException; /** * 获取行的总数 */ public int GetRowCount() throws SQLException; }</span>实现 这些方法
<span style="font-size:18px;">public class ProductDaoImpl implements IProductDao { DBManager db=new DBManager(); @Override </span><span style="color:#000099;font-size:18px; "><strong>增加和 更新 都是 用到 一个 实例 传入的</strong></span><span style="font-size:18px;"> public int Add(Product entity) throws SQLException { String sql="insert into product values(?,?,?,?)"; Object [] objs={entity.getName(),entity.getUnit(),entity.getPrice(),entity.getCount()}; return db.MyExecuteUpdate(sql, objs); } public int Update(Product entity) throws SQLException { String sql="update product set name=?,unit=?,price=?,count=? where id=?"; Object [] objs={entity.getName(),entity.getUnit(),entity.getPrice(),entity.getCount(),entity.getId()}; return db.MyExecuteUpdate(sql, objs); } </span><span style="font-size:18px; "><span style="color:#993300;">》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》</span></span><span style="font-size:18px;"> @Override public int DeleteById(int id) throws SQLException { String sql="delete from product where id=?"; return db.MyExecuteUpdate(sql,</span><span style="color:#cc0000;font-size:18px; "> </span><span style="color:#ff0000;font-size:18px; "><strong>id)</strong>; 可以 把 id 当成一个 obj的数组传入 也没错</span><span style="font-size:18px;"> } </span><span style="font-size:24px;color:#009900;"><strong>删除和 查出 都是根据 id 的</strong></span><span style="font-size:18px;"> @Override public Product SelectById(int id) throws SQLException { String sql="select * from product where id=?"; Result rs = db.GetResult(sql, id); Product entity=null; </span><strong><span style="font-size:18px;">if(rs!=null){ 这里 不能小看,有这些 if 在, 可以实现过滤 if(rs.getRowCount()>0){ 如果 id 查出来的 只有一个对象 Map row</span><span style="font-size:24px;">=<span style="background-color: rgb(0, 153, 0); ">(Map)rs.getRows()[0];</span> </span></strong><span style="font-size:18px;"> int pid=Integer.parseInt(row.get("id").toString()); String name=row.get("name").toString(); String unit=row.get("unit").toString(); double price=Double.parseDouble(row.get("price").toString()); int count=Integer.parseInt(row.get("count").toString()); <strong>entity=new Product(pid, name, unit, price, count);</strong> } } return entity; } </span><strong><span style="font-size:32px;">对于获得的是 很多的对象时 ,就会 用到 泛型和集合</span></strong><span style="font-size:18px;"> public List<Product> GetAll() throws SQLException { String sql="select * from product"; Result rs = db.GetResult(sql); <strong>List<Product> list=null; <span style="color:#009900;">需要 返回的 一开始都是 null的,不参与 和 if打交道</span> <span style="background-color: rgb(0, 153, 0); ">if(rs!=null){ if(rs.getRowCount()>0){ list=new ArrayList<Product>(); Map[] rows=(Map[])rs.getRows();</span></strong> for(Map row : rows){ int pid=Integer.parseInt(row.get("id").toString()); String name=row.get("name").toString(); String unit=row.get("unit").toString(); double price=Double.parseDouble(row.get("price").toString()); int count=Integer.parseInt(row.get("count").toString()); </span><span style="font-size:24px;"><strong>Product entity=new Product(pid, name, unit, price, count); list.add(entity);</strong></span><span style="font-size:18px;"> } } } return list; } @Override public List<Product> GetPageList(int pageSize, int pageNo) throws SQLException {</span>
<span style="font-size:18px;"><span style="background-color: rgb(51, 204, 0); "> <strong>分页的 语句》》》</strong></span> </span><strong><span style="font-size:24px;background-color: rgb(255, 0, 0); ">String sql="select top "+pageSize+" * from product where id not in (select top "+pageSize*(pageNo-1)+" id from product order by id asc) order by id asc";</span></strong><span style="font-size:18px;"> Result rs = db.GetResult(sql); List<Product> list=null; if(rs!=null){ if(rs.getRowCount()>0){ list=new ArrayList<Product>(); Map[] rows=(Map[])rs.getRows(); for(Map row : rows){ int pid=Integer.parseInt(row.get("id").toString()); String name=row.get("name").toString(); String unit=row.get("unit").toString(); double price=Double.parseDouble(row.get("price").toString()); int count=Integer.parseInt(row.get("count").toString()); Product entity=new Product(pid, name, unit, price, count); list.add(entity); } } } return list; } @Override public int GetRowCount() throws SQLException { String sql="select count(1) as<strong> <span style="color:#cc0000;">cnt</span></strong> from product"; Result rs = db.GetResult(sql); int icount=0; if(rs!=null){ Map row=(Map)rs.getRows()<strong><span style="color:#cc0000;">[0]</span></strong>; icount=Integer.parseInt<strong><span style="color:#cc0000;">(row.get("cnt")</span></strong>.toString()); } return icount; } }</span>
<span style="background-color: rgb(255, 0, 0); ">${error }</span><br/> <span style="background-color: rgb(0, 153, 0); ">通过 后面带的参数,实现 一个类的 多种复用,</span> <form action="<span style="color:#ff0000;">userservlet?action=login</span>" method="post"> 用户名:<input type="text" name="uname" /><br/> 密码:<input type="password" name="upwd"/><br/> <input type="submit" name="btnok" value="登录"/> </form> <a href=<span style="color:#000099;">"<span style="background-color: rgb(192, 192, 192); ">register.jsp</span></span><span style="color:#3333ff;background-color: rgb(192, 192, 192); ">"</span>>注册</a>先写 简单的 注册页面
<span style="background-color: rgb(204, 0, 0); ">${msg }</span><br/> 实现 复用 一个 servlet类 <form action="<span style="color:#000099;">userservlet?action=reg</span>" method="post"> 用户名:<input type="text" name="uname" /><br/> 密码:<input type="password" name="upwd"/><br/> 备注:<input type="text" name="marker" /><br/> <input type="submit" name="btnok" value="注册"/> </form>接着就是 接收 表单的 common 类的写
<span style="font-size:18px;">protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String action=request.getParameter("action"); 这里根据 传入的 <strong><span style="color:#33cc00;">action 把两个 分开</span></strong> if(action.equals("login"))<span style="background-color: rgb(255, 0, 0); ">{传入 的 re 都是包含参数的httpservlet 请求</span></span>
<span style="font-size:18px;"> <span style="background-color: rgb(192, 192, 192); "><span style="color:#000099;">MyLogin(request,response);</span></span> }else if(action.equals("reg")){ MyReg(request,response); } } protected void MyLogin(<span style="background-color: rgb(255, 0, 0); ">HttpServletRequest request</span>, HttpServletResponse response) throws ServletException, IOException { String uname=request.getParameter("uname"); String upwd=request.getParameter("upwd"); try { UserInfo entity = dao.MyLogin(uname, upwd); if(entity.getUname().equals(uname)){ HttpSession session=request.getSession(); session.setAttribute("userinfo", entity); System.out.println("登录成功"); //转发到登录成功的页面(商品列表页面)</span><span style="font-size:24px;"> <span style="background-color: rgb(255, 0, 0); ">也是 一个 复用</span> <span style="background-color: rgb(255, 0, 0); ">MyForward(request, response, "<strong>productservlet?action=pagelist&pageno=1</strong>");</span> </span><span style="font-size:18px;"> }else{ //登录失败,并转发回当前页面继续登录 request.setAttribute("error","登录失败,重新登录!"); MyForward(request,response,"/index.jsp"); } } catch (SQLException e) { e.printStackTrace(); } } protected void MyReg(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String uname=request.getParameter("uname"); String upwd=request.getParameter("upwd"); String marker=request.getParameter("marker"); UserInfo entity=new UserInfo(0, uname, upwd, marker); try { int iline = dao.Register(entity); if(iline>0){ System.out.println("注册成功!"); //返回注册 成功页面,在注册成功页面给一个连接点击登录 MyForward(request, response, "/reg_success.jsp"); }else{ //注册失败 </span><span style="font-size:18px; ">在 实际中, 是 各种的 限制来 确认 是不是失败了</span><span style="font-size:18px;"> </span><span style="font-size:18px; "><span style="color:#ff0000;">request.setAttribute("msg", "注册失败了,重新再试一次。");</span></span><span style="font-size:18px;"> MyForward(request, response, "/register.jsp"); } } catch (SQLException e) { e.printStackTrace(); } } </span><span style="font-size:32px;"> <strong> 这是 一个 简化 转发的 方法,</strong></span><span style="font-size:18px;"> public void MyForward(HttpServletRequest request,HttpServletResponse response,String path) throws ServletException, IOException{ request.getRequestDispatcher(path).forward(request, response); } }</span><span style="font-size:32px;"> </span>
接着 是进入了 商品的 servlet 中,
<span style="font-size:14px;">protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { </span><span style="font-size:24px;">String action=request.getParameter("action");</span>
<span style="font-size:24px;"> <strong><span style="color:#009900;">这样可以 实现 少写 一些 接收的 servlet,为了 以后的 登录验证 过滤器的 编写方便</span> <span style="color:#3333ff;">if(action.equals("add")){ Add(request,response); }else if(action.equals("getentity")){ GetEntity(request,response); }else if(action.equals("update")){ Update(request,response); }else if(action.equals("delete")){ Delete(request,response); }else if(action.equals("pagelist")){ SelectPageList(request,response); }</span></strong></span><span style="font-size:14px;"> } protected void Add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name=request.getParameter("name"); String unit=request.getParameter("unit"); double price=Double.parseDouble(request.getParameter("price")); int count=Integer.parseInt(request.getParameter("count")); Product entity=new Product(0, name, unit, price, count); try { int iline = dao.Add(entity); if(iline>0){ //执行成功,则跳转到列表页面 MyForward(request, response, "productservlet?action=pagelist&pageno=1"); }else{ request.setAttribute("error", "添加商品信息失败!"); //执行失败,跳转到error.jsp页面 MyForward(request, response, "/error.jsp"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } protected void GetEntity(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int id=Integer.parseInt(request.getParameter("id")); try { Product entity = dao.SelectById(id); request.setAttribute("entity", entity); MyForward(request, response, "/updateProduct.jsp"); } catch (SQLException e) { e.printStackTrace(); } } protected void Update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int id=Integer.parseInt(request.getParameter("id")); String name=request.getParameter("name"); String unit=request.getParameter("unit"); double price=Double.parseDouble(request.getParameter("price")); int count=Integer.parseInt(request.getParameter("count")); Product entity=new Product(id, name, unit, price, count); try { int iline = dao.Update(entity); if(iline>0){ //执行成功,则跳转到列表页面 MyForward(request, response, "productservlet?action=pagelist&pageno=1"); }else{ request.setAttribute("error", "修改商品信息失败!"); //执行失败,跳转到error.jsp页面 MyForward(request, response, "/error.jsp"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } protected void Delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int id=Integer.parseInt(request.getParameter("id")); try { int iline = dao.DeleteById(id); if(iline>0){ //执行成功,则跳转到列表页面 MyForward(request, response, "productservlet?action=pagelist&pageno=1"); }else{ request.setAttribute("error", "删除商品信息失败!"); //执行失败,跳转到error.jsp页面 MyForward(request, response, "/error.jsp"); } } catch (SQLException e) { e.printStackTrace(); } } <strong>protected void SelectPageList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {</strong></span><pre name="code" class="java"><strong><span style="font-size:14px;"> int pagesize=3; </span></strong><pre name="code" class="java"><span style="background-color: rgb(51, 204, 0); "><span style="font-size:24px;">/*</span></span>这里 有一个 很 重要的 点 就是 request.getParameter("pageno")
protected void SelectPageList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int pagesize=3; int pageno=Integer.parseInt(request.getParameter("pageno")); int rowcount=dao.GetRowCount();//得到总行数 int pagecount=rowcount%pagesize==0?rowcount/pagesize:rowcount/pagesize+1; if(pageno<1){ pageno=1;//如果传递过来的页码为比1小的值时,改为显示第一页 }else if(pageno>pagecount){ pageno=pagecount;//如果页码大于了最后一页的页码 则改为最后一页 } List<Product> list = dao.GetPageList(pagesize, pageno); <strong><span style="font-size:24px;color:#ff0000;">request.setAttribute("pageno", pageno); 很 神奇的 一句 request.setAttribute("pagecount", pagecount); request.setAttribute("list", list);</span></strong> MyForward(request, response, "/ProductList.jsp"); }
这里 是商品 显示界面
<span style="font-size:18px;"> <a href="addProduct.jsp">增加商品</a></span>
<span style="font-size:18px;"> <c:if test="${list != null }"> <table> <caption> 商品信息列表 </caption> <tr> <th>商品名称</th> <th>商品单位</th> <th>单价</th> <th>库存</th> <th>操作</th> </tr> <c:forEach items="${list }" var="e"> <tr><td>${e.name }</td><td>${e.unit }</td><td>${e.price }</td><td>${e.count }</td> <td> <a href="productservlet?action=getentity&id=${e.id }">修改</a> <a href="productservlet?action=delete&id=${e.id }">删除</a> </td> </tr> </c:forEach> <tr> <td> <a href="productservlet?action=pagelist&pageno=1">首页</a> </td> <span style="color:#33cc00;"><strong>后面的 pageno 可以 进行 算术 运算 只是因为 第一次 给了 值</strong></span> <td><a href="productservlet?action=pagelist&pageno=${pageno+1 }">下一页</a></td> <td><a href="productservlet?action=pagelist&pageno=${pageno-1 }">上一页</a></td> <td><a href="productservlet?action=pagelist&pageno=${pagecount }">最后一页</a></td> <td></td> </tr> </table> </c:if> </body> </html></span>
其他的 就没有 什么的 了
标签:
原文地址:http://blog.csdn.net/sinat_27639721/article/details/51878869