用Jsp Servlet和Jquery以及SQLServer实现商品管理系统。
采用MVC三层架构进行此程序的开发。
功能分析:
1.登录功能
2.查询和联合查询商品
3.增加商品
4.删除商品
效果演示
登录页面
查询页面
添加商品
点击删除
实现商品管理系统需要数据库和程序两部分。
一: 数据库
参考下面图片进行设计和实现SQLserver数据库
二:程序设计
我们先看一下程序目录结构
特别注意:
需要引入sqljdbc.jar和jquery.js两个架包
完整代码
servlet包
servlet包里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 {response.setContentType("text/html");PrintWriter out = response.getWriter();String id = request.getParameter("id");String sql = "delete from Product where ProductID = ?";Connection conn = DBUtil.getconn();try {System.out.println(sql);PreparedStatement ps = conn.prepareStatement(sql);ps.setInt(1,Integer.parseInt(id));ps.executeUpdate();response.sendRedirect("list");} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}out.flush();out.close();}}
servlet包里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 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 {response.setContentType("text/html");PrintWriter out = response.getWriter();String ProductName = new String(request.getParameter("ProductName").getBytes("iso-8859-1"),"utf-8");String IsUp = new String(request.getParameter("IsUp").getBytes("iso-8859-1"),"utf-8");String UnitPrice = new String(request.getParameter("UnitPrice").getBytes("iso-8859-1"),"utf-8");String Remark = new String(request.getParameter("Remark").getBytes("iso-8859-1"),"utf-8");String CategoryID = new String(request.getParameter("CategoryID").getBytes("iso-8859-1"),"utf-8");String sql = "insert into Product values(?,?,?,?,?)";Connection conn = DBUtil.getconn();try {System.out.println(sql);PreparedStatement ps = conn.prepareStatement(sql);ps.setString(1,ProductName);ps.setString(2,IsUp);ps.setInt(3, Integer.parseInt(UnitPrice));ps.setString(4,Remark);ps.setInt(5, Integer.parseInt(CategoryID));if(ps.executeUpdate()>0){response.sendRedirect("list");}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}out.flush();out.close();}}
servlet包里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 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.util.DBUtil;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");PrintWriter out = response.getWriter();//接受从查询页面传过来的值String ProductName = request.getParameter("ProductName");String Remark = request.getParameter("Remark");HttpSession session = request.getSession();//联合查询// select b表的所有,a表的名字 from a表 as a, b表 as b where b.b表的id = a.a表的idString sql = "select b.* ,a.CategoryName from Category as a , Product as b where b.CategoryID = a.CategoryID";//判断是否为空if(!"".equals(ProductName) && ProductName!=null){ProductName = new String(ProductName.getBytes("iso-8859-1"),"utf-8");sql =sql +" and b.ProductName = \'"+ProductName+"\'";}if(!"".equals(Remark) && Remark!=null){Remark = new String(Remark.getBytes("iso-8859-1"),"utf-8");sql =sql +" and b.Remark = \'"+Remark+"\'";}//列表Connection conn = DBUtil.getconn();try {System.out.println(sql);PreparedStatement ps = conn.prepareStatement(sql);ResultSet rs = ps.executeQuery();session.setAttribute("set", rs);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}//类别try {String sq = "select * from Category";PreparedStatement ps = conn.prepareStatement(sq);ResultSet rs = ps.executeQuery();session.setAttribute("se", rs);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}response.sendRedirect("list.jsp");out.flush();out.close();}}
servlet包里LoginServlet.java
package com.hnpi.servlet;import java.awt.image.DataBufferUShort;
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 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 LoginServlet 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 name = request.getParameter("name");String pwd = request.getParameter("pwd");Connection conn = DBUtil.getconn();PreparedStatement ps = null;ResultSet rs = null;String sql = "select * from Userinfo where UserName = ? and Password = ?";try {ps = conn.prepareStatement(sql);ps.setString(1, name);ps.setString(2, pwd);rs = ps.executeQuery();if(rs.next()){response.sendRedirect("list");}else{response.sendRedirect("login.jsp");}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
util包
util包里DBUtil.java
package com.hnpi.util;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;public class DBUtil {public static Connection getconn(){try {Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}Connection conn = null;try {conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=Test","sa","1");} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return conn;}
}
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></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>LoginServlet</servlet-name><servlet-class>com.hnpi.servlet.LoginServlet</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>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-mapping><servlet-name>LoginServlet</servlet-name><url-pattern>/login</url-pattern></servlet-mapping><servlet-mapping><servlet-name>ListServlet</servlet-name><url-pattern>/list</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> <welcome-file-list><welcome-file>login.jsp</welcome-file></welcome-file-list>
</web-app>
Jsp页面
add.jsp
<%@page import="java.sql.ResultSet"%>
<%@ 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 'add.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><form action="insert">商品名称:<input id="ProductName" name ="ProductName"/><br/>是否上架:<input id="IsUp" type="radio" name ="IsUp" value="是"/>是<input type="radio" name ="IsUp" value="否"/>否<br/>单价:<input type="number" id="UnitPrice" name ="UnitPrice"/><br/>备注:<textarea id="Remark" rows="5" cols="20" name="Remark"></textarea><br/>所属类别:<select id="CategoryID" name= "CategoryID"><% ResultSet rs = (ResultSet)session.getAttribute("se");while(rs.next()){%><option value=<%=rs.getString(1) %>><%=rs.getString(2) %></option><%}%></select><br/><input type="submit" value="提交"></form></body><script type="text/javascript" src="jquery-3.3.1.js"></script><script type="text/javascript">
$(function(){$("form").submit(function(){var ProductName = $("#ProductName").val();var IsUp = $("#IsUp").val();var UnitPrice = $("#UnitPrice").val();var Remark = $("#Remark").val();if(""==ProductName||""==IsUp||""==UnitPrice||""==Remark){alert("请补充完整信息")return false;}else{return true;}})
})</body>
</html>
list.jsp
<%@page import="java.sql.ResultSet"%>
<%@ 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 '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">--></head><body><center><form action="list">商品名称:<input name = "ProductName">商品备注:<input name = "Remark"><input type="submit" value="查询"><button><a href="add.jsp">添加商品</a></button></form><table border="1" cellspacing="0"><tr><th>编号</th><th>商品名称</th><th>是否上架</th><th>单价</th><th>备注</th><th>所属类别</th><th>操作</th></tr><% ResultSet rs = (ResultSet)session.getAttribute("set");while(rs.next()){%><tr><th><%=rs.getString(1) %></th><th><%=rs.getString(2) %></th><th><%=rs.getString(3) %></th><th><%=rs.getString(4) %></th><th><%=rs.getString(5) %></th><th><%=rs.getString(7) %></th><th><button><a href="del?id=<%=rs.getString(1) %>" >删除</a></button></th></tr><%}%></table></center></body>
</html>
login.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><form action="login">账号:<input id="name" name="name"><br /> 密码:<input id="pwd"name="pwd"><br /> <input type="submit"></form>
</body>
<script type="text/javascript" src="jquery-3.3.1.js"></script>
<script type="text/javascript">$(function() {$("form").submit(function() {var name = $("#name").val();var pwd = $("#pwd").val();if ("" == name) {alert("请输入账号")return false;} else if ("" == pwd) {alert("请输入密码")return false;} else {return true;}})})
</script>
</body>
</html>
我们已经把商品管理系统完整实现了。
扫一扫关注我的公众号获取更多资讯!!!