IDEA+Java+JSP+Mysql+Tomcat实现Web图书管理系统-添加管理员增删改查功能

一、系统介绍

软件环境
Operating System:Windows10
IDEA:2018.2
Java:jdk1.8
Mysql:8.0.13
Tomcat:8.5.23

本次更新添加了管理员的增删改查,图书管理员可以进行图书的新增,修改,删除,查询。然后登录部分添加了区分是普通用户登录还是管理登录的功能。

下面是新增页面之后的工程的截图
在这里插入图片描述

二、系统展示

1.管理员主页

在这里插入图片描述

2.管理员查询图书页面

在这里插入图片描述

3.管理员图书明细页面

在这里插入图片描述

三、代码实现

1.前端

admin.jsp

<%--
  Created by IntelliJ IDEA.
  User: shuijianshiqing
  Date: 2020/5/24
  Time: 10:50
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%@ page import="java.util.List" %>
<%@ page import="com.sjsq.po.Book" %>
<%@ page import="com.sjsq.service.impl.BookServiceImpl" %>
<%@ page import="com.sjsq.po.User" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>查看图书</title>
    <style type="text/css">
        h1{
            text-align: center;
        }
        body{
            background-color: antiquewhite;
        }
        #before{
            text-align: center;
        }
        #head{
            background: #eeeeee;height: 80px;
        }
        #headLink{
            font-size: 20px;
        }
        #headWelLink{
            font-size: 20px;
        }
    </style>
</head>
<body>
<%--头部信息--%>
<%
    User user =(User)session.getAttribute("user");
    if(user == null){
        response.sendRedirect("login.jsp");
    }else {
%>
<div id="head">
    <table width="100%">
        <td id="headWelLink">欢迎您:<%=user.getName()%></td>
        <td align="right" id="headLink">
            <a href="logout.jsp">安全退出</a>
        </td>
    </table>
</div>
<%
    }
%>

<%--图书信息--%>
<%
    // 设置获取注册时的编码为UTF-8
    request.setCharacterEncoding("UTF-8");
    Book book = new Book();
    // 获取上一个页面传过来的值
    String boookname=request.getParameter("bookname");
    book.setBookname(boookname);
    BookServiceImpl service = new BookServiceImpl();
    List<Book> list = service.select(book);

%>
<h1>图书列表</h1>
<div id="before">
    <form action="admin.jsp" method="post">
        请输入姓名:<input type="text" name="bookname" placeholder="输入图书名称搜索">
        <input type="submit" value="查询" />
    </form>
    <a href="javascript: window.history.go(-1)">返回上一级</a>
</div>
<table align="center" cellpadding="10" cellspacing="10">
    <tr bgcolor="green">
        <td>编号</td>
        <td>书名</td>
        <td>价格</td>
        <td>作者</td>
        <td>封皮</td>
        <td>出版社</td>
        <td>操作图书</td>
    </tr>
    <%
        String bg = null;
        for (int i = 0;i<list.size();i++){
            Book b =list.get(i);
            if(i%2 == 0){
                bg = "pink";
            }else{
                bg = "yellow";
            }

    %>
    <tr bgcolor="<%=bg%>">
        <td><%=b.getBookid()%></td>
        <td><a href="admin_do_info.jsp?bookid=<%=b.getBookid()%>"><%=b.getBookname()%></a></td>
        <td><%=b.getPrice() %></td>
        <td><%=b.getAuthor() %></td>
        <td><%=b.getPicture() %></td>
        <td><%=b.getPublish() %></td>
        <td>
            <a href="admin_update.jsp?bookid=<%=b.getBookid()%>">修改</a>
            <a href="admin_delete.jsp?bookid=<%=b.getBookid()%>">删除</a>
        </td>
    </tr>
    <%
        }
    %>

</table>
<table align="center">
    <tr>
        <td><a href="admin_addbook.jsp">新增图书</a></td>
    </tr>
</table>

</body>
</html>

admin_addbook.jsp

