商品销售管理系统
用JSP servlet和jquery以及SQLServer數據庫實現商品銷售管理系統。
功能詳解:
1.查詢商品
2.添加商品
3.刪除商品
4.添加銷售記錄
5.查看銷售記錄
效果演示:
進入界面
商品顯示頁面
刪除提示
銷售記錄查詢
商品銷售系統需要SQLServer數據庫和程序兩大部分
一:設計并實現數據庫
二:程序設計
在程序正式開始之前先看一下目錄結構吧
==特別注意:==引入sqljdbc4.jar和jquery-3.3.1.js架包
程序演示
bean包
Product.java
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
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
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 blocke.printStackTrace();}finally{DBUtil.closeConn(conn, ps, null);}response.sendRedirect("list");}}DeleteServlet.java
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 blocke.printStackTrace();}finally{DBUtil.closeConn(conn, ps, null);}response.sendRedirect("list");}}InsertServlet.java
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 blocke.printStackTrace();}finally{DBUtil.closeConn(conn, ps, null);}response.sendRedirect("select");}}ListChoseServlet
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 blocke.printStackTrace();}finally{DBUtil.closeConn(conn, ps, rs);}HttpSession session = request.getSession();session.setAttribute("productList", products);response.sendRedirect("list.jsp");}}ListServlet.java
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 blocke.printStackTrace();}finally{DBUtil.closeConn(conn, ps, rs);}HttpSession session = request.getSession();session.setAttribute("productList", products);response.sendRedirect("list.jsp");}}SelectServlet.java
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 blocke.printStackTrace();}finally{DBUtil.closeConn(conn, ps, rs);}HttpSession session = request.getSession();session.setAttribute("saleList", sales);response.sendRedirect("select.jsp");}}util包
DBUtil.java
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 blocke.printStackTrace();} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.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 blocke.printStackTrace();}}if(ps!=null){try {ps.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(rs!=null){try {rs.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}WEB-INF包
web.xml
<?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
<%@ 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
<%@ 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
<%@ 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>看到這里相信你已經知道此系統的程序原理了,趕快去試試吧.
掃一掃關注我的公眾號獲取更多資訊呦!!!
總結
- 上一篇: python 可执行文件打包_使用可执行
- 下一篇: MyEclipse生成常用方法