仓库管理系统
建两张数据表,一张用来商品的增删改查,一张记录商品的出库与入库信息
建两个实体类,用来传递商品与单据的信息
在添加与删除时,填写入库或者出库单据,入库时增加商品,出库时删除商品。course类写商品的增删改查,另一个dao类只写单据的添加,自后将两张表连接起来
//course.dao package com.hjf.entity; public class Course { private int id; private String name; private String sname; private String xinghao; private String size; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getXinghao() { return xinghao; } public void setXinghao(String shape) { this.xinghao = shape; } public String getSize() { return size; } public void setSize(String size) { this.size = size; } public Course() {} public Course(int id, String name, String sname, String xinghao, String size) { this.id = id; this.name = name; this.sname =sname; this.xinghao = xinghao; this.size=size; } public Course(String name, String sname, String xinghao, String size) { this.name = name; this.sname =sname; this.xinghao = xinghao; this.size=size; } }
//danju.java package com.hjf.entity; public class danju { private int id; private String name; private String sname; private String xinghao; private String size; private String pname; private String rname; private int number; private String riqi; private String shijian; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getXinghao() { return xinghao; } public void setXinghao(String xinghao) { this.xinghao = xinghao; } public String getSize() { return size; } public void setSize(String size) { this.size = size; } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } public String getRiqi() { return riqi; } public void setRiqi(String riqi) { this.riqi = riqi; } public String getShijian() { return shijian; } public void setShijian(String shijian) { this.shijian = shijian; } public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } public String getRname() { return rname; } public void setRname(String rname) { this.rname = rname; } public danju(int id, String name, String sname, String xinghao, String size,int number,String riqi,String shijian,String rname,String pname) { this.id = id; this.name = name; this.sname =sname; this.xinghao = xinghao; this.size=size; this.number=number; this.riqi=riqi; this.shijian=shijian; this.rname=rname; this.pname=pname; } public danju( String name, String sname, String xinghao, String size,int number,String riqi,String shijian,String rname,String pname) { this.name = name; this.sname =sname; this.xinghao = xinghao; this.size=size; this.number=number; this.riqi=riqi; this.shijian=shijian; this.rname=rname; this.pname=pname; } }
//course.dao package com.hjf.dao; 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.hjf.entity.Course; import com.hjf.entity.danju; import com.hjf.util.DBUtil; /** * 课程Dao * Dao层操作数据 * @author Hu * */ public class CourseDao { /** * 添加 * @param course * @return */ public boolean add(Course course,danju danju1) { String sql = "insert into course(name, sname, xinghao,size) values('" + course.getName() + "','" + course.getSname() + "','" + course.getXinghao() + "','" + course.getSize() + "')"; String sql1 = "insert into danju(name, sname, xinghao,size,number,riqi,shijian,rname,pname) values('" + danju1.getName() + "','" + danju1.getSname() + "','" + danju1.getXinghao() + "','" + danju1.getSize() + "','" + danju1.getNumber() + "','" + danju1.getRiqi() + "','" + danju1.getShijian() + "','" + danju1.getRname() + "','" + danju1.getPname() + "')"; //创建数据库链接 Connection conn = DBUtil.getConn(); Statement state = null; boolean f = false; int a = 0; try { state = conn.createStatement(); state.executeUpdate(sql); state.executeUpdate(sql1); } catch (Exception e) { e.printStackTrace(); } finally { //关闭连接 DBUtil.close(state, conn); } if (a > 0) { f = true; } return f; } /** * 删除 * * @param id * @return */ public boolean delete (int id) { boolean f = false; String sql = "delete from course where id='" + 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; } /** * 修改 * @param name * @param pass */ public boolean update(Course course) { String sql = "update course set name='" + course.getName() + "', sname='" + course.getSname() + "', xinghao='" + course.getXinghao()+ "', size='" + course.getSize() + "' where id='" + course.getId() + "'"; Connection conn = DBUtil.getConn(); Statement state = null; boolean f = false; 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; } /** * 验证课程名称是否唯一 * true --- 不唯一 * @param name * @return */ public boolean name(String name) { boolean flag = false; String sql = "select name from course where name = '" + name + "'"; Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { flag = true; } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return flag; } /** * 通过ID得到课程信息 * @param id * @return */ public Course getCourseById(int id) { String sql = "select * from course where id ='" + id + "'"; Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; Course course = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { String name = rs.getString("name"); String sname = rs.getString("sname"); String xinghao = rs.getString("xinghao"); String size = rs.getString("size"); course = new Course(id, name, sname, xinghao,size); } } catch (Exception e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return course; } /** * 通过name得到Course * @param name * @return */ public Course getCourseByName(String name) { String sql = "select * from course where name ='" + name + "'"; Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; Course course = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { int id = rs.getInt("id"); String sname = rs.getString("sname"); String xinghao = rs.getString("xinghao"); String size = rs.getString("size"); course = new Course(id, name, sname, xinghao,size); } } catch (Exception e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return course; } /** * 查找 * @param name * @param teacher * @param classroom * @return */ public List<danju> search(String name) { String sql = "select * from jilu where "; if (name != "") { sql += "name like '%" + name + "%'"; } 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()) { int id = rs.getInt("id"); int number = rs.getInt("number"); String name2 = rs.getString("name"); String sname = rs.getString("sname"); String xinghao = rs.getString("xinghao"); String size = rs.getString("size"); String rname = rs.getString("rname"); String pname = rs.getString("pname"); String riqi = rs.getString("riqi"); String shijian = rs.getString("shijian"); bean = new danju(id,name2,sname,xinghao,size,number,riqi,shijian,rname,pname); list.add(bean); } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return list; } /** * 全部数据 * @param name * @param sname * @param xinghao * @param size * @return */ public List<Course> list() { String sql = "select * from course"; List<Course> list = new ArrayList<>(); Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); Course bean = null; while (rs.next()) { int id = rs.getInt("id"); String name = rs.getString("name"); String sname = rs.getString("sname"); String xinghao = rs.getString("xinghao"); String size = rs.getString("size"); bean = new Course(id, name,sname,xinghao,size); list.add(bean); } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return list; } }
//courseservlet package com.hjf.servlet; 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 com.hjf.entity.Course; import com.hjf.entity.danju; import com.hjf.service.CourseService; @WebServlet("/CourseServlet") public class CourseServlet extends HttpServlet { private static final long serialVersionUID = 1L; CourseService service = new CourseService(); /** * 方法选择 */ protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html;charset=utf-8"); 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 ("update".equals(method)) { update(req, resp); } else if ("search".equals(method)) { search(req, resp); } else if ("getcoursebyid".equals(method)) { getCourseById(req, resp); } else if ("getcoursebyname".equals(method)) { getCourseByName(req, resp); } else if ("list".equals(method)) { list(req, resp); } } /** * 添加 * @param req * @param resp * @throws IOException * @throws ServletException */ private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { resp.setContentType("text/html;charset=utf-8"); req.setCharacterEncoding("utf-8"); //获取数据 String name = req.getParameter("name"); String sname = req.getParameter("sname"); String xinghao = req.getParameter("xinghao"); String size = req.getParameter("size"); int number = Integer.parseInt(req.getParameter("number")); String riqi = req.getParameter("riqi"); String shijian = req.getParameter("shijian"); String rname = req.getParameter("rname"); String pname = req.getParameter("pname"); Course course = new Course(name, sname, xinghao,size); danju danju1=new danju( name,sname,xinghao,size,number,riqi,shijian,rname,pname); //添加后消息显示 if(service.add(course,danju1)) { req.setAttribute("message", "添加成功"); req.getRequestDispatcher("add.jsp").forward(req,resp); } else { req.setAttribute("message", "商品名称重复,请重新录入"); req.getRequestDispatcher("add.jsp").forward(req,resp); } } /** * 全部 * @param req * @param resp * @throws ServletException */ private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); resp.setContentType("text/html;charset=utf-8"); List<Course> courses = service.list(); req.setAttribute("courses", courses); req.getRequestDispatcher("list.jsp").forward(req,resp); } /** * 通过ID得到Course * @param req * @param resp * @throws ServletException */ private void getCourseById(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ resp.setContentType("text/html;charset=utf-8"); req.setCharacterEncoding("utf-8"); int id = Integer.parseInt(req.getParameter("id")); Course course = service.getCourseById(id); req.setAttribute("course", course); req.getRequestDispatcher("detail2.jsp").forward(req,resp); } /** * 通过名字查找 * 跳转至删除 * @param req * @param resp * @throws IOException * @throws ServletException */ private void getCourseByName(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ resp.setContentType("text/html;charset=utf-8"); req.setCharacterEncoding("utf-8"); String name = req.getParameter("name"); Course course = service.getCourseByName(name); if(course == null) { req.setAttribute("message", "查无此商品!"); req.getRequestDispatcher("del.jsp").forward(req,resp); } else { req.setAttribute("course", course); req.getRequestDispatcher("detail.jsp").forward(req,resp); } } /** * 删除 * @param req * @param resp * @throws IOException * @throws ServletException */ private void del(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ resp.setContentType("text/html;charset=utf-8"); req.setCharacterEncoding("utf-8"); int id = Integer.parseInt(req.getParameter("id")); service.del(id); req.setAttribute("message", "删除成功!"); req.getRequestDispatcher("del.jsp").forward(req,resp); } /** * 修改 * @param req * @param resp * @throws IOException * @throws ServletException */ private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ resp.setContentType("text/html;charset=utf-8"); req.setCharacterEncoding("utf-8"); int id = Integer.parseInt(req.getParameter("id")); String name = req.getParameter("name"); String sname = req.getParameter("sname"); String xinghao = req.getParameter("xinghao"); String size = req.getParameter("size"); Course course = new Course(id, name, sname, xinghao,size); service.update(course); req.setAttribute("message", "修改成功"); req.getRequestDispatcher("CourseServlet?method=list").forward(req,resp); } /** * 查找 * @param req * @param resp * @throws ServletException */ private void search(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ resp.setContentType("text/html;charset=utf-8"); req.setCharacterEncoding("utf-8"); String name = req.getParameter("name"); List<danju> danju1 = service.search(name); req.setAttribute("daju1", danju1); req.getRequestDispatcher("searchlist.jsp").forward(req,resp); } }
//DBU.java package com.hjf.servlet; 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 com.hjf.entity.Course; import com.hjf.entity.danju; import com.hjf.service.CourseService; @WebServlet("/CourseServlet") public class CourseServlet extends HttpServlet { private static final long serialVersionUID = 1L; CourseService service = new CourseService(); /** * 方法选择 */ protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html;charset=utf-8"); 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 ("update".equals(method)) { update(req, resp); } else if ("search".equals(method)) { search(req, resp); } else if ("getcoursebyid".equals(method)) { getCourseById(req, resp); } else if ("getcoursebyname".equals(method)) { getCourseByName(req, resp); } else if ("list".equals(method)) { list(req, resp); } } /** * 添加 * @param req * @param resp * @throws IOException * @throws ServletException */ private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { resp.setContentType("text/html;charset=utf-8"); req.setCharacterEncoding("utf-8"); //获取数据 String name = req.getParameter("name"); String sname = req.getParameter("sname"); String xinghao = req.getParameter("xinghao"); String size = req.getParameter("size"); int number = Integer.parseInt(req.getParameter("number")); String riqi = req.getParameter("riqi"); String shijian = req.getParameter("shijian"); String rname = req.getParameter("rname"); String pname = req.getParameter("pname"); Course course = new Course(name, sname, xinghao,size); danju danju1=new danju( name,sname,xinghao,size,number,riqi,shijian,rname,pname); //添加后消息显示 if(service.add(course,danju1)) { req.setAttribute("message", "添加成功"); req.getRequestDispatcher("add.jsp").forward(req,resp); } else { req.setAttribute("message", "商品名称重复,请重新录入"); req.getRequestDispatcher("add.jsp").forward(req,resp); } } /** * 全部 * @param req * @param resp * @throws ServletException */ private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); resp.setContentType("text/html;charset=utf-8"); List<Course> courses = service.list(); req.setAttribute("courses", courses); req.getRequestDispatcher("list.jsp").forward(req,resp); } /** * 通过ID得到Course * @param req * @param resp * @throws ServletException */ private void getCourseById(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ resp.setContentType("text/html;charset=utf-8"); req.setCharacterEncoding("utf-8"); int id = Integer.parseInt(req.getParameter("id")); Course course = service.getCourseById(id); req.setAttribute("course", course); req.getRequestDispatcher("detail2.jsp").forward(req,resp); } /** * 通过名字查找 * 跳转至删除 * @param req * @param resp * @throws IOException * @throws ServletException */ private void getCourseByName(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ resp.setContentType("text/html;charset=utf-8"); req.setCharacterEncoding("utf-8"); String name = req.getParameter("name"); Course course = service.getCourseByName(name); if(course == null) { req.setAttribute("message", "查无此商品!"); req.getRequestDispatcher("del.jsp").forward(req,resp); } else { req.setAttribute("course", course); req.getRequestDispatcher("detail.jsp").forward(req,resp); } } /** * 删除 * @param req * @param resp * @throws IOException * @throws ServletException */ private void del(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ resp.setContentType("text/html;charset=utf-8"); req.setCharacterEncoding("utf-8"); int id = Integer.parseInt(req.getParameter("id")); service.del(id); req.setAttribute("message", "删除成功!"); req.getRequestDispatcher("del.jsp").forward(req,resp); } /** * 修改 * @param req * @param resp * @throws IOException * @throws ServletException */ private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ resp.setContentType("text/html;charset=utf-8"); req.setCharacterEncoding("utf-8"); int id = Integer.parseInt(req.getParameter("id")); String name = req.getParameter("name"); String sname = req.getParameter("sname"); String xinghao = req.getParameter("xinghao"); String size = req.getParameter("size"); Course course = new Course(id, name, sname, xinghao,size); service.update(course); req.setAttribute("message", "修改成功"); req.getRequestDispatcher("CourseServlet?method=list").forward(req,resp); } /** * 查找 * @param req * @param resp * @throws ServletException */ private void search(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ resp.setContentType("text/html;charset=utf-8"); req.setCharacterEncoding("utf-8"); String name = req.getParameter("name"); List<danju> danju1 = service.search(name); req.setAttribute("daju1", danju1); req.getRequestDispatcher("searchlist.jsp").forward(req,resp); } }
//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> </head> <body> <% Object message = request.getAttribute("message"); if(message!=null && !"".equals(message)){ %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); </script> <%} %> <div align="center"> <h1 style="color: black;">商品单据信息录入</h1> <a href="index.jsp">返回主页</a> <form action="CourseServlet?method=add" method="post" onsubmit="return check()"> <div class="a"> 商品名称<input type="text" id="name" name="name"/> </div> <div class="a"> 生产厂家<input type="text" id="sname" name="sname" /> </div> <div class="a"> 型号<input type="text" id="xinghao" name="xinghao" /> </div> <div class="a"> 规格<input type="text" id="size" name="size" /> </div> <div class="a"> 数量<input type="text" id="number" name="number" /> </div> <div class="a"> 日期<input type="text" id="riqi" name="riqi" /> </div> <div class="a"> 时间<input type="text" id="shijian" name="shijian" /> </div> <div class="a"> 单位<input type="text" id="rname" name="rname" /> </div> <div class="a"> 姓名<input type="text" id="pname" name="pname" /> </div> <div class="a"> <button type="submit" class="b">保 存</button> </div> </form> </div> <script type="text/javascript"> function check() { var name = document.getElementById("name");; //非空 if(name.value == '') { alert('商品名称为空'); name.focus(); return false; } } </script> </body> </html>
<%@ 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"> <h1 style="color: black;">商品信息删除</h1> <a href="index.jsp">返回主页</a> <form action="CourseServlet?method=getcoursebyname" method="post" onsubmit="return check()"> <div class="a"> 商品名称<input type="text" id="name" name="name"/> </div> <div class="a"> <button type="submit" class="b">查 找</button> </div> </form> </div> <script type="text/javascript"> function check() { var name = document.getElementById("name");; //非空 if(name.value == '') { alert('课程名称为空'); name.focus(); return false; } } </script> </body> </html>
<%@ 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> .a{ margin-top: 20px; } .b{ font-size: 20px; width: 160px; color: white; background-color: greenyellow; } .tb, td { border: 1px solid black; font-size: 22px; } </style> </head> <body> <div align="center"> <h1 style="color: black;">课程信息删除</h1> <a href="index.jsp">返回主页</a> <table class="tb"> <tr> <td>商品名称</td> <td>${course.name}</td> </tr> <tr> <td>出产厂家</td> <td>${course.sname}</td> </tr> <tr> <td>型号</td> <td>${course.xinghao}</td> </tr> <tr> <td>规格</td> <td>${course.size}</td> </tr> </table> <div class="a"> <a onclick="return check()" href="CourseServlet?method=del&id=${course.id}" >删 除</a> </div> </div> <script type="text/javascript"> function check() { if (confirm("真的要删除吗?")){ return true; }else{ return false; } } </script> </body> </html>