<%@ page import="com.sjsq.po.User" %><%--
  Created by IntelliJ IDEA.
  User: shuijianshiqing
  Date: 2020/5/21
  Time: 23:14
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>新增图书</title>
    <style type="text/css">
        h1{
            text-align: center;
        }
        h4{
            text-align: center;color: red;
        }
        body{
            background-color: antiquewhite;
        }
        div{
            text-align: center;
        }
        #before{
            text-align: center;
        }
        #head{
            background: #eeeeee;height: 80px;
        }
        #headLink{
            font-size: 20px;
        }
        #headWelLink{
            font-size: 20px;
        }
    </style>
</head>
<body>
<%--头部信息--%>
<%
    User user =(User)session.getAttribute("user");
    if(user == null){
        response.sendRedirect("login.jsp");
    }else {
%>

<table width="100%" id="head">
    <td id="headWelLink">欢迎您:<%=user.getName()%></td>
    <td align="right" id="headLink">
        <a href="logout.jsp">安全退出</a>
    </td>
</table>

<%
    }
%>

<h1>新增图书</h1>
<hr/>

<div id="before">
    <a href="javascript: window.history.go(-1)">返回上一级</a>
</div>
</br>

<form action="admin_do_addbook.jsp" method="post" name="registerForm">
    <div>
        <tr>
            <label>图书名称:</label>
            <input type="text" name="bookname" id="bookname" placeholder="图书名称" autofocus="autofocus">
        </tr>
    </div>
    <div>
        <tr>
            <label>图书价格:</label></td>
            <input type="text" name="price" id="price" placeholder="图书价格(数字)">
        </tr>
    </div>
    <div>
        <tr>
        <label>图书作者:</label>
        <input type="text" name="author" id="author" placeholder="图书作者">
        </tr>
    </div>
    <div>
        <tr>
            <label>出版公司:</label>
            <input type="text" name="publish" id="publish" placeholder="出版公司">
        </tr>
    </div>

    <div id="submitbtn">
        <tr>
            <button type="submit" onclick="return checkForm()">添加</button>
            <button type="reset">重置</button>

        </tr>
    </div>
</form>

<script type="text/javascript">
    function checkForm() {
        var bookname = registerForm.bookname.value;
        var price = registerForm.price.value;
        //alert(name + pwd + repwd);
        if (bookname == "" || bookname == null) {
            alert("请输入图书名称");
            registerForm.bookname.focus();
            return false;
        } else if (price == "" || price == null) {
            alert("请输入图书价格");
            registerForm.price.focus();
            return false;
        }
        alert('添加成功!');
        return true;
    }
</script>
</body>
</html>

admin_delete.jsp

<%@ page import="com.sjsq.po.Book" %>
<%@ page import="com.sjsq.dao.impl.BookDaoImpl" %>
<%@ page import="com.sjsq.dao.BookDao" %>
<%@ page import="com.sjsq.po.User" %><%--
  Created by IntelliJ IDEA.
  User: shuijianshiqing
  Date: 2020/6/10
  Time: 22:45
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>删除图书</title>
    <style type="text/css">
        #before{
            text-align: center;
        }
        body{
            background-color: antiquewhite;
        }
        #head{
            background: #eeeeee;height: 80px;
        }
        #headLink{
            font-size: 20px;
        }
        #headWelLink{
            font-size: 20px;
        }
    </style>
</head>
<body>

<%--头部信息--%>
<%
    User user =(User)session.getAttribute("user");
    if(user == null){
        response.sendRedirect("login.jsp");
    }else {
%>
<div id="head">
    <table width="100%">
        <td id="headWelLink">欢迎您:<%=user.getName()%></td>
        <td align="right" id="headLink">
            <a href="logout.jsp">安全退出</a>
        </td>
    </table>
</div>
<%
    }
%>

<%
    // 设置获取注册时的编码为UTF-8
    request.setCharacterEncoding("UTF-8");

    //获取admin.jsp页面的bookid
    Integer bookid=Integer.parseInt(request.getParameter("bookid"));


    //引入数据交互层
    BookDao dao=new BookDaoImpl();
    Book book=new Book();
    book = dao.getBook(bookid);
    System.out.println("删除的图书信息:");
    System.out.println(book);
    boolean flag=dao.deleteBook(bookid);

    if(flag){
        response.sendRedirect("admin.jsp");
    }else{
        response.sendRedirect("error.jsp");
    }
