题目:添加更新库存
一、 语言和环境
a) 实现语言
java
b) 环境要求
eclipse,Oracle
二、 数据库设计
数据库名称:自定义
数据库表信息
表名 wareTable
主键 wareName
序号 字段名称 字段说明 类型 位数 属性 备注
1 wareName 商品名称 Varchar2 50 非空 主键
2 wareSort 商品类别 Number(4) 4 非空
3 wareAmount 库存量 Number(8) 8 非空
三、 要求
利用jsp、Javabean、JDBC技术,编写一个添加更新库存的web程序。要求根据输入商品名称,在数据库中查找该商品:
如果该商品为新增商品,则将商品信息添加如数据库表中;
如果该商品已经存在,则更新该商品的库存量,在原有库存量上增加输入的数量
1、 程序初始界面如图1所示:
图1:初始页面
2、 输入商品名称、选择类别和商品数量,点击“加入库存”按钮,根据具体情况给出相应提示信息,如图2、图3、图4、图5所示:
##1.创建数据库
CREATE TABLE waretable
(
wareName
varchar(50) NOT NULL,
wareSort
int(11) NOT NULL,
wareAmount
int(11) NOT NULL,
PRIMARY KEY (wareName
)
)
##实体类
package com.neu.entity;
public class WareTable {
private String wareName;
private Integer wareSort;
private Integer wareAmount;
public WareTable() {
super();
// TODO 自动生成的构造函数存根
}
public WareTable(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;
}
@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;
WareTable other = (WareTable) 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;
}
@Override
public String toString() {
return "WareTable [wareName=" + wareName + ", wareSort=" + wareSort + ", wareAmount=" + wareAmount + "]";
}
}
#wareTableDao的实现类
package com.neu.dao;
import java.sql.Connection;
import java.sql.ResultSet;
import com.neu.entity.WareTable;
public class WareTableDaoImpl implements WareTableDao {
@Override
public WareTable 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});
WareTable wareTable = null;
if(rs.next()) {
int wareSort = rs.getInt("wareSort");
int wareAmount = rs.getInt("wareAmount");
wareTable = new WareTable(wareName, wareSort, wareAmount);
}
JDBCUtil.closeConnection(connection);
return wareTable;
}
@Override
public int getInsert(WareTable wareTable) throws Exception {
String sql= "insert into waretable values(?,?,?)";
int n = JDBCUtil.executeUpdate(sql, new Object[] {wareTable.getWareName(),wareTable.getWareSort(),wareTable.getWareAmount()});
return n;
}
@Override
public int getUpdate(String wareName, int wareAmount) throws Exception {
String sql = "update waretable set wareAmount = ? where wareName = ?";
int n = JDBCUtil.executeUpdate(sql, new Object[] {wareAmount,wareName});
return n;
}
}
#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.WareTable;
import com.neu.service.WareTableService;
import com.neu.service.WareTableServiceImpl;
/**
* Servlet implementation class selectServlet
*/
@WebServlet("/selectServlet")
public class selectServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String wareName = request.getParameter("wareName");
int wareAmount =Integer.parseInt(request.getParameter("wareAmount"));
int wareSort =Integer.parseInt(request.getParameter("wareSort"));
WareTableService wareTableService = new WareTableServiceImpl();
WareTable wareTable =null;
try {
wareTable = wareTableService.getByName(wareName);
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
//查询信息
String msg =null;
if(wareTable !=null) {
if(wareSort != wareTable.getWareSort()) {
msg = "商品类别错误!\n请重新选择!";
response.sendRedirect(request.getContextPath()+"/selware.jsp?msg="+msg);
return;
}
//更改信息
try {
msg = "修改成功!";
int n = wareTableService.getUpdate(wareName, (wareAmount+wareTable.getWareAmount()));
response.sendRedirect(request.getContextPath()+"/selware.jsp?msg="+msg);
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}else {
msg = "无此商品,请添加!";
request.getRequestDispatcher("/WEB-INF/ware/insware.jsp?msg="+msg).forward(request, response);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
#2.
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.WareTable;
import com.neu.service.WareTableService;
import com.neu.service.WareTableServiceImpl;
/**
* Servlet implementation class insertServlet
*/
@WebServlet("/insertServlet")
public class insertServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String wareName = request.getParameter("wareName");
int wareAmount =Integer.parseInt(request.getParameter("wareAmount"));
int wareSort =Integer.parseInt(request.getParameter("wareSort"));
WareTable wareTable = new WareTable(wareName, wareSort, wareAmount);
WareTableService wareTableService = new WareTableServiceImpl();
try {
wareTableService.getInsert(wareTable);
request.getRequestDispatcher("/selware.jsp").forward(request, response);
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
#3.selware.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>
<c:if test="${!(empty param.msg) }">
<script type="text/javascript">
alert("${param.msg}");
</script>
</c:if>
<form action="${pageContext.request.contextPath }/selectServlet" method="post">
<table border="1" >
<tr><th colspan="2"><h1>录入商品库存信息</h1></th></tr>
<tr>
<td>商品名称:</td>
<td>
<input type="text" name="wareName">
</td>
</tr>
<tr>
<td>商品类别:</td>
<td>
<input type="radio" name="wareSort" value="1">电器
<input type="radio" name="wareSort" value="2">食品
<input type="radio" name="wareSort" value="3">服装
</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>
#4.insware.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>
<c:if test="${!(empty param.msg) }">
<script type="text/javascript">
alert("${param.msg}");
</script>
</c:if>
<form action="${pageContext.request.contextPath }/insertServlet" method="post">
<table>
<tr>
<td>商品名称:</td>
<td>
<input type="text" name="wareName">
</td>
</tr>
<tr>
<td>商品类别:</td>
<td>
<input type="radio" name="wareSort" value="1">电器
<input type="radio" name="wareSort" value="2">食品
<input type="radio" name="wareSort" value="3">服装
</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>