1、背景资料:
1、有一个存放商品的仓库,每天都有商品出库和入库。
2、每种商品都有名称、生产厂家、型号、规格等。
3、出入库时必须填写出入库单据,单据包括商品名称、生产厂家、型号、
规格、数量、日期、时间、入库单位(或出库单位)名称、送货(或提货)人
姓名。
2.系统要求与功能设计
2.1 页面要求
(1)能够在 Tomcat 服务器中正确部署,并通过浏览器查看;(1 分)
(2)网站页面整体风格统一;
2.2 设计要求
1、设计出入库单据的录入。
2、实现按商品名称、出入库日期的查询。
一、商品的增删改查
dao层
与数据库连接有关的代码
package dao; 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/eshop?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT"; public static String db_user="root"; public static String db_password="password"; public static Connection getConn() { Connection conn=null; try { Class.forName("com.mysql.cj.jdbc.Driver"); conn=DriverManager.getConnection(db_url, db_user, db_password); System.out.println("连接数据库成功"); }catch(Exception e) { System.out.println("连接数据库失败"); e.printStackTrace(); } return 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(); } } } }
entity层
package entity; public class Goods{ private String name; private String changshang; private String xinghao; private String guige; public String getname() { return name; } public void setname(String name) { this.name=name; } public String getchangshang() { return changshang; } public void setchangshang(String changshang) { this.changshang=changshang; } 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; } }
Servlet层
package GoodsServlet; import java.io.IOException; import java.io.UnsupportedEncodingException; 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 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 dao.DBUtil; import entity.Goods; @SuppressWarnings("serial") public class GoodsServlet extends HttpServlet { protected void service(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException{ req.setCharacterEncoding("utf-8"); String method = req.getParameter("method"); String name = req.getParameter("name"); if("insert".equals(method)) insert(req, resp); else if("delete".equals(method)) delete(req, resp); else if("update".equals(method)) update(req, resp); else if("query".equals(method)) query(req, resp); } private void query(HttpServletRequest req, HttpServletResponse resp) { try { req.setCharacterEncoding("UTF-8"); HttpSession session = req.getSession(); String name = req.getParameter("name"); String changshang = req.getParameter("changshang"); String xinghao = req.getParameter("xinghao"); String guige=req.getParameter("guige"); Connection conn = DBUtil.getConn(); Statement st = conn.createStatement(); List<Goods> list = new ArrayList<>(); ResultSet rs = null; if(name!="" && changshang=="" && xinghao==""&&guige=="") rs = queryname(name, st, conn); else if(name=="" && changshang!="" && xinghao==""&&guige=="") rs = querychangshang(changshang, st, conn); else if(name=="" && changshang=="" && xinghao!=""&&guige=="") rs = queryxinghao(xinghao, st, conn); else if(name=="" && changshang=="" && xinghao==""&&guige!="") rs = queryguige(guige, st, conn); if(rs!=null) { Goods cb = null; while(rs.next()) { cb = new Goods(); cb.setname(rs.getString("name")); cb.setchangshang(rs.getString("changshang")); cb.setxinghao(rs.getString("xinghao")); cb.setguige(rs.getString("guige")); list.add(cb); } session.setAttribute("list",list); DBUtil.close(st, conn); resp.sendRedirect(req.getContextPath() + "/admin/query.jsp?status=1"); } else { DBUtil.close(st, conn); resp.sendRedirect(req.getContextPath() + "/admin/query.jsp?status=0"); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } private ResultSet querychangshang(String changshang, Statement st, Connection conn) { ResultSet rs = null; try { String sql = "select * from goods where changshang='"+ changshang + "'"; rs = st.executeQuery(sql); } catch (SQLException e) { e.printStackTrace(); } return rs; } private ResultSet queryxinghao(String xinghao, Statement st, Connection conn) { ResultSet rs = null; try { String sql = "select * from goods where xinghao='"+ xinghao + "'"; rs = st.executeQuery(sql); } catch (SQLException e) { e.printStackTrace(); } return rs; } private ResultSet queryguige(String guige, Statement st, Connection conn) { ResultSet rs = null; try { String sql = "select * from goods where guige='"+ guige + "'"; rs = st.executeQuery(sql); } catch (SQLException e) { e.printStackTrace(); } return rs; } private ResultSet queryname(String name, Statement st, Connection conn) { ResultSet rs = null; try { String sql = "select * from goods where name='"+ name + "'"; rs = st.executeQuery(sql); } catch (SQLException e) { e.printStackTrace(); } return rs; } @SuppressWarnings("unused") private void update(HttpServletRequest req, HttpServletResponse resp) { try { req.setCharacterEncoding("utf-8"); //String name = req.getParameter("oldname"); String newname = req.getParameter("newname"); //String changshang = req.getParameter("oldchangshang"); String newchangshang = req.getParameter("newchangshang"); //String xinghao = req.getParameter("oldxinghao"); String newxinghao=req.getParameter("newxinghao"); //String guige=req.getParameter("oldguige"); String newguige=req.getParameter("newguige"); Connection conn = DBUtil.getConn(); Statement st = conn.createStatement(); String sql = "update goods set name='"+ newname + "',changshang='" + newchangshang + "',xinghao='" + newxinghao + "',guige='" + newguige+"'"/*+"where name='"+ name + "' and changshang='" + changshang + "' and xinghao='" + xinghao + "' and guige='"+"'"*/; st.executeUpdate(sql); resp.sendRedirect(req.getContextPath() + "/goods.jsp"); DBUtil.close(st, conn); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } private void delete(HttpServletRequest req, HttpServletResponse resp) { try { req.setCharacterEncoding("UTF-8"); String name = req.getParameter("name"); Connection conn = DBUtil.getConn(); Statement st = conn.createStatement(); String sql = "select * from goods where name='"+ name + "'"; ResultSet rs = st.executeQuery(sql); if(rs.next()) { sql = "delete from goods where name = '" + name + "'"; st.executeUpdate(sql); resp.sendRedirect(req.getContextPath() + "/goods.jsp"); DBUtil.close(st, conn); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } private void insert(HttpServletRequest req, HttpServletResponse resp) { try { req.setCharacterEncoding("utf-8"); String name=req.getParameter("name"); String changshang=req.getParameter("changshang"); String xinghao=req.getParameter("xinghao"); String guige=req.getParameter("guige"); Connection conn = DBUtil.getConn(); Statement st = conn.createStatement(); String sql="insert into goods values('"+name+"','"+changshang+"','"+xinghao+"','"+guige+"')"; st.executeUpdate(sql); resp.sendRedirect(req.getContextPath() + "/goods.jsp"); DBUtil.close(st, conn); }catch(Exception e) { e.getStackTrace(); } } }
页面
主页面
<%@ 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>
<ul class="topmenu">
<li><a href="insert.jsp" target="iframe_a">增加</a></li>
<li><a href="delete.jsp" target="iframe_a">删除</a></li>
<li><a href="modify.jsp" target="iframe_a">修改</a></li>
<li><a href="Query.jsp" target="iframe_a">查询</a></li>
</ul>
</body>
</html>
增
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>添加页面</title> </head> <body> <form action="${pageContext.request.contextPath}/admin/Manage?method=insert" method="post" id="insert"> <table border='1'> <tr align='center'> <td colspan='2'><h2>添加商品</h2></td> </tr> <tr align='center'> <td>商品名称</td> <td><input name="name" type="text" id="name" placeholder="商品名称" value="" /></td> </tr> <tr align='center'> <td>生产厂商</td> <td><input name="changshang" type="text" id="changshang" placeholder="生产厂商" value="" /></td> </tr> <tr align='center'> <td>型号</td> <td><input name="xinghao" type="text" id="xinghao" placeholder="型号" value="" /></td> </tr> <tr align='center'> <td>规格</td> <td><input name="guige" type="text" id="guige" placeholder="规格" value="" /></td> </tr> <tr align='center'> <td colspan='2'><input type="submit"value="保存" /></td> </tr> </table> </form> </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>
<form action="${pageContext.request.contextPath}/admin/Manage?method=delete" method="post" id="delete">
<table border='1'>
<tr align='center'>
<td colspan='2'><h2>删除商品</h2></td>
</tr>
<tr align='center'>
<td>商品名称</td>
<td><input name="name" type="text" id="name" placeholder="商品名称" value="" /></td>
</tr>
<tr align='center'>
<td>生产厂商</td>
<td><input name="changshang" type="text" id="changshang" placeholder="生产厂商" value="" /></td>
</tr>
<tr align='center'>
<td>型号</td>
<td><input name="xinghao" type="text" id="xinghao" placeholder="型号" value="" /></td>
</tr>
<tr align='center'>
<td>规格</td>
<td><input name="guige" type="text" id="guige" placeholder="规格" value="" /></td>
</tr>
<tr align='center'>
<td colspan='2'><input type="submit"value="删除" /></td>
</tr>
</table>
</form>
</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>
<form action="${pageContext.request.contextPath}/admin/Manage?method=update" method="post" id="insert">
<table border='1'>
<tr align='center'>
<td colspan='3'><h2>修改</h2></td>
</tr>
<tr align='center'>
<td>商品名称</td>
<td><input name="newname" type="text" id="newname" placeholder="新的商品名称" value="" /></td>
</tr>
<tr align='center'>
<td>生产厂商</td>
<td><input name="newchangshang" type="text" id="newchangshang" placeholder="新的生产厂商" value="" /></td>
</tr>
<tr align='center'>
<td>型号</td>
<td><input name="newxinghao" type="text" id="newxinghao" placeholder="新的型号" value="" /></td>
</tr>
<tr align='center'>
<td>规格</td>
<td><input name="newguige" type="text" id="neqguige" placeholder="新的规格" value="" /></td>
</tr>
<tr align='center'>
<td colspan='3'><input type="submit"value="修改" /></td>
</tr>
</table>
</form>
</body>
</html>
查
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.sql.*,java.util.*,entity.Goods" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <form action="${pageContext.request.contextPath}/admin/Manage?method=query" method="post" id="insert"> <table border='1'> <tr align='center'> <td colspan='2'><h2>查询</h2></td> </tr> <tr align='center'> <td>商品名称</td> <td><input name="name" type="text" id="name" placeholder="商品名称" value="" /></td> </tr> <tr align='center'> <td>生产厂商</td> <td><input name="changshang" type="text" id="changshang" placeholder="生产厂商" value="" /></td> </tr> <tr align='center'> <td>型号</td> <td><input name="xinghao" type="text" id="xinghao" placeholder="型号" value="" /></td> </tr> <tr align='center'> <td>规格</td> <td><input name="guige" type="text" id="guige" placeholder="规格" value="" /></td> </tr> <tr align='center'> <td colspan='2'><input type="submit" value="查询"/></td> </tr> </table> <c:if test="${param.status.equals('0')}"> <br/><br/><br/> <div>商品信息不存在!</div> </c:if> <c:if test="${param.status.equals('1')}"> <br/><br/><br/> <table border='1'> <tr> <td>商品名称</td> <td>生产厂商</td> <td>型号</td> <td>规格</td> </tr> <%List<Goods> l = (List<Goods>)session.getAttribute("list"); for(int i=0;i<l.size();i++){%> <tr> <td><%=l.get(i).getname()%></td> <td><%=l.get(i).getchangshang()%></td> <td><%=l.get(i).getxinghao()%></td> <td><%=l.get(i).getguige()%></td> <%}%> </tr> </table> </c:if> </form> </body> </html>
二,出入库
entity层
package entity; public class Inout { private String name; private String changshang; private String xinghao; private String guige; private String date; private String quliaty; private String time; private String danwei; private String peoplename; public String getname() { return name; } public void setname(String name) { this.name=name; } public String getchangshang() { return changshang; } public void setchangshang(String changshang) { this.changshang=changshang; } 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 getdate() { return date; } public void setdate(String date) { this.date=date; } public String getquliaty() { return quliaty; } public void getquliaty(String quliaty) { this.quliaty=quliaty; } 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 getpeoplename() { return peoplename; } public void setpeoplename(String peoplename) { this.peoplename=peoplename; } }
Servelet层
package GoodsServlet; import java.io.UnsupportedEncodingException; import java.sql.Connection; import java.sql.Statement; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import dao.DBUtil; @SuppressWarnings("serial") @WebServlet("/Input") public class Input extends HttpServlet { protected void service(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException{ req.setCharacterEncoding("utf-8"); String method = req.getParameter("method"); if("insert".equals(method)) insert(req, resp); else if("query".equals(method)) query(req, resp); } @SuppressWarnings("unused") private void insert(HttpServletRequest req, HttpServletResponse resp) { try { req.setCharacterEncoding("utf-8"); String name=req.getParameter("name"); String changshang=req.getParameter("changshang"); String xinghao=req.getParameter("xinghao"); String guige=req.getParameter("guige"); String date=req.getParameter("date"); String quality =req.getParameter("quality"); String time=req.getParameter("time"); String danwei=req.getParameter("danwei"); String peoplename=req.getParameter("peoplename"); Connection conn = DBUtil.getConn(); Statement st = conn.createStatement(); String sql="insert into inout values('"+name+"','"+changshang+"','"+xinghao+"','"+guige+"','"+date+"','"+quality+"','"+time+"','"+danwei+"','"+peoplename+"')"; st.executeUpdate(sql); resp.sendRedirect(req.getContextPath() + "/inout.jsp"); DBUtil.close(st, conn); }catch(Exception e) { e.getStackTrace(); } } private void query(HttpServletRequest req, HttpServletResponse resp) { // TODO 自动生成的方法存根 } }
页面
主页面
<%@ 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>
<ul class="topmenu">
<li><a href="insert1.jsp" >增加</a></li>
<li><a href="Query.jsp" >查询</a></li>
</ul>
</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>
<form action="${pageContext.request.contextPath}/admin/Manage1?method=insert" method="post" id="insert">
<table border='1'>
<tr align='center'>
<td colspan='2'><h2>添加商品</h2></td>
</tr>
<tr align='center'>
<td>商品名称</td>
<td><input name="name" type="text" id="name" placeholder="商品名称" value="" /></td>
</tr>
<tr align='center'>
<td>生产厂商</td>
<td><input name="changshang" type="text" id="changshang" placeholder="生产厂商" value="" /></td>
</tr>
<tr align='center'>
<td>型号</td>
<td><input name="xinghao" type="text" id="xinghao" placeholder="型号" value="" /></td>
</tr>
<tr align='center'>
<td>规格</td>
<td><input name="guige" type="text" id="guige" placeholder="规格" value="" /></td>
</tr>
<tr align='center'>
<td>日期</td>
<td><input name="time" type="text" id="time" placeholder="日期" value="" /></td>
</tr>
<tr align='center'>
<td>数量</td>
<td><input name="quality" type="text" id="quality" placeholder="数量" value="" /></td>
</tr>
<tr align='center'>
<td>时间</td>
<td><input name="time" type="text" id="time" placeholder="数量" value="" /></td>
</tr>
<tr align='center'>
<td>单位</td>
<td><input name="danwei" type="text" id="danwei" placeholder="单位" value="" /></td>
</tr>
<tr align='center'>
<td>送提货人姓名</td>
<td><input name="peoplename" type="text" id="peoplename" placeholder="送提货人姓名" value="" /></td>
</tr>
<tr align='center'>
<td colspan='2'><input type="submit"value="保存" /></td>
</tr>
</table>
</form>
</body>
</html>