%>
</body>
</html>

admin_detail.jsp

<%--
  Created by IntelliJ IDEA.
  User: shuijianshiqing
  Date: 2020/5/24
  Time: 10:51
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%@ page import="com.sjsq.po.Book" %>
<%@ page import="com.sjsq.po.User" %>

<%
    // 获取绝对路径路径 ,开发项目一定要使用绝对路径,不然肯定出错
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":"
            + request.getServerPort() + path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <base href="<%=basePath %>" />
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>图书详情</title>
    <style type="text/css">
        h1{
            text-align: center;
        }
        body{
            background-color: antiquewhite;
        }
        a{
            text-align:center;font-size: 24px;text-decoration: none;
        }
        a:hover{
            text-decoration: underline;font-size: 20px;
        }
        #before{
            text-align: center;
        }
        #head{
            background: #eeeeee;height: 80px;
        }
        #headLink{
            font-size: 20px;
        }
        #headWelLink{
            font-size: 20px;
        }
    </style>
</head>
<body>
<%--头部信息--%>
<%
    User user =(User)session.getAttribute("user");
    if(user == null){
        response.sendRedirect("login.jsp");
    }else {
%>
<div id="head">
    <table width="100%">
        <td id="headWelLink">欢迎您:<%=user.getName()%></td>
        <td align="right" id="headLink">
            <a href="logout.jsp">安全退出</a>
        </td>
    </table>
</div>
<%
    }
%>


<h1>图书详情</h1>
<div id="before">
    <a href="javascript: window.history.go(-1)">返回上一级</a>
</div>
<%
    Book book = (Book)session.getAttribute("book");
%>
<table align="center" cellpadding="20" cellspacing="20">
    <tr style="font-size: 20px">
        <td>图书编号</td>
        <td>图书名称</td>
        <td>图书价格</td>
        <td>图书作者</td>
        <td>图书封皮</td>
        <td>图书出版社</td>
    </tr>
    <tr>
        <td><%=book.getBookid()%></td>
        <td><%=book.getBookname()%></td>
        <td><%=book.getPrice()%></td>
        <td><%=book.getAuthor()%></td>
        <td><%=book.getPicture()%></td>
        <td><%=book.getPublish()%></td>
    </tr>
    <tr>
        <td colspan="3"></td>
        <td></td>
        <td colspan="2"></td>
    </tr>
</table>
<div style="text-align:center;font-size: 36px;">
    <a href="admin.jsp">图书列表</a>
</div>
</body>
</html>

admin_do_addbook.jsp

<%--
  Created by IntelliJ IDEA.
  User: shuijianshiqing
  Date: 2020/5/21
  Time: 23:45
  To change this template use File | Settings | File Templates.
--%>
<%@page import="com.sjsq.dao.impl.UserDaoImpl"%>
<%@page import="com.sjsq.dao.UserDao"%>
<%@page import="com.sjsq.po.User"%>
<%@ page import="com.sjsq.po.Book" %>
<%@ page import="com.sjsq.dao.BookDao" %>
<%@ page import="com.sjsq.dao.impl.BookDaoImpl" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>处理新增图书</title>
    <style type="text/css">
        #before{
            text-align: center;
        }
        #head{
            background: #eeeeee;height: 80px;
        }
        #headLink{
            font-size: 20px;
        }
        #headWelLink{
            font-size: 20px;
        }
    </style>
</head>
<body>
<%--头部信息--%>
<%
    User user =(User)session.getAttribute("user");
    if(user == null){
        response.sendRedirect("login.jsp");
    }else {
%>
<div id="head">
    <table width="100%">
        <td id="headWelLink">欢迎您:<%=user.getName()%></td>
        <td align="right" id="headLink">
            <a href="logout.jsp">安全退出</a>
        </td>
    </table>
</div>
<%
    }
%>

