From 441e92d7312fa3fe35edb23f5fdd8aa8674e7bea Mon Sep 17 00:00:00 2001 From: you <284908631@qq.com> Date: Tue, 9 Jun 2020 16:23:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9F=BA=E6=9C=AC=E5=AE=8C=E6=88=90=E7=B3=BB?= =?UTF-8?q?=E7=BB=9F=E7=AE=A1=E7=90=86=E5=91=98=E7=9A=84=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WebContent/admin/sortadd.jsp | 79 +++++++++++++++++++++++ WebContent/admin/sortedit.jsp | 101 +++++++++++++++++++++++++++++ WebContent/admin/sortlist.jsp | 108 ++++++++++++++++++++++++++++++++ src/servlet/admin/SortAdd.java | 83 ++++++++++++++++++++++++ src/servlet/admin/SortDel.java | 82 ++++++++++++++++++++++++ src/servlet/admin/SortEdit.java | 86 +++++++++++++++++++++++++ src/servlet/admin/SortList.java | 75 ++++++++++++++++++++++ 7 files changed, 614 insertions(+) create mode 100644 WebContent/admin/sortadd.jsp create mode 100644 WebContent/admin/sortedit.jsp create mode 100644 WebContent/admin/sortlist.jsp create mode 100644 src/servlet/admin/SortAdd.java create mode 100644 src/servlet/admin/SortDel.java create mode 100644 src/servlet/admin/SortEdit.java create mode 100644 src/servlet/admin/SortList.java diff --git a/WebContent/admin/sortadd.jsp b/WebContent/admin/sortadd.jsp new file mode 100644 index 0000000..bf98bd1 --- /dev/null +++ b/WebContent/admin/sortadd.jsp @@ -0,0 +1,79 @@ +<%@ page language="java" contentType="text/html; charset=UTF-8" + pageEncoding="UTF-8"%> + + + + +分类添加 + + + + + + + +
+
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+
+ +
+
+
+ + + \ No newline at end of file diff --git a/WebContent/admin/sortedit.jsp b/WebContent/admin/sortedit.jsp new file mode 100644 index 0000000..2d041e0 --- /dev/null +++ b/WebContent/admin/sortedit.jsp @@ -0,0 +1,101 @@ +<%@page import="javabean.Base"%> +<%@page import="java.sql.ResultSet"%> +<%@page import="java.sql.PreparedStatement"%> +<%@page import="java.sql.Connection"%> +<%@ page language="java" contentType="text/html; charset=UTF-8" + pageEncoding="UTF-8"%> + + + + +分类编辑 + + + + + + +<% + String id = request.getParameter("id"); + Connection connection = null; + PreparedStatement pstmt = null; + ResultSet resultSet = null; + String sql = ""; + + connection = (Connection)Base.getConnection(); + sql = "select * from book_sort where id = ?"; + pstmt = connection.prepareStatement(sql); + pstmt.setString(1, id); + resultSet = pstmt.executeQuery(); + resultSet.next(); +%> + +
+ lay-verify="required" required autocomplete="off" class="layui-input layui-hide"> +
+ +
+ lay-verify="required" required autocomplete="off" placeholder="请输入分类名" class="layui-input"> +
+
+ +
+ +
+ +
+
+ +
+
+ +
+
+
+ +<% + Base.closeResource(connection, pstmt, resultSet); +%> + + \ No newline at end of file diff --git a/WebContent/admin/sortlist.jsp b/WebContent/admin/sortlist.jsp new file mode 100644 index 0000000..66053be --- /dev/null +++ b/WebContent/admin/sortlist.jsp @@ -0,0 +1,108 @@ +<%@ page language="java" contentType="text/html; charset=UTF-8" + pageEncoding="UTF-8"%> + + + + + 借阅卡 + + + + + + +
+ + + + + + + + + \ No newline at end of file diff --git a/src/servlet/admin/SortAdd.java b/src/servlet/admin/SortAdd.java new file mode 100644 index 0000000..027f724 --- /dev/null +++ b/src/servlet/admin/SortAdd.java @@ -0,0 +1,83 @@ +package servlet.admin; + +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.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import javabean.Base; +import javabean.Common; +import javabean.Util; + +@WebServlet("/admin/sortAdd") +public class SortAdd extends HttpServlet{ + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + resp.setContentType("application/json; charset=utf8"); + // 接受数据 + String name = req.getParameter("name"); + String description = req.getParameter("description"); + + // 准备数据 + Connection connection = null; + PreparedStatement pstmt = null; + ResultSet resultSet = null; + int result = 0; + String sql = ""; + int count = 0 ; + // 准备返回数据 + int code = 1; + String msg = ""; + + try { + connection = (Connection) Base.getConnection(); + // 查询重复name + sql = "select count(*) as count from book_sort where name=?"; + pstmt = connection.prepareStatement(sql); + pstmt.setString(1, name); + resultSet = pstmt.executeQuery(); + if(resultSet.next()) { + // 有重复 + if(resultSet.getInt("count") > 0) { + msg = "分类名不能重复"; + }else { + // 进行插入 + sql = "insert into book_sort(name, description) values(?,?)"; + pstmt = connection.prepareStatement(sql); + pstmt.setString(1, name); + pstmt.setString(2, description); + result = pstmt.executeUpdate(); + if(result == 1) { + code = 0; + msg = "添加成功"; + }else { + msg = "添加失败"; + } + } + } + + } catch (ClassNotFoundException e) { + msg = "classnotfound"; + } catch (SQLException e) { + msg = "SQL错误"; + } finally { + try { + Base.closeResource(connection, pstmt, resultSet); + } catch (SQLException e) { + msg = "关闭失败"; + } + } + + PrintWriter out = resp.getWriter(); + out.print(Util.jsonResponse(code, msg, null)); + + } +} diff --git a/src/servlet/admin/SortDel.java b/src/servlet/admin/SortDel.java new file mode 100644 index 0000000..e368b57 --- /dev/null +++ b/src/servlet/admin/SortDel.java @@ -0,0 +1,82 @@ +package servlet.admin; + +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.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import javabean.Base; +import javabean.Util; + + +@WebServlet("/admin/sortDel") +public class SortDel extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + // !!!!! 默认1为未分类 + String defaultId = "1"; + resp.setContentType("application/json; charset=utf8"); + // 接受数据 + String id = req.getParameter("id"); + + // 准备数据 + Connection connection = null; + PreparedStatement pstmt = null; + ResultSet resultSet = null; + int result = 0; + String sql = ""; + + // 准备返回数据 + int code = 1; + String msg = ""; + try { + // 不能删除未分类 + if(defaultId.equals(id)) { + msg = "不能删除未分类"; + }else { + connection = (Connection) Base.getConnection(); + // 分类下的文章修改 + sql = "update books set sort_id=? where sort_id=?"; + pstmt = connection.prepareStatement(sql); + pstmt.setString(1, defaultId); + pstmt.setString(2, id); + result = pstmt.executeUpdate(); + + // 进行删除 + sql = "delete from book_sort where id=?"; + pstmt = connection.prepareStatement(sql); + pstmt.setString(1, id); + result = pstmt.executeUpdate(); + if(result == 1) { + code = 0; + msg = "删除成功"; + }else { + msg = "删除失败"; + } + } + + } catch (ClassNotFoundException e) { + msg = "classnotfound"; + } catch (SQLException e) { + msg = "SQL错误"; + } finally { + try { + Base.closeResource(connection, pstmt, resultSet); + } catch (SQLException e) { + msg = "关闭失败"; + } + } + + PrintWriter out = resp.getWriter(); + out.print(Util.jsonResponse(code, msg, null)); + } + +} diff --git a/src/servlet/admin/SortEdit.java b/src/servlet/admin/SortEdit.java new file mode 100644 index 0000000..36d90b1 --- /dev/null +++ b/src/servlet/admin/SortEdit.java @@ -0,0 +1,86 @@ +package servlet.admin; + +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.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import javabean.Base; +import javabean.Util; + + +@WebServlet("/admin/sortEdit") +public class SortEdit extends HttpServlet { + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + resp.setContentType("application/json; charset=utf8"); + // 接受数据 + String id = req.getParameter("id"); + String name = req.getParameter("name"); + String description = req.getParameter("description"); + + // 准备数据 + Connection connection = null; + PreparedStatement pstmt = null; + ResultSet resultSet = null; + int result = 0; + String sql = ""; + int count = 0 ; + // 准备返回数据 + int code = 1; + String msg = ""; + + try { + connection = (Connection) Base.getConnection(); + // 查询重复name + sql = "select count(*) as count from book_sort where name=? and id != ?"; + pstmt = connection.prepareStatement(sql); + pstmt.setString(1, name); + pstmt.setString(2, id); + resultSet = pstmt.executeQuery(); + if(resultSet.next()) { + // 有重复 + if(resultSet.getInt("count") > 0) { + msg = "分类名不能重复"; + }else { + // 进行插入 + sql = "update book_sort set name=?, description=? where id=?"; + pstmt = connection.prepareStatement(sql); + pstmt.setString(1, name); + pstmt.setString(2, description); + pstmt.setString(3, id); + result = pstmt.executeUpdate(); + if(result == 1) { + code = 0; + msg = "修改成功"; + }else { + msg = "修改失败"; + } + } + } + + } catch (ClassNotFoundException e) { + msg = "classnotfound"; + } catch (SQLException e) { + msg = "SQL错误"; + } finally { + try { + Base.closeResource(connection, pstmt, resultSet); + } catch (SQLException e) { + msg = "关闭失败"; + } + } + + PrintWriter out = resp.getWriter(); + out.print(Util.jsonResponse(code, msg, null)); + + } +} diff --git a/src/servlet/admin/SortList.java b/src/servlet/admin/SortList.java new file mode 100644 index 0000000..e0e014e --- /dev/null +++ b/src/servlet/admin/SortList.java @@ -0,0 +1,75 @@ +package servlet.admin; + +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.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import javabean.Base; +import javabean.Util; +import net.sf.json.JSONArray; +import net.sf.json.JSONObject; + + +@WebServlet("/admin/sortList") +public class SortList extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + resp.setContentType("application/json; charset:utf8"); + // 接受参数 + + // 准备参数 + Connection connection = null; + PreparedStatement pstmt = null; + ResultSet resultSet = null; + JSONObject jsonObject = new JSONObject(); + JSONArray jsonArray = new JSONArray(); + // 返回参数 + int code = 1; + String msg = "error"; + int count = 0; + String sql = ""; + PrintWriter out = resp.getWriter(); + // 开始查询 + try { + connection = Base.getConnection(); + sql = "select * from book_sort"; + pstmt = connection.prepareStatement(sql); + resultSet = pstmt.executeQuery(); + while(resultSet.next()) { + jsonObject.put("id", resultSet.getString("id")); + jsonObject.put("name", resultSet.getString("name")); + jsonObject.put("description", resultSet.getString("description")); + jsonArray.add(jsonObject.toString()); + } + if(!jsonArray.isEmpty()) { + code = 0; + msg = "查询成功"; + }else { + msg = "数据为空"; + } + } catch (ClassNotFoundException e) { + msg = "没找到"; + e.printStackTrace(); + } catch (SQLException e) { + msg = "sql错误"; + }finally { + try { + Base.closeResource(connection, pstmt, resultSet); + } catch (SQLException e) { + msg = "关闭失败"; + } + } + out.print( Util.jsonResponse(code, msg, jsonArray.toString()) ); + + } + +}