#数据库
CREATE table storage(
gdsid VARCHAR(20) PRIMARY key,
gdsname VARCHAR(100) not null,
gdsidate date not null,
godspdate date not null,
gdscount int not null,
stgname VARCHAR(100) not null
);
INSERT into storage VALUES (‘42322321993110’,‘扫描仪’,‘2018-10-01’,‘2019-01-20’,1,‘铁西网点’);
INSERT into storage VALUES (‘42322321993846’,‘易票365’,‘2018-10-01’,‘2019-01-05’,12,‘铁西网点’);
INSERT into storage VALUES (‘42322321993204’,‘联想电脑’,‘2018-10-01’,‘2019-01-12’,3,‘铁西网点’);
INSERT into storage VALUES (‘42322321993132’,‘显示器’,‘2018-10-01’,‘2019-01-21’,3,‘铁西网点’);
INSERT into storage VALUES (‘42322321993245’,‘视频课件’,‘2018-10-01’,‘2018-01-02’,18,‘铁西网点’);
INSERT into storage VALUES (‘42322321993778’,‘扫描器’,‘2018-10-01’,‘2018-12-25’,5,‘铁西网点’);
INSERT into storage VALUES (‘42322321993901’,‘打印机’,‘2018-10-01’,‘2018-04-23’,7,‘铁西网点’);
INSERT into storage VALUES (‘42322321993674’,‘色带盒’,‘2018-10-01’,‘2018-09-20’,4,‘铁西网点’);
INSERT into storage VALUES (‘42322321993552’,‘色带架’,‘2018-10-01’,‘2019-01-20’,5,‘铁西网点’);
#实体类
package com.neu.entity;
import java.util.Date;
public class Storage {
private String gdsid;
private String gdsname;
private Date gdsidate;
private Date godspdate;
private Integer gdscount;
private String stgname;
public Storage() {
super();
// TODO Auto-generated constructor stub
}
public Storage(String gdsid, String gdsname, Date gdsidate, Date godspdate, Integer gdscount, String stgname) {
super();
this.gdsid = gdsid;
this.gdsname = gdsname;
this.gdsidate = gdsidate;
this.godspdate = godspdate;
this.gdscount = gdscount;
this.stgname = stgname;
}
public String getGdsid() {
return gdsid;
}
public void setGdsid(String gdsid) {
this.gdsid = gdsid;
}
public String getGdsname() {
return gdsname;
}
public void setGdsname(String gdsname) {
this.gdsname = gdsname;
}
public Date getGdsidate() {
return gdsidate;
}
public void setGdsidate(Date gdsidate) {
this.gdsidate = gdsidate;
}
public Date getGodspdate() {
return godspdate;
}
public void setGodspdate(Date godspdate) {
this.godspdate = godspdate;
}
public Integer getGdscount() {
return gdscount;
}
public void setGdscount(Integer gdscount) {
this.gdscount = gdscount;
}
public String getStgname() {
return stgname;
}
public void setStgname(String stgname) {
this.stgname = stgname;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((gdscount == null) ? 0 : gdscount.hashCode());
result = prime * result + ((gdsid == null) ? 0 : gdsid.hashCode());
result = prime * result + ((gdsidate == null) ? 0 : gdsidate.hashCode());
result = prime * result + ((gdsname == null) ? 0 : gdsname.hashCode());
result = prime * result + ((godspdate == null) ? 0 : godspdate.hashCode());
result = prime * result + ((stgname == null) ? 0 : stgname.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Storage other = (Storage) obj;
if (gdscount == null) {
if (other.gdscount != null)
return false;
} else if (!gdscount.equals(other.gdscount))
return false;
if (gdsid == null) {
if (other.gdsid != null)
return false;
} else if (!gdsid.equals(other.gdsid))
return false;
if (gdsidate == null) {
if (other.gdsidate != null)
return false;
} else if (!gdsidate.equals(other.gdsidate))
return false;
if (gdsname == null) {
if (other.gdsname != null)
return false;
} else if (!gdsname.equals(other.gdsname))
return false;
if (godspdate == null) {
if (other.godspdate != null)
return false;
} else if (!godspdate.equals(other.godspdate))
return false;
if (stgname == null) {
if (other.stgname != null)
return false;
} else if (!stgname.equals(other.stgname))
return false;
return true;
}
@Override
public String toString() {
return "Storage [gdsid=" + gdsid + ", gdsname=" + gdsname + ", gdsidate=" + gdsidate + ", godspdate="
+ godspdate + ", gdscount=" + gdscount + ", stgname=" + stgname + "]";
}
}
#StorageDao.java
package com.neu.dao;
import java.util.List;
import com.neu.entity.Storage;
public interface StorageDao {
List<Storage> getAll() throws Exception;
Storage getByName(String gdsname) throws Exception;
int getUpdate(String gdsname,int gdscount) throws Exception;
}
#StorageDaoImpl.java
package com.neu.dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.neu.entity.Storage;
public class StorageDaoImpl implements StorageDao {
@Override
public List<Storage> getAll() throws Exception {
Connection connection = JDBCUtil.getConnection();
String sql = "select * from storage";
ResultSet rs = JDBCUtil.executeQuery(connection, sql, null);
List<Storage> list = new ArrayList<>();
Storage storage = null;
String gdsid;
String gdsname;
Date gdsidate;
Date godspdate;
Integer gdscount;
String stgname;
while(rs.next()) {
gdsid = rs.getString("gdsid");
gdsname = rs.getString("gdsname");
gdsidate = rs.getDate("gdsidate");
godspdate = rs.getDate("godspdate");
gdscount = rs.getInt("gdscount");
stgname = rs.getString("stgname");
storage = new Storage(gdsid, gdsname, gdsidate, godspdate, gdscount, stgname);
list.add(storage);
}
JDBCUtil.closeConnection(connection);
return list;
}
@Override
public Storage getByName(String gdsname) throws Exception {
Connection connection = JDBCUtil.getConnection();
String sql = "select * from storage where gdsname = ?";
ResultSet rs = JDBCUtil.executeQuery(connection, sql, new Object[] {gdsname});
Storage storage = null;
String gdsid;
Date gdsidate;
Date godspdate;
Integer gdscount;
String stgname;
if(rs.next()) {
gdsid = rs.getString("gdsid");
gdsidate = rs.getDate("gdsidate");
godspdate = rs.getDate("godspdate");
gdscount = rs.getInt("gdscount");
stgname = rs.getString("stgname");
storage = new Storage(gdsid, gdsname, gdsidate, godspdate, gdscount, stgname);
}
JDBCUtil.closeConnection(connection);
return storage;
}
@Override
public int getUpdate(String gdsname, int gdscount) throws Exception {
String sql = "update storage set gdscount = ? where gdsname = ?";
int n = JDBCUtil.executeUpdate(sql, new Object[] {gdscount,gdsname});
return n;
}
}
#StorageService.java
package com.neu.service;
import java.util.List;
import com.neu.entity.Storage;
public interface StorageService {
List<Storage> getAll() throws Exception;
Storage getByName(String gdsname) throws Exception;
int getUpdate(String gdsname,int gdscount) throws Exception;
}
#StorageServiceImpl.java
package com.neu.service;
import java.util.List;
import com.neu.dao.StorageDao;
import com.neu.dao.StorageDaoImpl;
import com.neu.entity.Storage;
public class StorageServiceImpl implements StorageService {
private StorageDao storageDao = new StorageDaoImpl();
@Override
public List<Storage> getAll() throws Exception {
// TODO Auto-generated method stub
return storageDao.getAll();
}
@Override
public Storage getByName(String gdsname) throws Exception {
// TODO Auto-generated method stub
return storageDao.getByName(gdsname);
}
@Override
public int getUpdate(String gdsname, int gdscount) throws Exception {
// TODO Auto-generated method stub
return storageDao.getUpdate(gdsname, gdscount);
}
}
#storagetopServlet.java
package com.neu.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.neu.entity.Storage;
import com.neu.service.StorageService;
import com.neu.service.StorageServiceImpl;
/**
* Servlet implementation class storagetopServlet
*/
@WebServlet("/storagetop")
public class storagetopServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String err = request.getParameter("err");
StorageService storageService = new StorageServiceImpl();
try {
List<Storage> list = storageService.getAll();
request.setAttribute("list", list);
if(err!=null) {
request.getRequestDispatcher("/WEB-INF/storage/showAll.jsp").forward(request, response);
}else {
request.getRequestDispatcher("/WEB-INF/storage/storage.jsp").forward(request, response);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
#storage.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 PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="">
<table align="center">
<tr><th colspan="2"><h1 style="color: green;">商品出库</h1></th></tr>
<tr>
<td align="right">请选择您要出库的商品:</td>
<td>
<select name="gdsname">
<option value="0">--请选择--</option>
<c:forEach items="${ list }" var="storage">
<option value="${ storage.gdsname }">${ storage.gdsname }</option>
</c:forEach>
</select>
</td>
</tr>
<tr>
<td align="right">请输入出库的数量:</td>
<td><input type="text" name="num" size="10"></td>
</tr>
<tr align="center">
<td colspan="2">
<input type="submit" value="确定">
<input type="reset" value="重置">
</td>
</tr>
<tr align="center">
<td colspan="2">
<a href="${ pageContext.request.contextPath }/WEB-INF/storage/showAll.jsp">查看库存信息表</a>
</td>
</tr>
</table>
</form>
</body>
</html>
#showAll.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table width="600" align="center">
<caption><h1 style="color: green;">库存信息如下</h1></caption>
<tr><td colspan="6"><hr size="1" color="grey"></td></tr>
<tr>
<th>货物编号</th>
<th>货物名称</th>
<th>生产日期</th>
<th>入库日期</th>
<th>所在仓库</th>
<th>库存数量</th>
</tr>
<c:forEach items="${ list }" var="storage">
<tr align="center">
<td>${ storage.gdsid }</td>
<td>${ storage.gdsname }</td>
<td><fmt:formatDate value="${ storage.gdsidate }" pattern="yyyy-MM-dd"/></td>
<td><fmt:formatDate value="${ storage.godspdate }" pattern="yyyy-MM-dd"/></td>
<td>${ storage.stgname }</td>
<td
<c:if test="${ storage.gdscount==0 }"> style="color: red;" </c:if>
>
${ storage.gdscount==0?"库存不足":storage.gdscount }
</td>
</tr>
</c:forEach>
<tr align="center">
<td colspan="6"><a href="${ pageContext.request.contextPath }/storagetop">退回出库页面</a></td>
</tr>
</table>
</body>
</html>
#storageUpdateServlet.java
package com.neu.servlet;
import java.io.IOException;
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.neu.entity.Storage;
import com.neu.service.StorageService;
import com.neu.service.StorageServiceImpl;
/**
* Servlet implementation class storageUpdateServlet
*/
@WebServlet("/storageUpdate")
public class storageUpdateServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String gdsname = request.getParameter("gdsname");
int num = Integer.parseInt(request.getParameter("num"));
StorageService storageService = new StorageServiceImpl();
try {
Storage storage = storageService.getByName(gdsname);
int gdscount = storage.getGdscount()-num;
request.setAttribute("storage", storage);
if(gdscount<0) {
request.getRequestDispatcher("/WEB-INF/storage/failure.jsp").forward(request, response);
}else {
storageService.getUpdate(gdsname, gdscount);
request.getRequestDispatcher("/WEB-INF/storage/successful.jsp").forward(request, response);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
#successful.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<div align="center">
<p><h1 style="color: green;">${ storage.gdsid }商品《${ storage.gdsname }》成功出库!</h1></p>
<br><br>
<p>点击<a href="${ pageContext.request.contextPath }/storagetop?err=b">返回</a>查看商品库存信息</p>
</div>
</body>
</html>
#failure.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<div align="center">
<p><h1 style="color: red;">出错啦:货物库存数量不足!</h1></p>
<table align="center" width="300">
<caption><b>编号为${ storage.gdsid }货物库存信息</b></caption>
<tr>
<td>货物名称:</td>
<td></td>
<td>${ storage.gdsname }</td>
</tr>
<tr>
<td>生产日期:</td>
<td></td>
<td><fmt:formatDate value="${ storage.gdsidate }" pattern="yyyy-MM-dd"/></td>
</tr>
<tr>
<td>入库日期:</td>
<td></td>
<td><fmt:formatDate value="${ storage.godspdate }" pattern="yyyy-MM-dd"/></td>
</tr>
<tr>
<td>所在仓库:</td>
<td></td>
<td>${ storage.stgname }</td>
</tr>
<tr>
<td>库存数量:</td>
<td></td>
<td>${ storage.gdscount }</td>
</tr>
</table>
<br>
<p>点击<a href="${ pageContext.request.contextPath }/storagetop?err=c">返回</a>查看商品库存信息</p>
</div>
</body>
</html>