<%
    // 设置获取注册时的编码为UTF-8
    request.setCharacterEncoding("UTF-8");
    Book book=new Book();
    //获取admin_addbook.jsp页面提交的账号和密码
    String bookname=request.getParameter("bookname");
    Double price= Double.parseDouble(request.getParameter("price"));
    String author=request.getParameter("author");
    String publish=request.getParameter("publish");

    //获取admin_addbook.jsp页面提交的账号和密码设置到实体类User中
    book.setBookname(bookname);
    book.setPrice(price);
    book.setAuthor(author);
    book.setPublish(publish);

    System.out.println("添加的图书信息");
    System.out.println(book);

    //引入数据交互层
    BookDao dao=new BookDaoImpl();
    boolean flag=dao.addBook(book);


    if(flag){
        response.sendRedirect("admin.jsp");
    }else{
        response.sendRedirect("error.jsp");
    }
%>
</body>
</html>

admin_do_info.jsp

<%--
  Created by IntelliJ IDEA.
  User: shuijianshiqing
  Date: 2020/5/24
  Time: 10:51
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%@ page import="com.sjsq.po.Book" %>
<%@ page import="com.sjsq.service.BookService" %>
<%@ page import="com.sjsq.service.impl.BookServiceImpl" %>
<%
    // 获取绝对路径路径 ,开发项目一定要使用绝对路径,不然肯定出错
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":"
            + request.getServerPort() + path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <base href="<%=basePath %>" />
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>书籍信息</title>
</head>
<body>
<%
    Book book = new Book();
    String sid = request.getParameter("bookid");
    Integer id = Integer.parseInt(sid);
    BookService service = new BookServiceImpl();
    book.setBookid(id);
    Book bookCur = service.getBook(book);
    // 控制台打印出类的信息(日志的前身)
    System.out.print("admin_do_info.jsp的信息-->");
    System.out.println(bookCur);
    session.setAttribute("book", bookCur);
    response.sendRedirect("admin_detail.jsp");
%>
</body>
</html>

admin_do_updatebook.jsp

<%--
  Created by IntelliJ IDEA.
  User: shuijianshiqing
  Date: 2020/5/21
  Time: 23:45
  To change this template use File | Settings | File Templates.
--%>
<%@page import="com.sjsq.dao.impl.UserDaoImpl"%>
<%@page import="com.sjsq.dao.UserDao"%>
<%@page import="com.sjsq.po.User"%>
<%@ page import="com.sjsq.po.Book" %>
<%@ page import="com.sjsq.dao.BookDao" %>
<%@ page import="com.sjsq.dao.impl.BookDaoImpl" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>处理修改图书</title>
    <style type="text/css">
        #before{
            text-align: center;
        }
        #head{
            background: #eeeeee;height: 80px;
        }
        #headLink{
            font-size: 20px;
        }
        #headWelLink{
            font-size: 20px;
        }
    </style>
</head>
<body>

<%--头部信息--%>
<%
    User user =(User)session.getAttribute("user");
    if(user == null){
        response.sendRedirect("login.jsp");
    }else {
%>
<div id="head">
    <table width="100%">
        <td id="headWelLink">欢迎您:<%=user.getName()%></td>
        <td align="right" id="headLink">
            <a href="logout.jsp">安全退出</a>
        </td>
    </table>
</div>
<%
    }
%>

<%
    // 设置获取注册时的编码为UTF-8
    request.setCharacterEncoding("UTF-8");

    //获取admin_addbook.jsp页面提交的账号和密码
    Integer bookid=Integer.parseInt(request.getParameter("bookid"));

    String bookname = request.getParameter("bookname");
    Double price = Double.parseDouble(request.getParameter("price"));
    String author=request.getParameter("author");
    String publish=request.getParameter("publish");

    //引入数据交互层
    BookDao dao=new BookDaoImpl();
    Book book=new Book();
    book = dao.getBook(bookid);

    book.setBookname(bookname);
    book.setPrice(price);
    book.setAuthor(author);
    book.setPublish(publish);

    System.out.println("修改的图书信息");
    System.out.println(book);

    boolean flag=dao.updateBook(book);


    if(flag){
        response.sendRedirect("admin.jsp");
    }else{
        response.sendRedirect("error.jsp");
    }
%>
</body>
</html>

admin_update.jsp

