用Jsp Servlet和Jquery实现设备缺陷管理系统。
设备缺陷管理系统采用MVC三层架构模式进行开发,有效地解决了对缺陷设备的管理和修正。
效果演示:
查询和联合查询页面
添加页面
点击删除之后
功能分析:
1.查询所有设备
2.联合查询
3.添加缺陷设备
4.删除缺陷设备
5.处理缺陷设备
实现此系统要准备两大方面的内容,分别是数据库和程序。
一: 数据库(SQLServer数据库)
我的数据库名为Test
设备表
缺陷表
当然在开始我们的程序之前还要特别注意:
引入sqljdbc.jar架包和jquery-3.3.1.js架包
二:完整程序
我们先看一下目录结构
正式进入程序
bean包:
bean包里的Defect.java
package com.hnpi.bean;import java.sql.Timestamp;public class Defect {private Integer defectId;private Integer defectType;private String content;private Timestamp addTime;private Integer defectState;private Timestamp dealTime;private Integer deviceId;public Integer getDefectId() {return defectId;}public void setDefectId(Integer defectId) {this.defectId = defectId;}public Integer getDefectType() {return defectType;}public void setDefectType(Integer defectType) {this.defectType = defectType;}public String getContent() {return content;}public void setContent(String content) {this.content = content;}public Timestamp getAddTime() {return addTime;}public void setAddTime(Timestamp addTime) {this.addTime = addTime;}public Integer getDefectState() {return defectState;}public void setDefectState(Integer defectState) {this.defectState = defectState;}public Timestamp getDealTime() {return dealTime;}public void setDealTime(Timestamp dealTime) {this.dealTime = dealTime;}public Integer getDeviceId() {return deviceId;}public void setDeviceId(Integer deviceId) {this.deviceId = deviceId;}public Defect(Integer defectId, Integer defectType, String content,Timestamp addTime, Integer defectState, Timestamp dealTime,Integer deviceId) {super();this.defectId = defectId;this.defectType = defectType;this.content = content;this.addTime = addTime;this.defectState = defectState;this.dealTime = dealTime;this.deviceId = deviceId;}public Defect() {super();// TODO Auto-generated constructor stub}}
bean包里的Device.java
package com.hnpi.bean;public class Device {private Integer deviceId;private String deviceName;public Integer getDeviceId() {return deviceId;}public void setDeviceId(Integer deviceId) {this.deviceId = deviceId;}public String getDeviceName() {return deviceName;}public void setDeviceName(String deviceName) {this.deviceName = deviceName;}public Device(Integer deviceId, String deviceName) {super();this.deviceId = deviceId;this.deviceName = deviceName;}public Device() {super();// TODO Auto-generated constructor stub}}
servlet包:
servlet包里的DefectAddServlet.java
package com.hnpi.servlet;import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Timestamp;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 DefectAddServlet 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");String deviceId = request.getParameter("deviceId");String defectState = request.getParameter("defectState");String content = request.getParameter("content");Connection conn = DBUtil.getConn();PreparedStatement ps = null;String sql = "insert into defect(defectType,contents,addTime,defectState,deviceId) values(?,?,?,?,?)";try {ps = conn.prepareStatement(sql);ps.setInt(1, Integer.parseInt(defectState));ps.setString(2, content);ps.setTimestamp(3, new Timestamp(System.currentTimeMillis()));ps.setInt(4, 1);ps.setInt(5, Integer.parseInt(deviceId));ps.executeUpdate();response.sendRedirect("defectList");} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}finally{DBUtil.closeConn(conn, ps, null);}}}
servlet包里的DefectDel.java
package com.hnpi.servlet;import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;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 DefectDel 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");String defectId = request.getParameter("id");Connection conn = DBUtil.getConn();PreparedStatement ps = null;String sql = "delete from defect where defectId = ?";try {ps = conn.prepareStatement(sql);ps.setInt(1, Integer.parseInt(defectId));ps.executeUpdate();response.sendRedirect("defectList");} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}finally{DBUtil.closeConn(conn, ps, null);}}}
servlet包里的DefectListServlet.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.HashMap;
import java.util.List;
import java.util.Map;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.Defect;
import com.hnpi.bean.Device;
import com.hnpi.util.DBUtil;public class DefectListServlet 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");String deviceId = request.getParameter("deviceId");String content = request.getParameter("content");String defectState = request.getParameter("defectState");Connection conn = DBUtil.getConn();PreparedStatement ps = null;ResultSet rs = null;String sql = "select * from defect where 1=1";List<Defect> defects = new ArrayList<Defect>();List<Device> devices = new ArrayList<Device>();Map<Integer, String> deviceMaps = new HashMap<Integer, String>();List<Object> params = new ArrayList<Object>();boolean deviceIdFlag = (deviceId != null && !"".equals(deviceId) && !"*".equals(deviceId));boolean contentFlag = (content != null && !"".equals(content));boolean defectStateFlag = (defectState != null && !"".equals(defectState) && !"*".equals(defectState));if (deviceIdFlag) {sql += " and deviceId = ?";params.add(Integer.parseInt(deviceId));}if (contentFlag) {sql += " and contents like ?";params.add("%"+ content +"%");}if (defectStateFlag) {sql += " and defectState = ?";params.add(Integer.parseInt(defectState));}try {ps = conn.prepareStatement(sql);for (int i = 1; i <= params.size(); i++) {Object obj = params.get(i-1);if (obj instanceof Integer) {ps.setInt(i, (Integer) obj);}if (obj instanceof String) {ps.setString(i, (String) obj);}}rs = ps.executeQuery();while (rs.next()) {Defect defect = new Defect(rs.getInt(1), rs.getInt(2), rs.getString(3), rs.getTimestamp(4),rs.getInt(5), rs.getTimestamp(6), rs.getInt(7));defects.add(defect);}sql = "select * from device";ps = conn.prepareStatement(sql);rs = ps.executeQuery();while (rs.next()) {Device device = new Device(rs.getInt(1), rs.getString(2));devices .add(device);deviceMaps.put(device.getDeviceId(), device.getDeviceName());}} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}finally{DBUtil.closeConn(conn, ps, rs);}HttpSession session = request.getSession();session.setAttribute("defects", defects);session.setAttribute("devices", devices);session.setAttribute("deviceMaps", deviceMaps);response.sendRedirect("defectList.jsp");}}
servlet包里的DefectUp.java
package com.hnpi.servlet;import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Timestamp;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 DefectUp 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");String defectId = request.getParameter("id");Connection conn = DBUtil.getConn();PreparedStatement ps = null;String sql = "update defect set defectState = 2,dealTime = ? where defectId = ?";try {ps = conn.prepareStatement(sql);ps.setTimestamp(1, new Timestamp(System.currentTimeMillis()));ps.setInt(2, Integer.parseInt(defectId));ps.executeUpdate();response.sendRedirect("defectList");} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}finally{DBUtil.closeConn(conn, ps, null);}}}
servlet包里的DeviceIdString.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.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.Device;
import com.hnpi.util.DBUtil;public class DeviceIdString 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");Connection conn = DBUtil.getConn();PreparedStatement ps = null;ResultSet rs = null;List<Device> devices = new ArrayList<Device>();String sql = "select * from device";try {ps = conn.prepareStatement(sql);rs = ps.executeQuery();while (rs.next()) {Device device = new Device(rs.getInt(1), rs.getString(2));devices .add(device);}} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}finally{DBUtil.closeConn(conn, ps, null);}HttpSession session = request.getSession();session.setAttribute("devices", devices);response.sendRedirect("defectAdd.jsp");}}
util包
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(){Connection conn = null;try {Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=Test", "sa", "1");} catch (ClassNotFoundException e) {// TODO: handle exceptione.printStackTrace();}catch (SQLException e) {// TODO: handle exceptione.printStackTrace();}return conn;}public static void closeConn(Connection conn,PreparedStatement ps,ResultSet rs){try {if (conn != null) {conn.close();}if (ps != null) {ps.close();}if (rs != null) {rs.close();}} catch (SQLException e) {// TODO: handle exceptione.printStackTrace();}}
}
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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"><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>DefectListServlet</servlet-name><servlet-class>com.hnpi.servlet.DefectListServlet</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>DefectDel</servlet-name><servlet-class>com.hnpi.servlet.DefectDel</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>DefectUp</servlet-name><servlet-class>com.hnpi.servlet.DefectUp</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>DeviceIdString</servlet-name><servlet-class>com.hnpi.servlet.DeviceIdString</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>DefectAddServlet</servlet-name><servlet-class>com.hnpi.servlet.DefectAddServlet</servlet-class></servlet><servlet-mapping><servlet-name>DefectListServlet</servlet-name><url-pattern>/defectList</url-pattern></servlet-mapping><servlet-mapping><servlet-name>DefectDel</servlet-name><url-pattern>/defectDel</url-pattern></servlet-mapping><servlet-mapping><servlet-name>DefectUp</servlet-name><url-pattern>/defectUp</url-pattern></servlet-mapping><servlet-mapping><servlet-name>DeviceIdString</servlet-name><url-pattern>/deviceId</url-pattern></servlet-mapping><servlet-mapping><servlet-name>DefectAddServlet</servlet-name><url-pattern>/defectAdd</url-pattern></servlet-mapping><welcome-file-list><welcome-file>defectList</welcome-file></welcome-file-list>
</web-app>
jsp页面
defectAdd.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="com.hnpi.bean.Device"%>
<%
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 'defectAdd.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="defectAdd" method="post"><table border="1" cellspacing="0"><tr><td align="center" colspan="2">添加缺陷</td></tr><tr><td>所属设备:</td><td><select name="deviceId"><option selected="selected">--请选择--</option><%List<Device> devices = (List)session.getAttribute("devices");for(Device device : devices){%><option value="<%=device.getDeviceId() %>"><%=device.getDeviceName() %></option><%} %></select></td></tr><tr><td>缺陷类型:</td><td><input type="radio" name="defectState" id="state1" value="1"/><label for="state1">紧急</label><input type="radio" name="defectState" id="state2" value="2"/><label for="state1">重要</label><input type="radio" name="defectState" id="state3" value="3"/><label for="state1">一般</label></td></tr><tr><td>缺陷内容:</td><td><textarea rows="4" cols="24" name="content"></textarea></td></tr><tr><td></td><td><input type="submit" value="添加"/><a href="defectList"><input type="button" value="返回到列表"/></a></td></tr></table></form></body>
</html>
defectList.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="com.hnpi.bean.Device"%>
<%@page import="com.hnpi.bean.Defect"%>
<%
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%>"><script src="jquery-3.3.1.js"></script><title>My JSP 'index.jsp' starting page</title></head><body><form action="defectList" method="post">所属设备:<select name="deviceId"><option selected="selected" value="*">全部设备</option><%List<Device> devices = (List)session.getAttribute("devices");for(Device device : devices){%><option value="<%=device.getDeviceId() %>"><%=device.getDeviceName() %></option><%} %></select>缺陷内容:<input type="text" name="content"/>缺陷状态:<input type="radio" name="defectState" id="state1" value="1"/><label for="state1">未解决</label><input type="radio" name="defectState" id="state2" value="2"/><label for="state1">已解决</label><input type="radio" name="defectState" id="state3" value="*"/><label for="state1">全部</label><input type="submit" value="查询"/><a href="deviceId"><input type="button" value="添加"/></a></form><table border="1" cellspacing="0"><thead><tr><td>所属设备</td><td>缺陷类型</td><td>缺陷内容</td><td>缺陷添加时间</td><td>缺陷状态</td><td>缺陷处理时间</td><td>操作</td></tr></thead><tbody><%List<Defect> defects = (List)session.getAttribute("defects");Map<Integer,String> deviceMaps = (HashMap)session.getAttribute("deviceMaps");for(Defect defect : defects){ %><tr><td><%=deviceMaps.get(defect.getDeviceId()) %></td><td><%if(defect.getDefectType() == 1){ %>紧急<%} %><%if(defect.getDefectType() == 2){ %>重要<%} %><%if(defect.getDefectType() == 3){ %>一般<%} %></td><td><%=defect.getContent() %></td><td><%=defect.getAddTime() %></td><td><%if(defect.getDefectState() == 1){ %>未处理<%} %><%if(defect.getDefectState() == 2){ %>已处理<%} %></td><td><%=defect.getDealTime() %></td><td><a style="display:<%=defect.getDefectState()!=1?"none":"block"%>" href="defectUp?id=<%=defect.getDefectId() %>"><input type="button" value="处理缺陷" id="up"/></a><a href="defectDel?id=<%=defect.getDefectId()%>"><input type="button" value="删除"/></a></td></tr><%} %></tbody></table></body>
</html>
程序已经完全结束,赶快去试试吧!!!
扫一扫添加我的微信公众号获取更多资讯呦!!!