package com.neu.entity;
public class Ware {
public String wareName;
public Integer wareSort;
public Integer wareAmount ;
public Ware() {
super();
}
public Ware(String wareName, Integer wareSort, Integer wareAmount) {
super();
this.wareName = wareName;
this.wareSort = wareSort;
this.wareAmount = wareAmount;
}
@Override
public String toString() {
return "Ware [wareName=" + wareName + ", wareSort=" + wareSort + ", wareAmount=" + wareAmount + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((wareAmount == null) ? 0 : wareAmount.hashCode());
result = prime * result + ((wareName == null) ? 0 : wareName.hashCode());
result = prime * result + ((wareSort == null) ? 0 : wareSort.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;
Ware other = (Ware) obj;
if (wareAmount == null) {
if (other.wareAmount != null)
return false;
} else if (!wareAmount.equals(other.wareAmount))
return false;
if (wareName == null) {
if (other.wareName != null)
return false;
} else if (!wareName.equals(other.wareName))
return false;
if (wareSort == null) {
if (other.wareSort != null)
return false;
} else if (!wareSort.equals(other.wareSort))
return false;
return true;
}
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 {
int insert(Ware ware) throws Exception;
int update(String wareName,Integer add) throws Exception;
Ware getByWare(String wareName) throws Exception;
}
package com.neu.dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.List;
import com.neu.entity.Ware;
public class WareDaoImpl implements WareDao {
@Override
public int insert(Ware ware) throws Exception {
String wareName = ware.getWareName();
Integer wareSort = ware.getWareSort();
Integer wareAmount = ware.getWareAmount();
Object[] params = {wareName,wareSort,wareAmount};
String sql="insert into waretable(wareName,wareSort,wareAmount) values(?,?,?)";
int n = JDBCUtil.executeUpdate(sql,params);
return n;
}
@Override
public int update(String wareName, Integer add) throws Exception {
Object[] params = {add,wareName};
String sql="update waretable set wareAmount=wareAmount+? where wareName=?";
int n = JDBCUtil.executeUpdate(sql,params);
return n;
}
@Override
public Ware getByWare(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);
}
return ware;
}
}
package com.neu.service;
import com.neu.entity.Ware;
public interface WareService {
Ware getByWare(String wareName) throws Exception;
int overall(String wareName,int add,int wareSort) 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 {
WareDao waredao=new WareDaoImpl();
@Override
public Ware getByWare(String wareName) throws Exception {
return waredao.getByWare(wareName);
}
@Override
public int overall(String wareName,int add,int wareSort) throws Exception {
Ware ware = waredao.getByWare(wareName);
if(wareName=="") {
return -1;
}
if(ware==null) {
ware=new Ware(wareName,wareSort,add);
int n = waredao.insert(ware);
return 1;
}else if(ware!=null) {
int n = waredao.update(wareName, add);
return 2;
}
return 0;
}
}
<%@ 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>
<form action="${pageContext.request.contextPath }/WareServlet" method="post">
<table>
<tr>
<td colspan="2">录入商品库存信息</td>
</tr>
<tr>
<td>商品名称:</td>
<td><input type="text" name="wareName" ></td>
</tr>
<td>商品名称:</td>
<td><input type="radio" name="wareSort" value="1" >电器<br>
<input type="radio" name="wareSort" value="2" >食品<br>
<input type="radio" name="wareSort" value="3" >服装<br>
</td>
</tr>
<td>商品数量:</td>
<td><input type="text" name="wareAmount" ></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="加入库存">
<input type="reset" value="重置">
</tr>
</table>
</form>
</body>
</html>
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;
@WebServlet("/WareServlet")
public class WareServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String wareName = request.getParameter("wareName");
int add=Integer.parseInt(request.getParameter("wareAmount")) ;
int wareSort = Integer.parseInt(request.getParameter("wareSort"));
WareService wareService=new WareServiceImpl();
try {
String msg="";
int n = wareService.overall(wareName,add,wareSort);
if(n==1) {
msg="添加库存成功";
}else if(n==2) {
msg="更新库存成功";
}else {
msg="更新库存失败";
request.getRequestDispatcher("/WEB-INF/ware/error.jsp").forward(request, response);
}
request.setAttribute("msg", msg);
Ware ware = wareService.getByWare(wareName);
request.setAttribute("ware", ware);
request.setAttribute("add",add);
request.getRequestDispatcher("/WEB-INF/ware/rs.jsp").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
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>
${msg}<br>
商品名称:${ware.wareName}<br>
新增库存:${add }<br>
现有库存:${ware.wareAmount}<br>
<a href="${pageContext.request.contextPath }/warebegin.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>
<form action="" method="post">
<table>
<tr>
<td colspan="2">录入商品库存信息</td>
</tr>
<tr>
<td>商品名称:</td>
<td><input type="text" name="wareName" ></td>
</tr>
<td>商品名称:</td>
<td><input type="radio" name="wareSort" value="1" >1<br>
<input type="radio" name="wareSort" value="2" >2<br>
<input type="radio" name="wareSort" value="3" >3<br>
</td>
</tr>
<td>商品数量:</td>
<td><input type="text" name="wareAmount" ></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="加入库存">
<input type="reset" value="重置">
</tr>
</table>
</form>
</body>
</html>