<%@ page import="com.sjsq.po.Book" %>
<%@ page import="com.sjsq.dao.impl.BookDaoImpl" %>
<%@ page import="com.sjsq.dao.BookDao" %>
<%@ page import="com.sjsq.po.User" %><%--
  Created by IntelliJ IDEA.
  User: shuijianshiqing
  Date: 2020/5/21
  Time: 23:14
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>修改图书</title>
    <style type="text/css">
        h1{
            text-align: center;
        }
        h4{
            text-align: center;color: red;
        }
        body{
            background-color: antiquewhite;
        }
        div{
            text-align: center;
        }
        #before{
            text-align: center;
        }
        #head{
            background: #eeeeee;height: 80px;
        }
        #headLink{
            font-size: 20px;
        }
        #headWelLink{
            font-size: 20px;
        }
    </style>
</head>
<body>

<%--头部信息--%>
<%
    User user =(User)session.getAttribute("user");
    if(user == null){
        response.sendRedirect("login.jsp");
    }
%>

<h1>修改图书</h1>
<hr/>
<%
    //获取admin.jsp页面的bookid
    Integer bookid=Integer.parseInt(request.getParameter("bookid"));
    BookDao dao=new BookDaoImpl();
    Book book=new Book();
    book = dao.getBook(bookid);
%>
<form action="admin_do_updatebook.jsp" method="post" name="registerForm">
    <div>
        <tr>
            <input type="hidden" name="bookid" id="bookid" value="<%=book.getBookid()%>">
        </tr>
    </div>
    <div>
        <tr>
            <label>图书名称:</label>
            <input type="text" name="bookname" id="bookname" value="<%=book.getBookname()%>" autofocus="autofocus">
        </tr>
    </div>
    <div>
        <tr>
            <label>图书价格:</label></td>
            <input type="text" name="price" id="price" value="<%=book.getPrice()%>">
        </tr>
    </div>
    <div>
        <tr>
            <label>图书作者:</label>
            <input type="text" name="author" id="author" value="<%=book.getAuthor()%>">
        </tr>
    </div>
    <div>
        <tr>
            <label>出版公司:</label>
            <input type="text" name="publish" id="publish" value="<%=book.getPublish()%>">
        </tr>
    </div>

    <div>
        <tr>
            <button type="submit" onclick="return checkForm()">修改</button>
            <button type="reset">重置</button>
        </tr>
    </div>
</form>

<script type="text/javascript">
    function checkForm() {
        var bookname = registerForm.bookname.value;
        var price = registerForm.price.value;
        //alert(name + pwd + repwd);
        if (bookname == "" || bookname == null) {
            alert("请输入图书名称");
            registerForm.bookname.focus();
            return false;
        } else if (price == "" || price == null) {
            alert("请输入图书价格");
            registerForm.price.focus();
            return false;
        }
        alert('修改成功!');
        return true;
    }
</script>
</body>
</html>

error.jsp

<%--
  Created by IntelliJ IDEA.
  User: shuijianshiqing
  Date: 2020/6/10
  Time: 22:28
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>错误页面</title>
</head>
<body>
    操作出错,请回到上一个页面!
    </hr>

</body>
</html>

login.jsp

<%--
  Created by IntelliJ IDEA.
  User: shuijianshiqing
  Date: 2020/5/19
  Time: 22:44
  To change this template use File | Settings | File Templates.
--%>

<%@ page contentType="text/html;charset=UTF-8" language="java" 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" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <base href="<%=basePath %>" />
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>用户登录</title>
    <style type="text/css">
        h1{
            text-align: center;
        }
        h4{
            text-align: center;color: red;
        }
        body{
            background-color: antiquewhite;
        }
        a{
            text-decoration: none;font-size: 20px;color: black;
        }
        a:hover{
            text-decoration: underline;font-size: 24px;color: red;
        }
    </style>
</head>
<body>
    <form action="info.jsp" method="post">
        <h1>用户登录</h1>
        <h4>---正在美化中---</h4>
        <hr/>
        <table align="center">
            <tr>
                <td>账号:</td>
                <td><input type="text" name="name" id="name" placeholder="请输入您的账号" autofocus="autofocus"></td>
            </tr>
            <tr>
                <td>密码:</td>
                <td><input type="password" name="password" id="password" placeholder="请输入您的密码"></td>
                <td><a href="searchPassword.jsp">找回密码</a></td>
            </tr>
            <tr>
                <td colspan="1">
                </td>
                <td>
                    <input type="submit" value="登录"/>
                    <input type="reset" value="重置"/>
                    <a href="register.jsp" target="_blank">注册</a>
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

