创建项目、配置环境等操作详见我的另一篇博客。
思路:先创建商品的数据表,完成商品数据库的增删,在建立表单的数据库,编写增删改查,在出库和入库的时候分别调用相应的商品增删函数。
遇到的问题:在表单的增删时,遇到了无法存储的操作,最后改了表单名,完成了操作,在遍历输出数据库内容时,运用了list容器的方法。遍历输出list中的内容。
<%List<Danju> l = (List<Danju>)session.getAttribute("list");%> <%for(int i=0;i<l.size();i++){%> <tr> <td><%=l.get(i).getCaozuo()%></td> <td><%=l.get(i).getName()%></td> <td><%=l.get(i).getChangjia()%></td> <td><%=l.get(i).getXinghao()%></td> <td><%=l.get(i).getGuige()%></td> <td><%=l.get(i).getNum()%></td> <td><%=l.get(i).getDate()%></td> <td><%=l.get(i).getTime()%></td> <td><%=l.get(i).getDanwei()%></td> <td><%=l.get(i).getPeople()%></td> <%}%>
程序的实现步骤:
开始的数据库内容:商品数据库
表单数据库:
程序界面:
添加商品
删除商品:
添加入库表单
出库操作:
显示全部表单
查询表单信息:
相关源代码:
Danju.java:
package com.Warehouse; public class Danju { private String caozuo; private String name; private String changjia; private String xinghao; private String guige; private String num; private String date; private String time; private String danwei; private String people; public Danju(String cao,String na,String chang,String xing,String gui,String num,String date,String time,String dan,String people) { this.caozuo=cao; this.name=na; this.changjia=chang; this.xinghao=xing; this.guige=gui; this.num=num; this.date=date; this.time=time; this.danwei=dan; this.people=people; } public String getCaozuo() { return caozuo; } public void setCaozuo(String caozuo) { this.caozuo = caozuo; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getChangjia() { return changjia; } public void setChangjia(String changjia) { this.changjia = changjia; } public String getXinghao() { return xinghao; } public void setXinghao(String xinghao) { this.xinghao = xinghao; } public String getGuige() { return guige; } public void setGuige(String guige) { this.guige = guige; } public String getNum() { return num; } public void setNum(String num) { this.num = num; } public String getTime() { return time; } public void setTime(String time) { this.time = time; } public String getDanwei() { return danwei; } public void setDanwei(String danwei) { this.danwei = danwei; } public String getPeople() { return people; } public void setPeople(String people) { this.people = people; } }
Warehouse.java:
package com.Warehouse; public class Warehouse { private String name; private String Manufacturer; private String xinghao; private String guige; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getManufacturer() { return Manufacturer; } public void setManufacturer(String manufacturer) { Manufacturer = manufacturer; } public String getXinghao() { return xinghao; } public void setXinghao(String xinghao) { this.xinghao = xinghao; } public String getGuige() { return guige; } public void setGuige(String guige) { this.guige = guige; } public Warehouse(String na,String Manu,String xin,String gui) { this.name=na; this.Manufacturer=Manu; this.xinghao=xin; this.guige=gui; } }
DBUtil.java:
package com.DBUtil; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DBUtil { public static String db_url = "jdbc:mysql://localhost:3306/warehouse?useSSL=false&serverTimezone=GMT"; public static String db_user = "root"; public static String db_pass = "1234567"; public static Connection getConn () { Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(db_url, db_user, db_pass); } catch (Exception e) { e.printStackTrace(); } return conn; } /** * 鍏抽棴杩炴帴 * @param state * @param conn */ public static void close (Statement state, Connection conn) { if (state != null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close (ResultSet rs, Statement state, Connection conn) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (state != null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
Serlvet:
package com.serlvet; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.Warehouse.Danju; import com.Warehouse.Warehouse; import com.daobao.Dao; /** * Servlet implementation class Serlvet */ @WebServlet("/Serlvet") public class Serlvet extends HttpServlet { private static final long serialVersionUID = 1L; Dao service=new Dao(); /** * @see HttpServlet#HttpServlet() */ public Serlvet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String method = req.getParameter("method"); if ("add".equals(method)) { add(req, resp); } else if ("del".equals(method)) { del(req, resp); } else if ("show".equals(method)) { show(req, resp); } else if ("search".equals(method)) { search(req, resp); }else if ("inwarehouse".equals(method)) { Inwarehouse(req, resp); }else if ("outwarehouse".equals(method)) { outwarehouse(req, resp); } } private void outwarehouse(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException { String str="出库"; String name = req.getParameter("name"); String changjia = req.getParameter("changjia"); String xinghao = req.getParameter("xinghao"); String guige = req.getParameter("guige"); String num = req.getParameter("num"); String date = req.getParameter("date"); String time = req.getParameter("time"); String danwei = req.getParameter("danwei"); String people = req.getParameter("people"); Danju course=new Danju(str,name,changjia,xinghao,guige,num,date,time,danwei,people); if(service.add2(course)&&service.Dlete(name)) { req.setAttribute("message", "出库成功"); req.getRequestDispatcher("OutWarehouse.jsp").forward(req,resp); } else { req.setAttribute("message", "出库失败"); req.getRequestDispatcher("OutWarehouse.jsp").forward(req,resp); } } private void Inwarehouse(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String str="入库"; String name = req.getParameter("name"); String changjia = req.getParameter("changjia"); String xinghao = req.getParameter("xinghao"); String guige = req.getParameter("guige"); String num = req.getParameter("num"); String date = req.getParameter("date"); String time = req.getParameter("time"); String danwei = req.getParameter("danwei"); String people = req.getParameter("people"); Danju course=new Danju(str,name,changjia,xinghao,guige,num,date,time,danwei,people); Warehouse course2 = new Warehouse(name, changjia, xinghao, guige); if(service.add2(course)&&service.add(course2)) { req.setAttribute("message", "入库成功"); req.getRequestDispatcher("InWarehouse.jsp").forward(req,resp); } else { req.setAttribute("message", "入库失败"); req.getRequestDispatcher("InWarehouse.jsp").forward(req,resp); } } private void add(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException { //req.setCharacterEncoding("utf-8"); String name = req.getParameter("name"); String manu = req.getParameter("manu"); String xing = req.getParameter("xing"); String gui = req.getParameter("gui"); Warehouse course = new Warehouse(name, manu, xing, gui); //添加后消息显示 if(service.add(course)) { req.setAttribute("message", "添加成功"); req.getRequestDispatcher("Add.jsp").forward(req,resp); } else { req.setAttribute("message", "添加失败"); req.getRequestDispatcher("Add.jsp").forward(req,resp); } } private void del(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String id = req.getParameter("id"); if(service.Dlete(id)) { req.setAttribute("message", "删除成功"); req.getRequestDispatcher("Delte.jsp").forward(req,resp); } else { req.setAttribute("message", "删除失败"); req.getRequestDispatcher("Delte.jsp").forward(req,resp); } } private void show(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException { req.setCharacterEncoding("utf-8"); List<Danju> courses = service.list(); HttpSession session = req.getSession(); session.setAttribute("list", courses); req.getRequestDispatcher("Show.jsp").forward(req,resp); } private void search(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String name = req.getParameter("name"); String date = req.getParameter("date"); HttpSession session = req.getSession(); List<Danju> courses = service.search(name,date); session.setAttribute("list", courses); req.getRequestDispatcher("Show.jsp").forward(req,resp); } }
Dao.java:
package com.daobao; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import com.Warehouse.Danju; import com.Warehouse.Warehouse; import com.DBUtil.DBUtil; public class Dao { public boolean add(Warehouse warehouse) { String sql ="insert into commodity(名称, 生产厂家, 型号, 规格) value('"+warehouse.getName()+"','"+warehouse.getManufacturer()+"','"+warehouse.getXinghao()+"','"+warehouse.getGuige()+"')"; Connection conn = DBUtil.getConn(); Statement state=null; boolean f=false; int a = 0; try { state =conn.createStatement(); a = state.executeUpdate(sql); }catch(Exception e) { }finally { DBUtil.close(state, conn); } if(a>0)f=true; return f; } public boolean add2(Danju dan) { String sql ="insert into inwarehouse(操作, 名称, 生产厂家, 型号, 规格, 数量, 日期, 时间, 入(出)库单位名称, 送货人姓名) value('"+dan.getCaozuo()+"','"+dan.getName()+"','"+dan.getChangjia()+"','"+dan.getXinghao()+"','"+dan.getGuige()+"','"+dan.getNum()+"','"+dan.getDate()+"','"+dan.getTime()+"','"+dan.getDanwei()+"','"+dan.getPeople()+"')"; Connection conn = DBUtil.getConn(); Statement state=null; boolean f=false; int a = 0; try { state =conn.createStatement(); a = state.executeUpdate(sql); }catch(Exception e) { }finally { DBUtil.close(state, conn); } if(a>0)f=true; return f; } public boolean Dlete(String id) { boolean f = false; String sql = "delete from commodity where 名称='" + id + "'"; Connection conn = DBUtil.getConn(); Statement state = null; int a = 0; try { state = conn.createStatement(); a = state.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(state, conn); } if (a > 0) { f = true; } return f; } public List<Danju> list() { String sql = "select * from inwarehouse"; List<Danju> list = new ArrayList<>(); Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); Danju bean = null; while (rs.next()) { String caozuo = rs.getString("操作"); String name = rs.getString("名称"); String changjia = rs.getString("生产厂家"); String xinghao = rs.getString("型号"); String guige = rs.getString("规格"); String num = rs.getString("数量"); String date = rs.getString("日期"); String time = rs.getString("时间"); String danwei = rs.getString("入(出)库单位名称"); String people = rs.getString("送货人姓名"); bean = new Danju(caozuo, name, changjia, xinghao,guige,num,date,time,danwei,people); list.add(bean); } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return list; } public List<Danju> search(String name, String date) { String sql = "select * from inwarehouse where "; if (name != "") { sql += "名称= '"+name+"'"; } if (date != "") { sql += "日期= '"+date+"'"; } List<Danju> list = new ArrayList<>(); Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); Danju bean = null; while (rs.next()) { String caozuo = rs.getString("操作"); String name2 = rs.getString("名称"); String changjia = rs.getString("生产厂家"); String xinghao = rs.getString("型号"); String guige = rs.getString("规格"); String num = rs.getString("数量"); String date2 = rs.getString("日期"); String time2 = rs.getString("时间"); String danwei = rs.getString("入(出)库单位名称"); String people = rs.getString("送货人姓名"); bean = new Danju(caozuo, name2, changjia, xinghao,guige,num,date2,time2,danwei,people); list.add(bean); } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return list; } }
Warehouse.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <style> h1{text-align:center;text-bgcolor:red; } a { text-decoation:none;} </style> </head> <body> <h1>仓库管理系统</h1> <div id=Layer1 style="position:absolute; ;z-index:1;left: 0px;top:50px;"> <table border="1" > <tr bgcolor="#DCDCDC"><td></td></tr> <tr><td align="center" bgcolor=Aqua>仓库管理</td></tr> <tr><td align="center"> <a href="Add.jsp">添加商品</a></td></tr> <tr><td align="center"> <a href="Delte.jsp">删除商品</a></td></tr> <tr><td align="center" bgcolor=Aqua>表单管理</td></tr> <tr><td align="center"> <a href="InWarehouse.jsp">入库</a></td></tr> <tr><td align="center"> <a href="OutWarehouse.jsp">出库</a></td></tr> <tr><td align="center"> <a href="Serlvet?method=show">显示出入库信息</a></td></tr> <tr><td align="center"> <a href="Search.jsp">查询(入)出库信息</a></td></tr> </table> </div> </body> </html>
Add.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <style> div{ text-align:center;} </style> </head> <body> <% Object message = request.getAttribute("message"); if(message!=null && !"".equals(message)){ %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); </script> <%} %> <div align="center"> <a href="Warehouse.jsp">返回首页</a> </div> <form action="Serlvet?method=add" method="post" onsubmit="return check()"> <div > <table align="center" border="1"> <tr> <td align="right">商品名称</td> <td><input type="text" id="name" name="name"/></td> </tr> <tr> <td align="right">生产厂家</td> <td><input type="text" id="manu" name="manu"/></td> </tr> <tr> <td align="right">型号</td> <td><input type="text" id="xing" name="xing"/></td> </tr> <tr> <td align="right">规格</td> <td><input type="text" id="gui" name="gui"/></td> </tr> </table> </div> <div align="center"> <button type="submit" style="160px;color:Write;">保 存</button> </div> </form> <script type="text/javascript"> function check(){ var name = document.getElementById("name"); var manu = document.getElementById("manu"); var xing = document.getElementById("xing"); var gui = document.getElementById("gui"); if(name.value == '') { alert('课程名称为空'); name.focus(); return false; } if(manu.value == '') { alert('课程名称为空'); name.focus(); return false; } if(xing.value == '') { alert('课程名称为空'); name.focus(); return false; } if(gui.value == '') { alert('课程名称为空'); name.focus(); return false; } } </script> </body> </html>
Delte.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <% Object message = request.getAttribute("message"); if(message!=null && !"".equals(message)){ %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); </script> <%} %> <div align="center"> <div> <a href="Warehouse.jsp">返回首页</a> </div> <form action="Serlvet?method=del" method="post" onsubmit="return check()"> <table align="center" border=""> <tr> <td align="right">要删除的商品名称</td> <td><input type="text" id="id" name="id"></td> </tr> </table> <button type="submit" class="b">删 除</button> </form> <script type="text/javascript"> function check(){ var name = document.getElementById("id"); if(name.value == '') { alert('课程名称为空'); name.focus(); return false; } } </script> </div> </body> </html>
InWerahouse.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <% Object message = request.getAttribute("message"); if(message!=null && !"".equals(message)){ %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); </script> <%} %> <div> <div> <a href="Warehouse.jsp">返回首页</a> </div> <form action="Serlvet?method=inwarehouse" method="post" onsubmit="return check()"> <table> <tr><th align="center">填写入库单据</th></tr> <tr> <td align="right">商品名称</td> <td><input type="text" id="name" name="name"></td> </tr> <tr> <td align="right">生产厂家</td> <td><input type="text" id="changjia" name="changjia"></td> </tr> <tr> <td align="right">型号</td> <td><input type="text" id="xinghao" name="xinghao"></td> </tr> <tr> <td align="right">规格</td> <td><input type="text" id="guige" name="guige"></td> </tr> <tr> <td align="right">数量</td> <td><input type="text" id="num" name="num"></td> </tr> <tr> <td align="right">日期</td> <td><input type="text" id="date" name="date"></td> </tr> <tr> <td align="right">时间</td> <td><input type="text" id="time" name="time"></td> </tr> <tr> <td align="right">入(出)库单位名称</td> <td><input type="text" id="danwei" name="danwei"></td> </tr> <tr> <td align="right">送货(提货)人名称</td> <td><input type="text" id="people" name="people"></td> </tr> </table> <button type="submit">入 库</button> </form> </div> <script> function check(){ var name = document.getElementById("name"); var changjia = document.getElementById("changjia"); var xinghao = document.getElementById("xinghao"); var guige = document.getElementById("guige"); var num = document.getElementById("num"); var date = document.getElementById("date"); var time = document.getElementById("time"); var danwei = document.getElementById("danwei"); var people = document.getElementById("people"); if(name.value == '') { alert('商品名称为空'); name.focus(); return false; } if(changjia.value == '') { alert('生产厂家为空'); name.focus(); return false; } if(xinghao.value == '') { alert('型号为空'); name.focus(); return false; } if(guige.value == '') { alert('规格为空'); name.focus(); return false; } if(num.value == '') { alert('数量为空'); name.focus(); return false; } if(date.value == '') { alert('日期为空'); name.focus(); return false; } if(time.value == '') { alert('时间为空'); name.focus(); return false; } if(danwei.value == '') { alert('入(出)库单位名称为空'); name.focus(); return false; } if(people.value == '') { alert('送(提)货人名称为空'); name.focus(); return false; } } </script> </body> </html>
OutWerahouse.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <% Object message = request.getAttribute("message"); if(message!=null && !"".equals(message)){ %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); </script> <%} %> <div> <div> <a href="Warehouse.jsp">返回首页</a> </div> <form action="Serlvet?method=outwarehouse" method="post" onsubmit="return check()"> <table> <tr><th align="center">填写出库单据</th></tr> <tr> <td align="right">商品名称</td> <td><input type="text" id="name" name="name"></td> </tr> <tr> <td align="right">生产厂家</td> <td><input type="text" id="changjia" name="changjia"></td> </tr> <tr> <td align="right">型号</td> <td><input type="text" id="xinghao" name="xinghao"></td> </tr> <tr> <td align="right">规格</td> <td><input type="text" id="guige" name="guige"></td> </tr> <tr> <td align="right">数量</td> <td><input type="text" id="num" name="num"></td> </tr> <tr> <td align="right">日期</td> <td><input type="text" id="date" name="date"></td> </tr> <tr> <td align="right">时间</td> <td><input type="text" id="time" name="time"></td> </tr> <tr> <td align="right">入(出)库单位名称</td> <td><input type="text" id="danwei" name="danwei"></td> </tr> <tr> <td align="right">送货(提货)人名称</td> <td><input type="text" id="people" name="people"></td> </tr> </table> <button type="submit">出 库</button> </form> </div> <script> function check(){ var name = document.getElementById("name"); var changjia = document.getElementById("changjia"); var xinghao = document.getElementById("xinghao"); var guige = document.getElementById("guige"); var num = document.getElementById("num"); var date = document.getElementById("date"); var time = document.getElementById("time"); var danwei = document.getElementById("danwei"); var people = document.getElementById("people"); if(name.value == '') { alert('商品名称为空'); name.focus(); return false; } if(changjia.value == '') { alert('生产厂家为空'); name.focus(); return false; } if(xinghao.value == '') { alert('型号为空'); name.focus(); return false; } if(guige.value == '') { alert('规格为空'); name.focus(); return false; } if(num.value == '') { alert('数量为空'); name.focus(); return false; } if(date.value == '') { alert('日期为空'); name.focus(); return false; } if(time.value == '') { alert('时间为空'); name.focus(); return false; } if(danwei.value == '') { alert('入(出)库单位名称为空'); name.focus(); return false; } if(people.value == '') { alert('送(提)货人名称为空'); name.focus(); return false; } } </script> </body> </html>
Show.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@page import="java.util.List" %> <%@page import="com.Warehouse.Danju" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <div align="center"> <a href="Warehouse.jsp">返回首页</a> </div> <div align="center"> <table border='1'> <tr> <td>操作</td> <td>商品名称</td> <td>生产厂家</td> <td>型号</td> <td>规格</td> <td>数量</td> <td>日期</td> <td>时间</td> <td>入(出)库单位名称</td> <td>送(取)货人姓名</td> </tr> <%List<Danju> l = (List<Danju>)session.getAttribute("list");%> <%for(int i=0;i<l.size();i++){%> <tr> <td><%=l.get(i).getCaozuo()%></td> <td><%=l.get(i).getName()%></td> <td><%=l.get(i).getChangjia()%></td> <td><%=l.get(i).getXinghao()%></td> <td><%=l.get(i).getGuige()%></td> <td><%=l.get(i).getNum()%></td> <td><%=l.get(i).getDate()%></td> <td><%=l.get(i).getTime()%></td> <td><%=l.get(i).getDanwei()%></td> <td><%=l.get(i).getPeople()%></td> <%}%> </tr> </table> </div> </body> </html>
Search.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <div align="center"> <a href="Warehouse.jsp">返回主页</a> <form action="Serlvet?method=search" method="post" onsubmit="return check()"> <table> <tr><td>商品名称</td> <td><input type="text" id="name" name="name"/></td> </tr> <tr><td>出(入)库日期</td> <td><input type="text" id="date" name="date"/></td> </tr> </table> <button type="submit" >查 询</button> </form> </div> <script type="text/javascript"> function check() { var name = document.getElementById("name"); var date = document.getElementById("date"); //非空 if(name.value == '' && date.value == '' ) { alert('请填写一个条件'); return false; } } </script> </body> </html>