库存商品管理机试题(JSP)——试题讲解
本试题使用Jsp+Servlet+Mysql+JDBC等技术实现库存商品管理。
语言和环境
A、实现语言
Java
B、环境要求
JDK1.8、Eclipse、Tomcat7、JSP、Servlet、Mysql
功能要求
编写一个添加更新库存的web程序。要求根据输入商品名称,在数据库中查找该商品:
如果该商品为新增商品,则将商品信息添加如数据库表中;
如果该商品已经存在,则更新该商品的库存量,在原有库存量上增加输入的数量
-
程序初始界面如图1所示:
图1:初始页面 -
输入商品名称、选择类别和商品数量,点击“加入库存”按钮,根据具体情况给出相应提示信息,如图2、图3、图4、图5所示:
图2:输入信息,点击“加入库存”按钮
图3:新增商品的情况
图4:商品已经存在,更新了库存量的情况
图5:添加失败的页面
3. 成功和失败的页面均有“返回上一页”,用以返回到初始页面
4. 商品的类别使用如下数字代替,写入数据库表中
电器——1
食品——2
服装——3
数据库设计
数据库名称:自定义
数据库表信息
源代码下载地址
https://download.csdn.net/download/pcbhyy/10776014
视频讲解及源代码地址https://download.csdn.net/download/pcbhyy/10776015
主要源代码
创建表语句
create table wareTable(
wareName VARCHAR(50) PRIMARY key,
wareSort int(4) not null,
wareAmount int(8) not null
)
工具类:JDBCUitls
package com.yy.dao;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
//重构
public class JDBCUitls {
private static String driverClassName;
private static String url;
private static String username;
private static String password;
static {
try {
Properties props = new Properties();
InputStream in = JDBCUitls.class.getResourceAsStream("/db.properties");
props.load(in);
driverClassName = props.getProperty("driverClassName");
url = props.getProperty("url");
username = props.getProperty("username");
password = props.getProperty("password");
Class.forName(driverClassName);
} catch (ClassNotFoundException | IOException e) {
e.printStackTrace();
}
}
//得到连接对象
public static Connection getConnection() throws SQLException {
Connection conn = DriverManager.getConnection(url,username,password);
return conn;
}
//关闭连接对象
public static void closeConnection(Connection conn) throws SQLException {
if(conn != null) {
conn.close();
}
}
public static int executeUpdate(String sql,Object... params) throws SQLException {
Connection conn = getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
if(params != null) {
//给占位符赋值
for(int i = 0;i < params.length;i++) {
ps.setObject(i+1, params[i]);
}
}
int n = ps.executeUpdate();
conn.close();
return n;
}
public static ResultSet executeQuery(Connection conn,String sql,Object... params) throws SQLException {
PreparedStatement ps = conn.prepareStatement(sql);
if(params != null) {
//给占位符赋值
for(int i = 0;i < params.length;i++) {
ps.setObject(i+1, params[i]);
}
}
ResultSet rs = ps.executeQuery();
return rs;
}
}
属性文件:db.properties(放在classpath路径下)
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mydata
username=root
password=root
数据访问层接口
package com.yy.dao;
import com.yy.entity.WareTable;
public interface WareTableDao {
public int insert(WareTable wareTable) throws Exception;
public int update(WareTable wareTable) throws Exception;
//根据商品的名称,得到该商品对象
public WareTable getWareByName(String wareName) throws Exception;
}
数据访问层实现类
package com.yy.dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.yy.entity.WareTable;
public class WareTableDaoImpl implements WareTableDao {
@Override
public int insert(WareTable wareTable) throws SQLException {
String sql = "insert into wareTable values(?,?,?)";
int n = JDBCUitls.executeUpdate(sql,
wareTable.getWareName(),
wareTable.getWareSort(),
wareTable.getWareAmount());
return n;
}
@Override
public int update(WareTable wareTable) throws SQLException {
String sql = "update wareTable set wareSort=?,wareAmount=? where wareName = ?";
int n = JDBCUitls.executeUpdate(sql,
wareTable.getWareSort(),
wareTable.getWareAmount(),
wareTable.getWareName());
return n;
}
@Override
public WareTable getWareByName(String wareName) throws SQLException {
String sql = "select * from wareTable where wareName = ?";
Connection conn = JDBCUitls.getConnection();
ResultSet rs = JDBCUitls.executeQuery(conn, sql, wareName);
int wareAmount = -1;
int wareSort;
WareTable wareTable = null;
if(rs.next()) {
wareSort = rs.getInt("wareSort");
wareAmount = rs.getInt("wareAmount");
wareTable = new WareTable(wareName, wareSort, wareAmount);
}
JDBCUitls.closeConnection(conn);
return wareTable;
}
}
实体类
package com.yy.entity;
public class WareTable {
private String wareName;
private Integer wareSort;
private Integer wareAmount;
public WareTable() {
super();
// TODO Auto-generated constructor stub
}
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;
}
}
业务逻辑层接口
package com.yy.service;
import com.yy.entity.WareTable;
public interface WareTableService {
public int insert(WareTable wareTable) throws Exception;
public int update(WareTable wareTable) throws Exception;
//根据商品的名称,得到该商品对象
public WareTable getWareByName(String wareName) throws Exception;
}
业务逻辑层实现类
package com.yy.service;
import com.yy.dao.WareTableDao;
import com.yy.dao.WareTableDaoImpl;
import com.yy.entity.WareTable;
public class WareTableServiceImpl implements WareTableService {
private WareTableDao wareTableDao = new WareTableDaoImpl();
@Override
public int insert(WareTable wareTable) throws Exception {
return wareTableDao.insert(wareTable);
}
@Override
public int update(WareTable wareTable) throws Exception {
return wareTableDao.update(wareTable);
}
@Override
public WareTable getWareByName(String wareName) throws Exception {
return wareTableDao.getWareByName(wareName);
}
}
Servlet
package com.yy.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.yy.entity.WareTable;
import com.yy.service.WareTableService;
import com.yy.service.WareTableServiceImpl;
/**
* Servlet implementation class WareTableServlet
*/
public class WareTableServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public WareTableServlet() {
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 method= request.getParameter("method");
try {
if("getAdd".equals(method)) {
doGetAdd(request,response);
}else if("save".equals(method)) {
doSave(request,response);
}
}catch(Exception e) {
e.printStackTrace();
}
}
private void doSave(HttpServletRequest request, HttpServletResponse response) throws Exception {
String wareName = request.getParameter("wareName");
int wareSort = Integer.parseInt(request.getParameter("wareSort"));
int wareAmount = Integer.parseInt(request.getParameter("wareAmount"));
WareTableService wareTableService = new WareTableServiceImpl();
WareTable wareTable = wareTableService.getWareByName(wareName);
int n;
if(wareTable == null) {
wareTable = new WareTable(wareName, wareSort, wareAmount);
n = wareTableService.insert(wareTable);
}else {
wareTable.setWareSort(wareSort);
wareTable.setWareAmount(wareAmount+wareTable.getWareAmount());
n = wareTableService.update(wareTable);
}
if(n == 1) {
request.setAttribute("newAmount", wareAmount);
request.setAttribute("wareTable", wareTable);
request.getRequestDispatcher("/WEB-INF/waretable/result.jsp").forward(request, response);
}else {
request.getRequestDispatcher("/WEB-INF/waretable/error.jsp").forward(request, response);
}
}
private void doGetAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("/WEB-INF/waretable/add.jsp").forward(request, response);
}
/**
* @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);
}
}
add.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 style="text-align: center;">
<h1>录入商品库存信息</h1>
<form action="${ pageContext.request.contextPath }/WareTableServlet?method=save" method="post">
<table border="0" width="400" align="center">
<tr>
<td>商品名称:</td>
<td align="left">
<input type="text" name="wareName">
</td>
</tr>
<tr>
<td>商品类别:</td>
<td align="left">
<input type="radio" name="wareSort" value="1" checked="checked">电器
<input type="radio" name="wareSort" value="2">食品
<input type="radio" name="wareSort" value="3">服装
</td>
</tr>
<tr>
<td>商品数量:</td>
<td align="left">
<input type="text" name="wareAmount">
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="加入库存">
<input type="reset" value="重置">
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
result.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>
<h1>添加库存成功</h1>
<p>商品名称:${ wareTable.wareName }</p>
<p>新增库存数:${ newAmount }</p>
<p>现共有库存:${ wareTable.wareAmount }</p>
<p><a href="javascript:window.history.go(-1);">返回上一页</a></p>
</body>
</html>
error.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>
<h1>添加库存失败</h1>
<p><a href="javascript:window.history.go(-1);">返回上一页</a></p>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>ware_project</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>WareTableServlet</display-name>
<servlet-name>WareTableServlet</servlet-name>
<servlet-class>com.yy.servlet.WareTableServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>WareTableServlet</servlet-name>
<url-pattern>/WareTableServlet</url-pattern>
</servlet-mapping>
</web-app>