2.后端

User

package com.sjsq.po;

import java.io.Serializable;

/**
 * @author shuijianshiqing
 * @date 2020/5/19 22:19
 * 用户的实体类
 */

public class User implements Serializable {

    // 增加序列号,作用是反序列化的时候不会报错,切能进行IO的持久化
    public static final long serialVersionUID = 1L;
    private Integer id;
    private String name;
    private String password;
    private String email;
    private String phone;
    private Integer isadmin;

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public Integer getIsadmin() {
        return isadmin;
    }

    public void setIsadmin(Integer isadmin) {
        this.isadmin = isadmin;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", password='" + password + '\'' +
                ", email='" + email + '\'' +
                ", phone='" + phone + '\'' +
                ", isadmin=" + isadmin +
                '}';
    }
}

UserDaoImpl

package com.sjsq.dao.impl;

import com.sjsq.dao.UserDao;
import com.sjsq.po.User;
import com.sjsq.utils.BaseDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author shuijianshiqing
 * @date 2020/5/20 22:15
 * 实现UserDao接口
 */

public class UserDaoImpl implements UserDao {

    /**
     * 用户登录
     * @param user
     * @return
     */
    @Override
    public User login(User user) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            // 1.获取数据库连接
            con = BaseDao.getConnection();
            // 2.写sql
            String sql = "select * from user where name = ? and password = ?";
            // 3.预编译
            ps = con.prepareStatement(sql);
            // 4.设置值
            ps.setObject(1,user.getName());
            ps.setObject(2,user.getPassword());
            rs = ps.executeQuery();
            User users = null;
            if(rs.next()){
                users = new User();
                // 从数据库中获取值到实体类的setter方法中
                users.setId(rs.getInt("id"));
                users.setName(rs.getString("name"));
                users.setPassword(rs.getString("password"));
                users.setEmail(rs.getString("email"));
                users.setPhone(rs.getString("phone"));
                users.setIsadmin(rs.getInt("isadmin"));
                // 返回的是你查询出来的完整的对象
                return users;

            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            // 关闭资源,避免出现异常
            BaseDao.close(con,ps,rs);
        }
        return null;
    }

    /**
     * 用户注册
     * @param user
     * @return
     */
    @Override
    public boolean register(User user) {
        String sql = "insert into user values (0,?,?,?,?,0)";
        List<Object> list = new ArrayList<Object>();
        list.add(user.getName());
        list.add(user.getPassword());
        list.add(user.getEmail());
        list.add(user.getPhone());

        boolean flag = BaseDao.addUpdateDelete(sql,list.toArray());
        if(flag){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 查询用户信息
     * @param sql
     * @param arr
     * @return
     */
    @Override
    public List<User> selectUser(String sql, Object[] arr) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            // 1.连接数据库
            con = BaseDao.getConnection();
            // 2.预编译
            ps = con.prepareStatement(sql);
            if(arr != null){
                for (int i = 0; i < arr.length; i++) {
                    ps.setObject(i+1,arr[i]);
                }
            }
            // 3.执行sql
            rs = ps.executeQuery();
            // 4.保存查询出来的数据到list
            List<User> list = new ArrayList<User>();
            while(rs.next()){
                User user = new User();
                // 从数据库中获取值到实体类的setter方法中
                user.setId(rs.getInt("id"));
                user.setName(rs.getString("name"));
                user.setPassword(rs.getString("password"));
                user.setEmail(rs.getString("email"));
                user.setPhone(rs.getString("phone"));
                user.setIsadmin(rs.getInt("isadmin"));

                // 测试数据
                System.out.println("Dao层测试日志,显示用户信息:");
                System.out.println(user);
                // 将对象保存到list中
                list.add(user);
            }
            return list;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            // 关闭资源,避免出现异常
            BaseDao.close(con,ps,rs);
        }
        return null;
    }
}

