大家好,又见面了,我是你们的朋友全栈君。
用JSP servlet和jquery以及SQLServer数据库实现商品销售管理系统。
功能详解: 1.查询商品 2.添加商品 3.删除商品 4.添加销售记录 5.查看销售记录
效果演示:
进入界面
商品显示页面
删除提示
销售记录查询
商品销售系统需要SQLServer数据库和程序两大部分
一:设计并实现数据库
二:程序设计
在程序正式开始之前先看一下目录结构吧
==特别注意:==引入sqljdbc4.jar和jquery-3.3.1.js架包
程序演示
bean包
Product.java
代码语言:javascript复制package com.hnpi.bean;
public class Product {
private int ProductID;
private String ProductName;
private String IsUp;
private int UnitPrice;
private String Remark;
public int getProductID() {
return ProductID;
}
public void setProductID(int productID) {
ProductID = productID;
}
public String getProductName() {
return ProductName;
}
public void setProductName(String productName) {
ProductName = productName;
}
public String getIsUp() {
return IsUp;
}
public void setIsUp(String isUp) {
IsUp = isUp;
}
public int getUnitPrice() {
return UnitPrice;
}
public void setUnitPrice(int unitPrice) {
UnitPrice = unitPrice;
}
public String getRemark() {
return Remark;
}
public void setRemark(String remark) {
Remark = remark;
}
public Product() {
super();
// TODO Auto-generated constructor stub
}
public Product(int productID, String productName, String isUp,
int unitPrice, String remark) {
super();
ProductID = productID;
ProductName = productName;
IsUp = isUp;
UnitPrice = unitPrice;
Remark = remark;
}
}
Sale.java
代码语言:javascript复制package com.hnpi.bean;
import java.util.Date; public class Sale {
private int SaleID;
private String SalePerson;
private int Amount;
private Date SaleDate;
private int ProductID; public int getSaleID() {
return SaleID;
}
public void setSaleID(int saleID) {
SaleID = saleID;
}
public String getSalePerson() {
return SalePerson;
}
public void setSalePerson(String salePerson) {
SalePerson = salePerson;
}
public int getAmount() {
return Amount;
}
public void setAmount(int amount) {
Amount = amount;
}
public Date getSaleDate() {
return SaleDate;
}
public void setSaleDate(Date saleDate) {
SaleDate = saleDate;
}
public int getProductID() {
return ProductID;
}
public void setProductID(int productID) {
ProductID = productID;
}
public Sale() {
super();
// TODO Auto-generated constructor stub
}
public Sale(int saleID, String salePerson, int amount, Date saleDate, int productID) {
super();
SaleID = saleID;
SalePerson = salePerson;
Amount = amount;
SaleDate = saleDate;
ProductID = productID;
}
}
servlet包
AddServlet.java
代码语言:javascript复制package com.hnpi.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.hnpi.bean.Product;
import com.hnpi.util.DBUtil;
public class AddServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String ProductName = request.getParameter("ProductName");
String IsUp = request.getParameter("IsUp");
String UnitPrice = request.getParameter("UnitPrice");
String Remark = request.getParameter("Remark");
Connection conn = DBUtil.getConn();
PreparedStatement ps = null;
String sql = "insert into Product(ProductName, IsUp, UnitPrice, Remark) values(?,?,?,?)";
try {
ps = conn.prepareStatement(sql);
ps.setString(1, ProductName);
ps.setString(2, IsUp);
ps.setInt(3, Integer.parseInt(UnitPrice));
ps.setString(4, Remark);
ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
DBUtil.closeConn(conn, ps, null);
}
response.sendRedirect("list");
}
}
DeleteServlet.java
代码语言:javascript复制package com.hnpi.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.hnpi.util.DBUtil;
public class DeleteServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String id = request.getParameter("id");
Connection conn = DBUtil.getConn();
PreparedStatement ps = null;
String sql = "delete from Product where ProductID = ?";
try {
ps = conn.prepareStatement(sql);
ps.setInt(1, Integer.parseInt(id));
ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
DBUtil.closeConn(conn, ps, null);
}
response.sendRedirect("list");
}
}
InsertServlet.java
代码语言:javascript复制package com.hnpi.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Currency;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.hnpi.util.DBUtil;
public class InsertServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String ProductName = request.getParameter("ProductName");
String Amount = request.getParameter("Amount");
String SalePerson = request.getParameter("SalePerson");
Connection conn = DBUtil.getConn();
PreparedStatement ps = null;
String sql = "insert into Sale(SalePerson, Amount, SaleDate, ProductID) values(?,?,?,?)";
try {
ps = conn.prepareStatement(sql);
ps.setString(1, SalePerson);
ps.setInt(2, Integer.parseInt(Amount));
ps.setTimestamp(3, new Timestamp(System.currentTimeMillis()));
ps.setInt(4, 3);
ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
DBUtil.closeConn(conn, ps, null);
}
response.sendRedirect("select");
}
}
ListChoseServlet
代码语言:javascript复制package com.hnpi.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.hnpi.bean.Product;
import com.hnpi.util.DBUtil;
public class ListChoseServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String ProductName = request.getParameter("ProductName");
String Remark = request.getParameter("Remark");
Connection conn = DBUtil.getConn();
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select * from Product where ProductName = ? and Remark = ?";
List<Product> products = new ArrayList<Product>();
try {
ps = conn.prepareStatement(sql);
ps.setString(1, ProductName);
ps.setString(1, Remark);
rs = ps.executeQuery();
while(rs.next()){
Product product = new Product();
product.setProductID(rs.getInt(1));
product.setProductName(rs.getString(2));
product.setIsUp(rs.getString(3));
product.setUnitPrice(rs.getInt(4));
product.setRemark(rs.getString(5));
products.add(product);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
DBUtil.closeConn(conn, ps, rs);
}
HttpSession session = request.getSession();
session.setAttribute("productList", products);
response.sendRedirect("list.jsp");
}
}
ListServlet.java
代码语言:javascript复制package com.hnpi.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.hnpi.bean.Product;
import com.hnpi.util.DBUtil;
import com.sun.net.httpserver.HttpsServer;
public class ListServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String ProductName = request.getParameter("ProductName");
String Remark = request.getParameter("Remark");
Connection conn = DBUtil.getConn();
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select * from Product";
List<Product> products = new ArrayList<Product>();
try {
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()){
Product product = new Product();
product.setProductID(rs.getInt(1));
product.setProductName(rs.getString(2));
product.setIsUp(rs.getString(3));
product.setUnitPrice(rs.getInt(4));
product.setRemark(rs.getString(5));
products.add(product);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
DBUtil.closeConn(conn, ps, rs);
}
HttpSession session = request.getSession();
session.setAttribute("productList", products);
response.sendRedirect("list.jsp");
}
}
SelectServlet.java
代码语言:javascript复制package com.hnpi.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.hnpi.bean.Product;
import com.hnpi.bean.Sale;
import com.hnpi.util.DBUtil;
public class SelectServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
Connection conn = DBUtil.getConn();
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select * from Sale";
List<Sale> sales = new ArrayList<Sale>();
try {
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()){
Sale sale = new Sale();
sale.setSaleID(rs.getInt(1));
sale.setSalePerson(rs.getString(2));
sale.setAmount(rs.getInt(3));
sale.setSaleDate(rs.getDate(4));
sale.setProductID(rs.getInt(5));
sales.add(sale);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
DBUtil.closeConn(conn, ps, rs);
}
HttpSession session = request.getSession();
session.setAttribute("saleList", sales);
response.sendRedirect("select.jsp");
}
}
util包
DBUtil.java
代码语言:javascript复制package com.hnpi.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtil {
public static Connection getConn(){
String url = "jdbc:sqlserver://localhost:1433;databaseName=Test";
String user = "sa";
String pwd = "1";
Connection conn = null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection(url, user, pwd);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public static void closeConn(Connection conn, PreparedStatement ps, ResultSet rs){
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(ps!=null){
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
WEB-INF包
web.xml
代码语言:javascript复制<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>Shop</display-name>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>AddServlet</servlet-name>
<servlet-class>com.hnpi.servlet.AddServlet</servlet-class>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>DeleteServlet</servlet-name>
<servlet-class>com.hnpi.servlet.DeleteServlet</servlet-class>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>InsertServlet</servlet-name>
<servlet-class>com.hnpi.servlet.InsertServlet</servlet-class>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>ListChoseServlet</servlet-name>
<servlet-class>com.hnpi.servlet.ListChoseServlet</servlet-class>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>ListServlet</servlet-name>
<servlet-class>com.hnpi.servlet.ListServlet</servlet-class>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>SelectServlet</servlet-name>
<servlet-class>com.hnpi.servlet.SelectServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AddServlet</servlet-name>
<url-pattern>/add</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>DeleteServlet</servlet-name>
<url-pattern>/del</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>InsertServlet</servlet-name>
<url-pattern>/insert</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ListChoseServlet</servlet-name>
<url-pattern>/listes</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ListServlet</servlet-name>
<url-pattern>/list</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>SelectServlet</servlet-name>
<url-pattern>/select</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
JSP页面
index.jsp
代码语言:javascript复制<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() "://" request.getServerName() ":" request.getServerPort() path "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<ul>
<li><a href="index.jsp">系统管理</a></li>
<li><a href="list">商品管理</a></li>
</ul>
</body>
</html>
list.jsp
代码语言:javascript复制<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="com.hnpi.bean.Product"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() "://" request.getServerName() ":" request.getServerPort() path "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'list.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script src="jquery-3.3.1.js"></script>
</head>
<body>
<form action="listes" method="post">
商品名称:<input type="text" name="ProductName">
商品备注:<input type="text" name="Remark">
<input type="submit" value="查询">
</form>
<table>
<thead>
<tr>
<td>编号</td>
<td>商品名称</td>
<td>是否上架</td>
<td>单价</td>
<td>备注</td>
<td>操作</td>
</tr>
</thead>
<tbody>
<%
List<Product> products = (ArrayList)session.getAttribute("productList"); for(Product product : products){
%>
<tr>
<td><%=product.getProductID() %></td>
<td><%=product.getProductName() %></td>
<td><%=product.getIsUp() %></td>
<td><%=product.getUnitPrice() %></td>
<td><%=product.getRemark() %></td>
<td><a href="del?id=<%=product.getProductID() %>" class="delete">删除</a></td>
</tr>
<%
}
%> </tbody> </table> <form action="add" method="post"> 商品名称:<input type="text" name="ProductName"/> 是否上架:<input type="radio" name="IsUp"/>是<input type="radio" name="IsUp"/>否 单价:<input type="text" name="UnitPrice"/><br/> 备注:<input type="text" name="Remark"/><br/> <input type="submit" value="添加商品" class="add"/> </form> <button><a href="select">查看销售记录</a></button> </body> <script> $(function(){
$(".add").on("click",function(){
alert("添加成功!");
}); $(".delete").on("click",function(){
alert("你确定要删除吗?");
});
});
</script>
</html>
select.jsp
代码语言:javascript复制<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="com.hnpi.bean.Sale"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() "://" request.getServerName() ":" request.getServerPort() path "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'select.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script src="jquery-3.3.1.js"></script>
</head>
<body>
<form action="insert" method="post">
商品名称:<input type="text" name="SalePerson"/><br/>
销售数量:<input type="text" name="Amount"/>
销售员:<input type="text" name="SalePerson"/>
<input type="submit" value="添加销售" class="add"/>
</form>
<table>
<thead>
<tr>
<td>销售员</td>
<td>商品名称</td>
<td>单价</td>
<td>销售数量</td>
<td>销售日期</td>
</tr>
</thead>
<tbody>
<%
List<Sale> sales = (ArrayList)session.getAttribute("saleList"); for(Sale sale : sales){
%>
<tr>
<td><%=sale.getSaleID() %></td>
<td><%=sale.getSalePerson() %></td>
<td><%=sale.getProductID() %></td>
<td><%=sale.getAmount() %></td>
<td><%=sale.getSaleDate() %></td>
</tr>
<%
}
%> </tbody> </table> </body> <script> $(function(){
$(".add").on("click",function(){
alert("添加成功");
})
});
</script>
</html>
看到这里相信你已经知道此系统的程序原理了,赶快去试试吧.
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/124762.html原文链接:https://javaforall.cn