要求根据输入商品名称,在数据库中查找该商品:
如果该商品为新增商品,则将商品信息添加如数据库表中;
如果该商品已经存在,则更新该商品的库存量,在原有库存量上增加输入的数量
(若已经存在,我判断了一下输入的商品类别和数据库中存储的是否一致,如不一致添加失败)
CREATE TABLE wareTable(
wareName VARCHAR(50) PRIMARY KEY,
wareSort int NOT NULL,
wareAmount int NOT NULL
)
package com.neu.entity;
public class Ware {
private String wareName;
private Integer wareSort;
private Integer wareAmount;
public Ware() {
super();
// TODO Auto-generated constructor stub
}
public Ware(String wareName, Integer wareSort, Integer wareAmount) {
super();
this.wareName = wareName;
this.wareSort = wareSort;
this.wareAmount = wareAmount;
}
public String getWareName() {
return wareName;
}
public void setWareName(String wareName) {
this.wareName = wareName;
}
public Integer getWareSort() {
return wareSort;
}
public void setWareSort(Integer wareSort) {
this.wareSort = wareSort;
}
public Integer getWareAmount() {
return wareAmount;
}
public void setWareAmount(Integer wareAmount) {
this.wareAmount = wareAmount;
}
}
package com.neu.dao;
import com.neu.entity.Ware;
public interface WareDao {
Ware getByName(String wareName) throws Exception;
int insert(Ware ware) throws Exception;
int update(String wareName,Integer amount) throws Exception;
}
package com.neu.dao;
import java.sql.Connection;
import java.sql.ResultSet;
import com.neu.entity.Ware;
public class WareDaoImpl implements WareDao {
@Override
public Ware getByName(String wareName) throws Exception {
Connection connection = JDBCUtil.getConnection();
String sql = "select * from wareTable where wareName = ?";
ResultSet rs = JDBCUtil.executeQuery(connection, sql, new Object[] {wareName});
Ware ware = null;
Integer wareSort;
Integer wareAmount;
if(rs.next()) {
wareSort = rs.getInt("wareSort");
wareAmount = rs.getInt("wareAmount");
ware = new Ware(wareName, wareSort, wareAmount);
}
JDBCUtil.closeConnection(connection);
return ware;
}
@Override
public int insert(Ware ware) throws Exception {
String sql = "insert into wareTable values(?,?,?)";
Object[] params = new Object[]{
ware.getWareName(),
ware.getWareSort(),
ware.getWareAmount()
};
int n = JDBCUtil.executeUpdate(sql, params);
return n;
}
@Override
public int update(String wareName, Integer amount) throws Exception {
String sql = "update wareTable set wareAmount = ? where wareName = ?";
int n = JDBCUtil.executeUpdate(sql, new Object[] {amount,wareName});
return n;
}
}
package com.neu.service;
import com.neu.entity.Ware;
public interface WareService {
int addWare(Ware ware) throws Exception;
}
package com.neu.service;
import com.neu.dao.WareDao;
import com.neu.dao.WareDaoImpl;
import com.neu.entity.Ware;
public class WareServiceImpl implements WareService {
//更新或插入成功后返回更新后的库存数量
private WareDao wareDao = new WareDaoImpl();
@Override
public int addWare(Ware ware) throws Exception {
Ware ware2 = wareDao.getByName(ware.getWareName());
if(ware2 == null) {
int n = wareDao.insert(ware);
if(n > 0) {
return ware.getWareAmount();
}else {
return -1;
}
}
if(ware2.getWareSort() != ware.getWareSort()) {
return -1;
}
Integer amount = ware.getWareAmount()+ware2.getWareAmount();
int n = wareDao.update(ware.getWareName(), amount);
if(n > 0) {
return amount;
}
return -1;
}
}
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.Ware;
import com.neu.service.WareService;
import com.neu.service.WareServiceImpl;
/**
* Servlet implementation class AddWareServlet
*/
@WebServlet("/AddWareServlet")
public class AddWareServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public AddWareServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String wareName = request.getParameter("wareName");
Integer wareSort = Integer.parseInt(request.getParameter("wareSort"));
Integer wareAmount = Integer.parseInt(request.getParameter("wareAmount"));
Ware ware = new Ware(wareName, wareSort, wareAmount);
WareService wareService = new WareServiceImpl();
try {
int n = wareService.addWare(ware);
if(n == -1) {
request.getRequestDispatcher("err.jsp").forward(request, response);
}else {
request.setAttribute("wareName", wareName);
request.setAttribute("oldAmount", wareAmount);
request.setAttribute("newAmount", n);
request.getRequestDispatcher("success.jsp").forward(request, response);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
<%@ 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>
<h1>录入商品库存信息</h1>
<form action="${ pageContext.request.contextPath }/AddWareServlet" method="post">
<table>
<tr>
<td>商品名称:</td>
<td>
<input type="text" name="wareName">
</td>
</tr>
<tr>
<td>商品类别:</td>
<td>
<label><input type="radio" name="wareSort" value="1">电器</label>
<label><input type="radio" name="wareSort" value="2">食品</label>
<label><input type="radio" name="wareSort" value="3">服装</label>
</td>
</tr>
<tr>
<td>商品数量:</td>
<td>
<input type="text" name="wareAmount">
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="加入库存">
<input type="reset" value="重置">
</td>
</tr>
</table>
</form>
</body>
</html>
<%@ 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>
<h1>添加库存成功</h1>
商品名称${ wareName }<br>
新增库存${ oldAmount }<br>
现共有库存${ newAmount }<br>
<a href="${ pageContext.request.contextPath }/index.jsp">返回上一页</a>
</body>
</html>
<%@ 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>
<h1>添加库存失败</h1>
<a href="${ pageContext.request.contextPath }/index.jsp">返回上一页</a>
</body>
</html>