BookDao

package com.sjsq.dao;

import com.sjsq.po.Book;

import java.util.List;

/**
 * @author shuijianshiqing
 * @date 2020/5/20 23:13
 * 图书信息接口
 */

public interface BookDao {

    /**
     * 查询图书信息
     * @param sql
     * @param arr
     * @return
     */
    public List<Book> select(String sql,Object[] arr);

    /**
     * 根据图书编号进行查询
     * @param id
     * @return
     */
    public Book getBook(Integer id);


    /**
     * 图书新增
     * @param book
     * @return
     */
    public boolean addBook(Book book);

    /**
     * 图书修改
     * @param book
     * @return
     */
    public boolean updateBook(Book book);

    /**
     * 删除图书
     * @param bookid
     * @return
     */
    public boolean deleteBook(Integer bookid);
}

BookDaoImpl

package com.sjsq.dao.impl;

import com.sjsq.dao.BookDao;
import com.sjsq.po.Book;
import com.sjsq.utils.BaseDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author shuijianshiqing
 * @date 2020/5/20 23:15
 */
public class BookDaoImpl implements BookDao {

    @Override
    public List<Book> select(String sql, Object[] arr) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            // 1.连接数据库
            con = BaseDao.getConnection();
            // 2.预编译
            ps = con.prepareStatement(sql);
            if(arr != null){
                for (int i = 0; i < arr.length; i++) {
                    // 传入sql的参数
                    ps.setObject(i+1,arr[i]);
                }
            }
            // 3.执行sql
            rs = ps.executeQuery();
            // 4.保存查询出来的数据到list
            List<Book> list = new ArrayList<Book>();
            while (rs.next()){
                Book book = new Book();
                book.setBookid(rs.getInt("bookid"));
                book.setBookname(rs.getString("bookname"));
                book.setPrice(rs.getDouble("price"));
                book.setAuthor(rs.getString("author"));
                book.setPicture(rs.getString("picture"));
                book.setPublish(rs.getString("publish"));

                list.add(book);
            }
            return list;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            // 关闭资源,避免出现异常
            BaseDao.close(con,ps,rs);
        }
        return null;
    }

    @Override
    public Book getBook(Integer id) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            // 1.连接数据库
            con = BaseDao.getConnection();
            // 2.预编译
            String sql = "select * from book where bookid = ?";
            ps = con.prepareStatement(sql);
            ps.setInt(1,id);
            // 3.执行sql
            rs = ps.executeQuery();
            while (rs.next()){
                Book book = new Book();
                book.setBookid(rs.getInt("bookid"));
                book.setBookname(rs.getString("bookname"));
                book.setPrice(rs.getDouble("price"));
                book.setAuthor(rs.getString("author"));
                book.setPicture(rs.getString("picture"));
                book.setPublish(rs.getString("publish"));

                return book;
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            // 关闭资源,避免出现异常
            BaseDao.close(con,ps,rs);
        }
        return null;
    }

    /**
     * 新增图书
     * @param book
     * @return
     */
    @Override
    public boolean addBook(Book book) {
        String sql = "insert into book values (0,?,?,?,null,?)";
        List<Object> list = new ArrayList<Object>();
        list.add(book.getBookname());
        list.add(book.getPrice());
        list.add(book.getAuthor());
        list.add(book.getPublish());

        boolean flag = BaseDao.addUpdateDelete(sql,list.toArray());
        if(flag){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 更新图书
     * @param book
     * @return
     */
    @Override
    public boolean updateBook(Book book) {
        String sql = "update book set bookname=?,price=?,author=?,publish=? where bookid=?";
        List<Object> list = new ArrayList<Object>();
        list.add(book.getBookname());
        list.add(book.getPrice());
        list.add(book.getAuthor());
        list.add(book.getPublish());
        list.add(book.getBookid());

        boolean flag = BaseDao.addUpdateDelete(sql,list.toArray());
        if(flag){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 删除图书
     * @param bookid
     * @return
     */
    @Override
    public boolean deleteBook(Integer bookid) {
        String sql = "delete from book where bookid=?";
        List<Object> list = new ArrayList<Object>();
        list.add(bookid);

        boolean flag = BaseDao.addUpdateDelete(sql,list.toArray());
        if(flag){
            return true;
        }else {
            return false;
        }
    }
}

BookServiceImpl

package com.sjsq.service.impl;

import com.sjsq.dao.BookDao;
import com.sjsq.dao.impl.BookDaoImpl;
import com.sjsq.po.Book;
import com.sjsq.service.BookService;

import java.util.ArrayList;
import java.util.List;

/**
 * @author shuijianshiqing
 * @date 2020/5/20 23:42
 * Book的Service实现层
 */
public class BookServiceImpl implements BookService {

    private BookDao dao = new BookDaoImpl();

    /**
     * 查询所有图书信息
     * @param book
     * @return
     */
    @Override
    public List<Book> select(Book book) {
        StringBuffer sql = new StringBuffer("select * from book where 1 = 1 ");
        List<Object> list = new ArrayList<Object>();
        if(book != null){
            // 根据bookid来查找对应的书籍
            if(book.getBookid() != null && book.getBookid() != 0){
                sql.append(" and bookid = ?");
                list.add(book.getBookid());
            }

            // 根据bookname来查找对应的书籍
            if(book.getBookname() != null){
                System.out.println("图书名称:"+book.getBookname());
                sql.append(" and bookname = ?");
                list.add(book.getBookname());
            }
        }
        return dao.select(sql.toString(),list.toArray());
    }

    /**
     * 根据图书编号查询信息
     * @param book
     * @return
     */
    @Override
    public Book getBook(Book book) {
        if(book.getBookid() != null && book.getBookid() != 0){
            return dao.getBook(book.getBookid());
        }
        return null;
    }
}

四、感悟及商务合作

实现管理增删改查用了一晚上时间,感觉代码越写越顺了。

1.项目相关

备注1:这个项目是基于上个项目写的,上个项目地址:
IDEA+Java+JSP+Mysql+Tomcat实现Web图书管理系统
备注2:要是不知道IDEA如何搭建WEB项目,请移步:
IDEA创建Web项目详细说明
备注3:要是您需要学生成绩管理系统(界面较友好),请移步:
学生成绩管理系统
备注4:要是侵犯到您的权益,请通知我删除此文章。

五、其他

1.其他系统实现

JavaWeb系统系列实现

Java+JSP实现学生图书管理系统
Java+JSP实现学生信息管理系统
Java+JSP实现用户信息管理系统
Java+Servlet+JSP实现航空订票系统
Java+Servlet+JSP实现房屋租赁管理系统
Java+Servlet+JSP实现学生选课管理系统
Java+Servlet+JSP实现宠物诊所管理系统
Java+Servlet+JSP实现学生成绩管理系统1
Java+Servlet+JSP实现学生成绩管理系统2
Java+SSM+JSP实现宠物商城系统
Java+SSM+Easyui实现网上考试系统
Java+SSM+Bootstrap实现学生信息管理系统
Java+Springboot+Mybatis+H-ui实现营销管理系统
Java+Springboot+Mybatis+Bootstrap实现网上商城系统
Java+Springboot+Mybatis+Bootstrap实现景区旅游管理系统

JavaSwing系统系列实现

Java+Swing实现医院管理系统
Java+Swing实现仓库管理系统
Java+Swing实现学生信息管理系统
Java+Swing实现学生宿舍管理系统
Java+Swing实现学生选课管理系统
Java+Swing实现电子相册管理系统
Java+Swing实现图书管理系统
Java+Swing实现斗地主游戏
Java+Swing实现宠物商店管理系统-TXT存储信息
Java+Swing实现学生成绩管理系统
Java+Swing实现企业人事管理系统
Java+Swing实现学校教材管理系统
Java+Swing实现学校教务管理系统
Java+Swing实现超市管理系统-TXT存储信息
Java+Swing实现考试管理系统

2.获取源码

源码包含管理员对用户的增删改查
Java+JSP+Mysql实现Web图书管理系统

3.备注

如有侵权请联系我删除。

4.鸡汤

勇敢走自己的路,相信自己的路。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

水坚石青

你的鼓励是我更新的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值