实验要求:
1.、有一个存放商品的仓库,每天都有商品出库和入库。
2、每种商品都有名称、生产厂家、型号、规格等。
3、出入库时必须填写出入库单据,单据包括商品名称、生产厂家、型号、规格、数量、日期、时间、入库单位(或出库单位)名称、送货(或提货)人姓名。
本次实验商品的增删改比较容易实现,但是商品的出库入库单据实现对我来说有点困难,后来向做完的同学请教,发现首先要建两个表,方便对商品出入库单据的记录,然后还有别的东西等等,真的学到很多
1.首先建两个数据表,一个用来储存商品,一个用来记录商品出库入库数据 。
2.新建4个包,entity存放实体类(Warehouse),dao存放数据库操作类(WarehouseDao),servlet存放控制类(WarehouseServlet),service包为dao和Servlet的中间层,DButil存放开发帮助类(这里是数据库操作帮助类,封装了数据库连接部分代码,避免大量重复代码)
3.
//Warehouse.Java package com.hjf.entity; public class Warehouse { private int id; private String name; private String changjia; private String xinghao; private String guige; 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 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 Warehouse() {} public Warehouse(int id, String name, String changjia, String xinghao,String guige) { this.id = id; this.name = name; this.changjia=changjia; this.xinghao=xinghao; this.guige=guige; } public Warehouse(String name, String changjia, String xinghao,String guige) { this.name = name; this.changjia=changjia; this.xinghao=xinghao; this.guige=guige; } }
//warehouseDao.java 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.Warehouse; import com.hjf.util.DBUtil; /** * 商品Dao * Dao层操作数据 * @author Hu * */ public class WarehouseDao { /** * 添加 * @param course * @return */ public boolean add(Warehouse warehouse) { String sql = "insert into warehouse(name, changjia, xinghao,guige) values('" + warehouse.getName() + "','" + warehouse.getChangjia() + "','"+ 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) { 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 warehouse 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(Warehouse warehouse) { System.out.println(warehouse.getName()); String sql = "update warehouse set name='" + warehouse.getName() + "', changjia='" + warehouse.getChangjia()+ "', xinghao='" + warehouse.getXinghao()+ "', guige='" + warehouse.getGuige() + "' where id=" + warehouse.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; } /** * 通过ID得到商品信息 * @param id * @return */ public Warehouse getWarehouseById(int id) { String sql = "select * from warehouse where id =" + id; System.out.println("b"+id); Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; Warehouse warehouse = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { String name = rs.getString("name"); String changjia = rs.getString("changjia"); String xinghao = rs.getString("xinghao"); String guige = rs.getString("guige"); warehouse = new Warehouse(id, name, changjia, xinghao,guige); } } catch (Exception e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return warehouse; } /** * 通过name得到warehouse * @param name * @return */ public Warehouse getWarehouseByName(String name) { String sql = "select * from warehouse where name ='" + name + "'"; Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; Warehouse warehouse = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { int id = rs.getInt("id"); String changjia = rs.getString("changjia"); String xinghao = rs.getString("xinghao"); String guige = rs.getString("guige"); warehouse = new Warehouse(id, name, changjia, xinghao,guige); } } catch (Exception e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return warehouse; } /** * 查找 * @param name * @param teacher * @param classroom * @return */ public List<Warehouse> search(String name, String changjia, String xinghao) { String sql = "select * from warehouse where "; if (name != "") { sql += "name like '%" + name + "%'"; } if (changjia != "") { sql += "changjia like '%" + changjia + "%'"; } if (xinghao != "") { sql += "xinghao like '%" + xinghao + "%'"; } List<Warehouse> list = new ArrayList<>(); Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); Warehouse bean = null; while (rs.next()) { int id = rs.getInt("id"); String name2 = rs.getString("name"); String changjia2 = rs.getString("changjia"); String xinghao2 = rs.getString("xinghao"); String guige2=rs.getString("guige"); bean = new Warehouse(id, name2, changjia2, xinghao2,guige2); list.add(bean); } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return list; } /** * 全部数据 * @param name * @param teacher * @param classroom * @return */ public List<Warehouse> list() { String sql = "select * from warehouse"; List<Warehouse> list = new ArrayList<>(); Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); Warehouse bean = null; while (rs.next()) { int id = rs.getInt("id"); String name2 = rs.getString("name"); String changjia2 = rs.getString("changjia"); String xinghao2 = rs.getString("xinghao"); String guige2=rs.getString("guige"); bean = new Warehouse(id, name2, changjia2, xinghao2,guige2); list.add(bean); } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return list; } }
//WarehouseService.java package com.hjf.service; import java.util.List; import com.hjf.dao.WarehouseDao; import com.hjf.entity.Warehouse; /** * CourseService * 服务层 * @author Hu * */ public class WarehouseService { WarehouseDao wDao = new WarehouseDao(); /** * 添加 * @param course * @return */ public boolean add(Warehouse warehouse) { boolean f =wDao.add(warehouse); return f; } /** * 删除 */ public void del(int id) { wDao.delete(id); } /** * 修改 * @return */ public void update(Warehouse warehouse) { wDao.update(warehouse); } /** * 通过ID得到一个Course * @return */ public Warehouse getWarehouseById(int id) { return wDao.getWarehouseById(id); } /** * 通过Name得到一个Course * @return */ public Warehouse getWarehouseByName(String name) { return wDao.getWarehouseByName(name); } /** * 查找 * @return */ public List<Warehouse> search(String name, String changjia, String xinghao) { return wDao.search(name, changjia, xinghao); } /** * 全部数据 * @return */ public List<Warehouse> list() { return wDao.list(); } }
//WarehouseServlet.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.Warehouse; import com.hjf.service.WarehouseService; /** * Servlet implementation class WarehouseServlet */ @WebServlet("/WarehouseServlet") public class WarehouseServlet extends HttpServlet { private static final long serialVersionUID = 1L; WarehouseService service = new WarehouseService(); /** * 方法选择 */ protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String method = req.getParameter("method"); /*获取对应的请求参数 String method = request.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 ("getwarehousebyid".equals(method)) { getWarehouseById(req, resp); } else if ("getwarehousebyname".equals(method)) { getWarehouseByName(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 { req.setCharacterEncoding("utf-8"); //获取数据 String name = req.getParameter("name"); String changjia = req.getParameter("changjia"); String xinghao = req.getParameter("xinghao"); String guige = req.getParameter("guige"); Warehouse warehouse = new Warehouse(name, changjia, xinghao,guige); //添加后消息显示 if(service.add(warehouse)) { 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"); List<Warehouse> warehouses = service.list(); req.setAttribute("warehouses", warehouses); req.getRequestDispatcher("list.jsp").forward(req,resp); } /** * 通过ID得到Warehouse * @param req * @param resp * @throws ServletException */ private void getWarehouseById(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); int id = Integer.parseInt(req.getParameter("id"));//返回的String类型转换成int 类型 System.out.println("s"+id); Warehouse warehouse = service.getWarehouseById(id); req.setAttribute("warehouse", warehouse); req.getRequestDispatcher("detail2.jsp").forward(req,resp); } /** * 通过名字查找 * 跳转至删除 * @param req * @param resp * @throws IOException * @throws ServletException */ private void getWarehouseByName(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); String name = req.getParameter("name"); Warehouse warehouse = service.getWarehouseByName(name); if(warehouse == null) { req.setAttribute("message", "查无此商品!"); req.getRequestDispatcher("del.jsp").forward(req,resp); } else { req.setAttribute("warehouse", warehouse); 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{ 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{ req.setCharacterEncoding("utf-8"); int id = Integer.parseInt(req.getParameter("id")); System.out.println("c"+id); String name = req.getParameter("name"); String changjia = req.getParameter("changjia"); String xinghao = req.getParameter("xinghao"); String guige = req.getParameter("guige"); Warehouse warehouse = new Warehouse(id,name, changjia, xinghao,guige); service.update(warehouse); req.setAttribute("message", "修改成功"); req.getRequestDispatcher("WarehouseServlet?method=list").forward(req,resp); } /** * 查找 * @param req * @param resp * @throws ServletException */ private void search(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); String name = req.getParameter("name"); String changjia = req.getParameter("changjia"); String xinghao = req.getParameter("xinghao"); List<Warehouse> warehouses = service.search(name, changjia,xinghao); req.setAttribute("warehouses", warehouses); req.getRequestDispatcher("searchlist.jsp").forward(req,resp); } }
//DBUtil.java package com.hjf.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; /** * 数据库连接工具 * @author Hu * */ public class DBUtil { public static String db_url = "jdbc:mysql://localhost:3306/warehouse"; public static String db_user = "root"; public static String db_pass = "root"; 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(); } } } public static void main(String[] args) throws SQLException { Connection conn = getConn(); PreparedStatement pstmt = null; ResultSet rs = null; String sql ="select * from warehouse"; pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(); if(rs.next()){ System.out.println("空"); }else{ System.out.println("不空"); } } }
<!-- 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="WarehouseServlet?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="changjia" name="changjia" /> </div> <div class="a"> 商品型号<input type="text" id="xinghao" name="xinghao" /> </div> <div class="a"> 商品规格<input type="text" id="guige" name="guige" /> </div> <div class="a"> <button type="submit" class="b">保 存</button> </div> </form> </div> <script type="text/javascript"> function check() { var name = document.getElementById("name");; var changjia = document.getElementById("changjia"); var xinghao = document.getElementById("xinghao"); var guige = document.getElementById("guige"); //非空 if(name.value == '') { alert('商品名称为空'); name.focus(); return false; } if(changjia.value == '') { alert('厂家为空'); changjia.focus(); return false; } if(xinghao.value == '') { alert('型号为空'); xinghao.focus(); return false; } if(guige.value == '') { alert('规格为空'); guige.focus(); return false; } } </script> </body> </html>
<!-- del.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="WarehouseServlet?method=getwarehousebyname" 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>
<!-- detail.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> .a{ margin-top: 20px; } .b{ font-size: 20px; 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>${warehouse.name}</td> </tr> <tr> <td>商品厂家</td> <td>${warehouse.changjia}</td> </tr> <tr> <td>商品型号</td> <td>${warehouse.xinghao}</td> </tr> <tr> <td>商品规格</td> <td>${warehouse.guige}</td> </tr> </table> <div class="a"> <a onclick="return check()" href="WarehouseServlet?method=del&id=${warehouse.id}">删 除</a> </div> </div> <script type="text/javascript"> function check() { if (confirm("真的要删除吗?")){ return true; }else{ return false; } } </script> </body> </html>
<!-- detail2.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="WarehouseServlet?method=update" method="post" onsubmit="return check()"> <div class="a"> 商品名称<input type="text" id="name" name="name" value="${warehouse.name}"/> </div> <div class="a"> 商品厂家<input type="text" id="changjia" name="changjia" value="${warehouse.changjia}"/> </div> <div class="a"> 商品型号<input type="text" id="xinghao" name="xinghao" value="${warehouse.xinghao}"/> </div> <div class="a"> 商品规格<input type="text" id="guige" name="guige" value="${warehouse.guige}"/> </div> <input type="hidden" id="id" name="id" value="${warehouse.id}"/> <div class="a"> <button type="submit" class="b">修 改</button> </div> </form> </div> <script type="text/javascript"> function check() { var name = document.getElementById("name");; var changjia = document.getElementById("changjia"); var xinghao = document.getElementById("xinghao"); var guige = document.getElementById("guige"); //非空 if(name.value == '') { alert('课程名称为空'); name.focus(); return false; } if(changjia.value == '') { alert('厂家为空'); changjia.focus(); return false; } if(xinghao.value == '') { alert('型号为空'); xinghao.focus(); return false; } if(guige.value == '') { alert('规格为空'); guige.focus(); return false; } } </script> </body> </html>
<!-- index.jsp --> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>首页</title> </head> <body> <div align="center"> <div class="a"> <a href="add.jsp">商品信息添加</a> </div> <div class="a"> <a href="WarehouseServlet?method=list">商品信息修改</a> </div> <div class="a"> <a href="del.jsp">商品信息删除</a> </div> <div class="a"> <a href="search.jsp">商品信息查询</a> </div> </div> </body> </html>
<!-- list.jsp --> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!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> <table class="tb"> <tr> <td>id</td> <td>商品名称</td> <td>商品厂家</td> <td>商品型号</td> <td>商品规格</td> <td align="center" colspan="2">操作</td> </tr> <c:forEach items="${warehouses}" var="item"><!-- {}里面表示数组名 --> <tr> <td>${item.id}</td> <td>${item.name}</td> <td>${item.changjia}</td> <td>${item.xinghao}</td> <td>${item.guige}</td> <td><a href="WarehouseServlet?method=getwarehousebyid&id=${item.id}">修改</a></td> </tr> </c:forEach> </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"> <h1 style="color: black;">商品信息查询</h1> <a href="index.jsp">返回主页</a> <form action="WarehouseServlet?method=search" method="post" onsubmit="return check()"> <div class="a"> 商品名称<input type="text" id="name" name="name"/> </div> <div class="a"> 商品厂家<input type="text" id="changjia" name="changjia" /> </div> <div class="a"> 商品型号<input type="text" id="xinghao" name="xinghao" /> </div> <div class="a"> <button type="submit" class="b">查 询</button> </div> </form> </div> <script type="text/javascript"> function check() { var name = document.getElementById("name"); var changjia = document.getElementById("changjia"); var xinghao = document.getElementById("xinghao"); //非空 if(name.value == '' && changjia.value == '' && xinghao.value == '') { alert('请填写一个条件'); return false; } } </script> </body> </html>
<!-- searchlist.jsp --> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <div align="center"> <h1 style="color: black;">商品信息列表</h1> <a href="index.jsp">返回主页</a> <table class="tb"> <tr> <td>id</td> <td>商品名称</td> <td>商品厂家</td> <td>商品型号</td> </tr> <!-- forEach遍历出adminBeans --> <c:forEach items="${warehouses}" var="item" varStatus="status"> <tr> <td>${item.id}</td> <td><a>${item.name}</a></td> <td>${item.changjia}</td> <td>${item.xinghao}</td> <td>${item.guige}</td> </tr> </c:forEach> </table> </div> </body> </html>
最后运行结果如下: