【javaweb】庫存物資管理系統思路與總結
題目:
1、有一個存放商品的倉庫,每天都有商品出庫和入庫。
2、每種商品都有名稱、生產廠家、型號、規格等。
3、出入庫時必須填寫出入庫單據,單據包括商品名稱、生產廠家、型號、 規格、數量、日期、時間、入庫單位(或出庫單位)名稱、送貨(或提貨)人 姓名。
首先建立資料庫goodsmanager
table:goods記錄商品資訊
table:list記錄出入庫資訊
Java部分:
先把頁面寫好
首頁面用<iframe>分為左側導航欄,和右側頁面
點選左側超連結即可實現右側頁面的跳轉,在left.jsp中使用<a>的target屬性來控制
1 <div> 2 <div><a style="color:blue" target="right" href="select.jsp">查詢</a></div><br/> 3 <div><a style="color:blue" target="right" href="out.jsp">出庫</a></div><br/> 4 <div><a style="color:blue" target="right" href="in.jsp"left.jsp>入庫</a></div><br/> 5 <div><a style="color:blue" target="right" href="1.jsp">增加商品</a></div><br/> 6 <div><a style="color:blue" target="right" href="2.jsp">刪除商品</a></div><br/> 7 <div><a style="color:blue" target="right" href="3.jsp">修改商品</a></div><br/> 8 <div><a style="color:blue" target="right" href="4.jsp">查詢商品</a></div><br/> 9 </div>
驗證跳轉沒有問題後,先寫出庫和入庫。
新建資料庫連線工具DB.java,放到com.ccr.jap.util包下
新建GoodsBean和ListBean,包括對應的私有變數和get、set函式
新建GoodsDao、ListDao進行資料庫的增刪改查操作
入庫涉及到兩種情況:
1、商品已存在
2、商品未存在
首先呼叫GoodsDao裡的chick()函式,如果商品存在,返回id,否則返回-1;
1 public int chick(GoodsBean g) 2 { 3 int i=-1;//i>0查詢有結果 4 DB db=new DB(); 5 Connection con = db.getCon(); 6 try 7 { 8 Statement stm = con.createStatement(); 9 ResultSet rs =stm.executeQuery("select id from goods where name='"+g.getName()+"' and PM='"+g.getPM()+"' and type='"+g.getType()+"' and specification='"+g.getSpecification()+"'"); 10 if(rs.next()) 11 { 12 i=rs.getInt("id"); 13 } 14 } 15 catch (Exception e) 16 { 17 e.printStackTrace(); 18 } 19 return i; 20 }GoodsDao.chick()
如果存在,直接修改商品數量
1 public boolean update(int number,int i) 2 { 3 int num=0; 4 DB db=new DB(); 5 Connection con = db.getCon(); 6 try 7 { 8 Statement stm = con.createStatement(); 9 ResultSet rs =stm.executeQuery("select number from goods where id="+i); 10 System.out.println("update"+i+"num "+number); 11 rs.next(); 12 num=rs.getInt("number"); 13 System.out.print(num+"+"+number); 14 num=num+number; 15 System.out.println("="+num); 16 stm.execute("update goods set number="+num+" where id="+i); 17 } 18 catch (Exception e) 19 { 20 e.printStackTrace(); 21 return false; 22 } 23 return true; 24 }GoodsDao.update(int number,int i)
不存在,則新建商品資訊
1 public boolean insert(GoodsBean g) 2 { 3 DB db=new DB(); 4 Connection con = db.getCon(); 5 try 6 { 7 Statement stm = con.createStatement(); 8 stm.execute("insert into goods(name,PM,type,specification,number) values ('"+g.getName()+"','"+g.getPM()+"','"+g.getType()+"','"+g.getSpecification()+"',"+g.getNumber()+")"); 9 } 10 catch (Exception e) 11 { 12 e.printStackTrace(); 13 return false; 14 } 15 return true; 16 }GoodsDao.insert(GoodsBean g)
然後呼叫ListDao裡的insert()函式新建入庫資訊
1 public boolean insert(ListBean l) 2 { 3 DB db=new DB(); 4 Connection con = db.getCon(); 5 try 6 { 7 Statement stm = con.createStatement(); 8 stm.execute("insert into list(name,PM,type,specification,number,units,PName,io,date) " 9 + "values ('"+l.getName()+"','"+l.getPM()+"','"+l.getType()+"','"+l.getSpecification()+"',"+l.getNumber()+",'"+l.getUnits()+"','"+l.getPName()+"','"+l.getIo()+"','"+l.getDate()+"')"); 10 } 11 catch (Exception e) 12 { 13 e.printStackTrace(); 14 return false; 15 } 16 return true; 17 }ListDao.insert(ListBean l)
出庫同理,但是在number賦值前先乘以-1變成負數
然後是servlet和jsp部分
入庫:
1 <h1>入庫</h1> 2 <form method="post" action="GoodsServlet?action=in"> 3 <div> 4 <div> 5 <div style="display:inline"><label for="name"> 名 稱 </label></div> 6 <div style="display:inline"><input type="text" name="name" id="name"></div> 7 </div><br/> 8 <div> 9 <div style="display:inline"><label for="name"> 型 號 </label></div> 10 <div style="display:inline"><input type="text" name="type" id="type"></div> 11 </div><br/> 12 <div> 13 <div style="display:inline"><label for="name">出產廠家</label></div> 14 <div style="display:inline"><input type="text" name="PM" id="PM"></div> 15 </div><br/> 16 <div> 17 <div style="display:inline"><label for="name"> 規 格 </label></div> 18 <div style="display:inline"><input type="text" name="specification" id="specification"></div> 19 </div><br/> 20 <div> 21 <div style="display:inline"><label for="name">入庫單位</label></div> 22 <div style="display:inline"><input type="text" name="units" id="units"></div> 23 </div><br/> 24 <div> 25 <div style="display:inline"><label for="name">送貨人名</label></div> 26 <div style="display:inline"><input type="text" name="PName" id="PName"></div> 27 </div><br/> 28 <div> 29 <div style="display:inline"><label for="name"> 數 量 </label></div> 30 <div style="display:inline"><input type="text" name="number" id="number"></div> 31 </div><br/> 32 <div> 33 <div><input type="submit" value="提交"></div> 34 </div> 35 </div> 36 </form>in.jsp
提交form表單到GoodsServlet,並附加資訊action=in,出庫同理
1 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 2 { 3 request.setCharacterEncoding("UTF-8"); 4 response.setCharacterEncoding("UTF-8"); 5 response.setContentType("text/html;charset=UTF-8"); 6 response.setHeader("content-type", "text/html;charset=UTF-8"); 7 String action=request.getParameter("action"); 8 if(action.equals("select")) 9 { 10 select(request,response); 11 } 12 if(action.equals("in")) 13 { 14 in(request,response); 15 } 16 if(action.equals("out")) 17 { 18 out(request,response); 19 } 20 } 21 protected void in(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 22 { 23 int id=-2; 24 ListBean b=new ListBean(); 25 GoodsBean g=new GoodsBean(); 26 GoodsDao gd=new GoodsDao(); 27 ListDao ld=new ListDao(); 28 b.setName(request.getParameter("name")); 29 b.setPM(request.getParameter("PM")); 30 b.setType(request.getParameter("type")); 31 b.setSpecification(request.getParameter("specification")); 32 b.setNumber(Integer.parseInt(request.getParameter("number"))); 33 b.setUnits(request.getParameter("units")); 34 b.setPName(request.getParameter("PName")); 35 b.setIo("in"); 36 b.setDate(); 37 ld.insert(b); 38 39 40 g.setName(request.getParameter("name")); 41 g.setPM(request.getParameter("PM")); 42 g.setType(request.getParameter("type")); 43 g.setSpecification(request.getParameter("specification")); 44 g.setNumber(Integer.parseInt(request.getParameter("number"))); 45 id=gd.chick(g); 46 System.out.println(id); 47 if(id>0) 48 { 49 gd.update(b.getNumber(), id); 50 } 51 else 52 { 53 gd.insert(g); 54 } 55 request.getRequestDispatcher("in.jsp").forward(request,response); 56 } 57 protected void out(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 58 { 59 int id=-2; 60 ListBean b=new ListBean(); 61 GoodsBean g=new GoodsBean(); 62 GoodsDao gd=new GoodsDao(); 63 ListDao ld=new ListDao(); 64 b.setName(request.getParameter("name")); 65 b.setPM(request.getParameter("PM")); 66 b.setType(request.getParameter("type")); 67 System.out.println(b.getType()); 68 b.setSpecification(request.getParameter("specification")); 69 int number=Integer.parseInt(request.getParameter("number")); 70 number=-1*number; 71 System.out.println("SN"+number); 72 b.setNumber(number); 73 b.setUnits(request.getParameter("units")); 74 b.setPName(request.getParameter("PName")); 75 b.setIo("out"); 76 b.setDate(); 77 ld.insert(b); 78 79 80 g.setName(request.getParameter("name")); 81 g.setPM(request.getParameter("PM")); 82 g.setType(request.getParameter("type")); 83 g.setSpecification(request.getParameter("specification")); 84 g.setNumber(number); 85 id=gd.chick(g); 86 if(id>0) 87 { 88 gd.update(b.getNumber(), id); 89 } 90 else 91 { 92 System.out.println("錯誤"); 93 } 94 request.getRequestDispatcher("out.jsp").forward(request,response); 95 }View Code
查詢:在頁面中,輸入日期和時間,點選查詢按鈕,出現查詢結構
呼叫ListDao的Select,使用名稱和日期進行查詢,將結果存在List中
jsp程式碼
1 <h1>資訊查詢</h1> 2 <form method="post" action="GoodsServlet?action=select"> 3 <div> 4 <div> 5 <div><label>產品名稱</label></div> 6 <div><input type="text" name="name" id="name"></div> 7 </div> 8 <div> 9 <div><label>日期</label></div> 10 <div><input type="text" name="date" id="date"></div> 11 </div> 12 <div> 13 <input type="submit" value="查詢"> 14 </div> 15 </div> 16 </form> 17 <c:set var="list" value="${sessionScope.l}"></c:set> 18 <c:if test="${l!=null }"> 19 <c:forEach var="i" begin="0" end="${l.size()-1}"> 20 名稱 ${l[i].getName()} 21 型號 ${l[i].getType()} 22 出產廠家 ${l[i].getPM()} 23 規格 ${l[i].getSpecification()} 24 數量 ${l[i].getNumber()} 25 單位 ${l[i].getUnits()} 26 姓名 ${l[i].getPName()} 27 操作 ${l[i].getIo()} 28 日期 ${l[i].getDate()} 29 <br/> 30 </c:forEach> 31 </c:if>select.jsp
使用了EL語句和JSTL標籤進行遍歷
Java程式碼:
1 public List<ListBean> select(String name,String date) 2 { 3 List<ListBean> l=new ArrayList<ListBean>(); 4 ListBean b=null; 5 DB db=new DB(); 6 Connection con = db.getCon(); 7 try 8 { 9 Statement stm = con.createStatement(); 10 ResultSet rs = stm.executeQuery("select * from list where name like '%"+name+"%' and date ='"+date+"'"); 11 while(rs.next()) 12 { 13 b=new ListBean(); 14 b.setName(rs.getString("name")); 15 b.setPM(rs.getString("PM")); 16 b.setType(rs.getString("type")); 17 b.setSpecification(rs.getString("specification")); 18 b.setNumber(rs.getInt("number")); 19 b.setUnits(rs.getString("units")); 20 b.setPName(rs.getString("PName")); 21 b.setIo(rs.getString("io")); 22 b.setDate(rs.getString("date")); 23 l.add(b); 24 } 25 } catch (Exception e) 26 { 27 e.printStackTrace(); 28 29 } 30 return l; 31 }ListDao.select(String name,String date)
GoodsServlet:
1 protected void select(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 2 { 3 String name=request.getParameter("name"); 4 String date=request.getParameter("date"); 5 ListDao d=new ListDao(); 6 List<ListBean>l=d.select(name,date); 7 request.getSession().setAttribute("l", l); 8 request.getRequestDispatcher("select.jsp").forward(request,response); 9 }View Code
第二部分:商品的增刪改查
首先寫好Dao相關的操作函式
1 public class GoodsDao 2 { 3 public boolean delete(String name) 4 { 5 DB db=new DB(); 6 Connection con = db.getCon(); 7 try 8 { 9 Statement stm = con.createStatement(); 10 stm.execute("delete from goods where name='"+name+"'"); 11 } 12 catch (Exception e) 13 { 14 e.printStackTrace(); 15 return false; 16 } 17 return true; 18 } 19 public List<GoodsBean> select(String name) 20 { 21 List<GoodsBean> l=new ArrayList<GoodsBean>(); 22 GoodsBean b=null; 23 DB db=new DB(); 24 Connection con = db.getCon(); 25 try 26 { 27 Statement stm = con.createStatement(); 28 ResultSet rs = stm.executeQuery("select * from goods where name like '%"+name+"%'"); 29 while(rs.next()) 30 { 31 b=new GoodsBean(); 32 b.setName(rs.getString("name")); 33 b.setPM(rs.getString("PM")); 34 b.setType(rs.getString("type")); 35 b.setSpecification(rs.getString("specification")); 36 b.setNumber(rs.getInt("number")); 37 l.add(b); 38 } 39 } catch (Exception e) 40 { 41 e.printStackTrace(); 42 43 } 44 return l; 45 } 46 public boolean insert(GoodsBean g) 47 { 48 DB db=new DB(); 49 Connection con = db.getCon(); 50 try 51 { 52 Statement stm = con.createStatement(); 53 stm.execute("insert into goods(name,PM,type,specification,number) values ('"+g.getName()+"','"+g.getPM()+"','"+g.getType()+"','"+g.getSpecification()+"',"+g.getNumber()+")"); 54 } 55 catch (Exception e) 56 { 57 e.printStackTrace(); 58 return false; 59 } 60 return true; 61 } 62 public boolean update(int number,int i) 63 { 64 int num=0; 65 DB db=new DB(); 66 Connection con = db.getCon(); 67 try 68 { 69 Statement stm = con.createStatement(); 70 ResultSet rs =stm.executeQuery("select number from goods where id="+i); 71 System.out.println("update"+i+"num "+number); 72 rs.next(); 73 num=rs.getInt("number"); 74 System.out.print(num+"+"+number); 75 num=num+number; 76 System.out.println("="+num); 77 stm.execute("update goods set number="+num+" where id="+i); 78 } 79 catch (Exception e) 80 { 81 e.printStackTrace(); 82 return false; 83 } 84 return true; 85 } 86 public boolean update(String name,GoodsBean b) 87 { 88 System.out.println("Dao"+name+b.getName()+b.getType()); 89 DB db=new DB(); 90 Connection con = db.getCon(); 91 try 92 { 93 Statement stm = con.createStatement(); 94 stm.execute("update goods set name='"+b.getName()+"',type='"+b.getType()+"',PM='"+b.getPM()+"',specification='"+b.getSpecification()+"' where name='"+name+"'"); 95 } 96 catch (Exception e) 97 { 98 e.printStackTrace(); 99 return false; 100 } 101 return true; 102 } 103 public int chick(GoodsBean g) 104 { 105 int i=-1;//i>0查詢有結果 106 DB db=new DB(); 107 Connection con = db.getCon(); 108 try 109 { 110 Statement stm = con.createStatement(); 111 ResultSet rs =stm.executeQuery("select id from goods where name='"+g.getName()+"' and PM='"+g.getPM()+"' and type='"+g.getType()+"' and specification='"+g.getSpecification()+"'"); 112 if(rs.next()) 113 { 114 i=rs.getInt("id"); 115 } 116 } 117 catch (Exception e) 118 { 119 e.printStackTrace(); 120 } 121 return i; 122 } 123 }GoodsDao
在修改時,重構一個引數為GoodsBean和name的函式
然後寫servlet(由於之前忘了寫這一部分,所以直接起名為GoodsServlet2)
1 package com.ccr.jsp.servlet; 2 3 import java.io.IOException; 4 import java.util.List; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.annotation.WebServlet; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 import com.ccr.jsp.bean.GoodsBean; 13 import com.ccr.jsp.bean.ListBean; 14 import com.ccr.jsp.dao.GoodsDao; 15 import com.ccr.jsp.dao.ListDao; 16 17 /** 18 * Servlet implementation class GoodsServlet2 19 */ 20 @WebServlet("/GoodsServlet2") 21 public class GoodsServlet2 extends HttpServlet { 22 private static final long serialVersionUID = 1L; 23 24 /** 25 * @see HttpServlet#HttpServlet() 26 */ 27 public GoodsServlet2() { 28 super(); 29 // TODO Auto-generated constructor stub 30 } 31 32 /** 33 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 34 */ 35 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 36 request.setCharacterEncoding("UTF-8"); 37 response.setCharacterEncoding("UTF-8"); 38 response.setContentType("text/html;charset=UTF-8"); 39 response.setHeader("content-type", "text/html;charset=UTF-8"); 40 String action=request.getParameter("action"); 41 if(action.equals("add")) 42 { 43 System.out.println("add"); 44 add(request,response); 45 } 46 if(action.equals("delete")) 47 { 48 delete(request,response); 49 } 50 if(action.equals("update")) 51 { 52 update(request,response); 53 } 54 if(action.equals("select")) 55 { 56 select(request,response); 57 } 58 if(action.equals("select2")) 59 { 60 select2(request,response); 61 } 62 } 63 protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 64 { 65 int id=-2; 66 GoodsBean g=new GoodsBean(); 67 GoodsDao gd=new GoodsDao(); 68 g.setName(request.getParameter("name")); 69 g.setPM(request.getParameter("PM")); 70 g.setType(request.getParameter("type")); 71 g.setSpecification(request.getParameter("specification")); 72 g.setNumber(0); 73 System.out.println(id); 74 gd.insert(g); 75 request.getRequestDispatcher("1.jsp").forward(request,response); 76 } 77 protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 78 { 79 String name=request.getParameter("name"); 80 GoodsDao gd=new GoodsDao(); 81 gd.delete(name); 82 request.getRequestDispatcher("2.jsp").forward(request,response); 83 } 84 protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 85 { 86 String name=request.getParameter("name0"); 87 System.out.println("SNAME"+name); 88 GoodsBean g=new GoodsBean(); 89 GoodsDao gd=new GoodsDao(); 90 g.setName(request.getParameter("name")); 91 g.setPM(request.getParameter("PM")); 92 g.setType(request.getParameter("type")); 93 g.setSpecification(request.getParameter("specification")); 94 System.out.println(g.getName()+g.getType()); 95 gd.update(name,g); 96 request.getRequestDispatcher("3.jsp").forward(request,response); 97 } 98 protected void select(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 99 { 100 String name=request.getParameter("name0"); 101 System.out.println("name="+name); 102 GoodsDao gd=new GoodsDao(); 103 List<GoodsBean>gl=gd.select(name); 104 System.out.println(gl.size()); 105 request.getSession().setAttribute("gl", gl); 106 request.getRequestDispatcher("4.jsp").forward(request,response); 107 } 108 protected void select2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 109 { 110 String name=request.getParameter("name0"); 111 System.out.println("name="+name); 112 GoodsDao gd=new GoodsDao(); 113 List<GoodsBean>gl=gd.select(name); 114 System.out.println(gl.size()); 115 request.getSession().setAttribute("gl", gl); 116 request.getRequestDispatcher("3.jsp").forward(request,response); 117 } 118 }GoodsServlet2
在插入時,直接設定數量為0
最後設計一下介面。完工!
總結:
1、新學習了el語句和JSTL標籤庫的一些使用,很方便
2、複製程式碼很快,但是很容易出錯