实验步骤:
创建数据库Shop 创建数据表goods
GoodsDAO:
public class GoodsDAO {
public Goods findByGoodsId(String id) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
Goods goods = null;
conn = DBUtils.getConn();
try {
ps = conn.prepareStatement("select * from goods where goodsid=?");
ps.setString(1, id);
rs = ps.executeQuery();
if(rs.next()) {
goods = new Goods();
goods.setGoodsid(rs.getString(1));
goods.setGoodsname(rs.getString(2));
goods.setStorage(rs.getInt(3));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DBUtils.closeAll(null, ps, conn);
}
return goods;
}
public void updateGoods(String id,int outware) {
Connection conn = null;
PreparedStatement ps = null;
conn = DBUtils.getConn();
try {
ps = conn.prepareStatement("update goods set storage=storage-"+outware+"where goodsid = ?");
ps.setString(1,id);
ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DBUtils.closeAll(null, ps, conn);
}
}
public GoodsDAO() {
}
}
Goods:
package cc.home.entity;
public class Goods {
private String goodsid;
private String goodsname;
private int storage;
public String getGoodsid() {
return goodsid;
}
public void setGoodsid(String goodsid) {
this.goodsid = goodsid;
}
public String getGoodsname() {
return goodsname;
}
public void setGoodsname(String goodsname) {
this.goodsname = goodsname;
}
public int getStorage() {
return storage;
}
public void setStorage(int storage) {
this.storage = storage;
}
public Goods() {
// TODO Auto-generated constructor stub
}
}
GoodsService:
public class GoodsService {
GoodsDAO goodsDAO = new GoodsDAO();
public int outware(String id,int outware) {
Goods goods = goodsDAO.findByGoodsId(id);
if(goods == null) {
return 1;//没有商品
}else {
if(goods.getStorage()<outware) {
return 2;//库存不足
}else {
goodsDAO.updateGoods(id, outware);
return 3;//有商品,库存充足
}
}
}
public Goods findById(String id) {
return goodsDAO.findByGoodsId(id);
}
public GoodsService() {
// TODO Auto-generated constructor stub
}
}
DBUtils:
public class DBUtils {
private static String driver = "org.apache.derby.jdbc.EmbeddedDriver";
//private static String protocl = "jdbc:derby";
private static String dbName= "D:\\shop";
public static Connection getConn() {
Connection conn = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection("jdbc:derby:"+dbName+";create=true");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public static void closeAll(ResultSet rs,Statement st,Connection conn) {
if(rs!=null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(st!=null) {
try {
st.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn!=null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public DBUtils() {
// TODO Auto-generated constructor stub
}
}
doWith:
<%@ page language="java" import="cc.home.service.* , cc.home.entity.*" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String goodsid = request.getParameter("goodsid");
int outware = Integer.parseInt(request.getParameter("outware"));
GoodsService gs = new GoodsService();
int result = gs.outware(goodsid,outware);
if(result==1){
request.setAttribute("error","商品不存在!");
%>
<jsp:forward page="error.jsp"></jsp:forward>
<%
}else if(result==2){
request.setAttribute("error", "库存不足");
%>
<jsp:forward page="error.jsp"></jsp:forward>
<%
}else if(result==3){
Goods g = gs.findById(goodsid);
request.setAttribute("goods", g);
%>
<jsp:forward page="success.jsp"></jsp:forward>
<%} %>
</body>
</html>
error:
<%@ 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>
<h1><%=request.getAttribute("error") %></h1>
</body>
</html>
success:
<%@ page language="java" import="cc.home.entity.*" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Success!</h1>
<%Goods g = (Goods)request.getAttribute("goods"); %>
<ul>
<li>商品编号<%=g.getGoodsid() %></li>
<li>商品名称<%=g.getGoodsname() %></li>
<li>商品数量<%=g.getStorage() %></li>
</ul>
</body>
</html>
index:
<%@ 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="/MVCDemo/doWith.jsp" method="post">
<ul id="mytable">
<li>商品编号:<input type="text" name="goodsid"/></li>
<li>商品数量:<input type="text" name="outware"/></li>
<li><input type="submit" value="出库"/></li>
</ul>
</form>
</body>
</html>