图书管理系统(JavaSE+MySQL+Tomcat

好像有那么点儿意思了…
我写的所有文章旨在交流分享,所以欢迎交流


一、随便讲点儿

1.1 Tomcat

Tomcat是一个Web应用服务器。轻量级的,开源的。
之前写的html文件啥的,只能在本地访问。
有了Tomcat,我们就可以把html文件部署到服务器上,然后别人就可以通过地址来访问我们的页面了。
Tomcat安装目录下的webapps文件夹下放我们的项目

1.2 HttpServlet和doGet方法

HttpServlet是一个类,用来写后端的东西的,核心是doGet方法

@WebServlet("/delete")//这里是浏览器访问时的url链接
public class DeleteBooksController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    //被访问时的操作。
    }
}

二、直接上代码(后端

2.1 JDBC工具类

package com.zwk_zsj.Project.utils;

import java.sql.*;

public class JDBCUtils {
    private static final String CLASSNAME="com.mysql.jdbc.Driver";
    private static final String URL="jdbc:mysql:///tsgsys?useSSL=false";
    private static final String USER="root";
    private static final String PASSWORD="7895123";

    static {
        try {
            Class.forName(CLASSNAME);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection(){
        Connection con = null;
        try {
            con = DriverManager.getConnection(URL,USER,PASSWORD);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return con;
    }

    public static void close(Connection con, PreparedStatement ps, ResultSet rst){
        if(rst !=null){
            try {
                rst.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(ps != null){
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(con != null){
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void close(Connection con, PreparedStatement ps){
        if(ps != null){
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(con != null){
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}

2.2 POJO 普通类

2.2.1 图书类

package com.zwk_zsj.Project.POJO;

import java.util.Objects;

public class Book {
    private int id;
    private String name;
    private String author;
    private String description;
    private double price;
    private int maxNum;
    private int nowNum;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Book book = (Book) o;
        return Double.compare(book.price, price) == 0 && Objects.equals(name, book.name) && Objects.equals(author, book.author);
    }

    public Book() {
    }
    public Book(int id, String name, String author, String description, double price, int maxNum, int nowNum) {
        this.id = id;
        this.name = name;
        this.author = author;
        this.description = description;
        this.price = price;
        this.maxNum = maxNum;
        this.nowNum = nowNum;
    }

    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", description='" + description + '\'' +
                ", price=" + price +
                ", maxNum=" + maxNum +
                ", nowNum=" + nowNum +
                '}';
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, author, price);
    }

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getMaxNum() {
        return maxNum;
    }

    public void setMaxNum(int maxNum) {
        this.maxNum = maxNum;
    }

    public int getNowNum() {
        return nowNum;
    }

    public void setNowNum(int nowNum) {
        this.nowNum = nowNum;
    }


}

2.2.2 用户类

package com.zwk_zsj.Project.POJO;

import java.util.Objects;

public class User {
    private String name;
    private String password;
    private int userType;

    public User(String name, String password, int userType) {
        this.name = name;
        this.password = password;
        this.userType = userType;
    }

    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 int getUserType() {
        return userType;
    }

    public void setUserType(int userType) {
        this.userType = userType;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User user = (User) o;
        return userType == user.userType && Objects.equals(name, user.name) && Objects.equals(password, user.password);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, password, userType);
    }

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

2.3 Dao层(操作数据

2.3.1 LoadData

package com.zwk_zsj.Project.Dao;

import com.zwk_zsj.Project.POJO.Book;
import com.zwk_zsj.Project.POJO.User;
import com.zwk_zsj.Project.utils.JDBCUtils;

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

public class LoadData {

    public static ArrayList<Book> findAllBooks(){
        ArrayList<Book> books = new ArrayList<>();
        Connection con = JDBCUtils.getConnection();
        PreparedStatement ps = null ;
        ResultSet rst = null;
        try {
            ps = con.prepareStatement("select * from books");
            rst = ps.executeQuery();
            while(rst.next()){
                books.add(new Book(rst.getInt("id"),
                        rst.getString("name"),
                        rst.getString("author"),
                        rst.getString("description"),
                        rst.getDouble("price"),
                        rst.getInt("max_num"),
                        rst.getInt("now_num")
                        ));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            JDBCUtils.close(con,ps,rst);
        }
        return books;
    }

    public static ArrayList<User> findUsersByNameAndPassword(String name,String passwords){
        ArrayList<User> users = new ArrayList<>();
        Connection con = JDBCUtils.getConnection();
        PreparedStatement ps = null ;
        ResultSet rst = null;
        try {
            ps = con.prepareStatement("select * from users where name=? and passwords=?");
            ps.setString(1,name);
            ps.setString(2,passwords);
            rst = ps.executeQuery();
            while(rst.next()){
                users.add(new User(rst.getString("name"), rst.getString("passwords"), rst.getInt("userType")));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            JDBCUtils.close(con,ps,rst);
        }
        return users;
    }

}

2.3.2 UpdateData

package com.zwk_zsj.Project.Dao;

import com.zwk_zsj.Project.POJO.Book;
import com.zwk_zsj.Project.utils.JDBCUtils;

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

public class UpdateData {

    public static void addBooks(ArrayList<Book> books,String name,String author,String description,double price,int add_num){
        Connection con = JDBCUtils.getConnection();
        Book newBook = new Book(0, name, author, description, price, add_num, add_num);
        boolean isIn = false;
        for (Book book:books) {
            if(book.equals(newBook)){
                isIn = true;
                break;
            }
        }
        PreparedStatement preparedStatement =null;
        if(isIn){
            try {
                preparedStatement = con.prepareStatement("update books set max_num=max_num+?,now_num=now_num+? where name=? and author=? and price=?");
                preparedStatement.setInt(1,add_num);
                preparedStatement.setInt(2,add_num);
                preparedStatement.setString(3,name);
                preparedStatement.setString(4,author);
                preparedStatement.setDouble(5,price);
                preparedStatement.execute();
                System.out.println("添加成功");
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                JDBCUtils.close(con,preparedStatement);
            }
        }else{
            try {
                preparedStatement = con.prepareStatement("insert into books values(null,?,?,?,?,?,?)");
                preparedStatement.setString(1,name);
                preparedStatement.setString(2,author);
                preparedStatement.setString(3,description);
                preparedStatement.setDouble(4,price);
                preparedStatement.setInt(5,add_num);
                preparedStatement.setInt(6,add_num);
                preparedStatement.execute();
                System.out.println("添加成功!");
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                JDBCUtils.close(con,preparedStatement);
            }

        }

    }
    public static boolean delete(ArrayList<Book> books,String name,String author,double price,int delete_num){
        boolean isDo=false;
        Book newBook = new Book(0, name, author, "description", price, 0, 0);
        boolean isIn = false;
        for (Book book:books) {
            if(book.equals(newBook)){
                isIn = true;
                break;
            }
        }
        Connection con = JDBCUtils.getConnection();
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        if(isIn){
            try {
                preparedStatement = con.prepareStatement("select * from books where name=? and author=? and price=?");
                preparedStatement.setString(1,name);
                preparedStatement.setString(2,author);
                preparedStatement.setDouble(3,price);
                resultSet = preparedStatement.executeQuery();
                while(resultSet.next()){
                    int max_num = resultSet.getInt("max_num");
                    int now_num = resultSet.getInt("now_num");
                    if(delete_num <= now_num){
                        preparedStatement = con.prepareStatement("update books set max_num=max_num-?,now_num=now_num-? where name='"+name+"' and "+"author='"+author+"' and price="+price);
                        preparedStatement.setInt(1,delete_num);
                        preparedStatement.setInt(2,delete_num);
                        preparedStatement.execute();
                        isDo=true;
                    }
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            finally {
                JDBCUtils.close(con,preparedStatement,resultSet);
            }
        }
        return isDo;
    }
    public static boolean borrow(ArrayList<Book> books,String name,String author,double price){
        boolean isOK=false;
        Connection con = JDBCUtils.getConnection();
        Book newBook = new Book(0, name, author, "", price, 0, 0);
        int nowNum=0;
        for(Book book:books){
            if(newBook.equals(book)){
                nowNum = book.getNowNum();
            }
        }
        PreparedStatement preparedStatement = null;
        if(nowNum>0){
            try{
                preparedStatement = con.prepareStatement("update books set now_num = now_num-1 where name=? and author=? and price=?");
                preparedStatement.setString(1,name);
                preparedStatement.setString(2,author);
                preparedStatement.setDouble(3,price);
                preparedStatement.execute();
                isOK=true;
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                JDBCUtils.close(con,preparedStatement);
            }
        }

        return isOK;
    }
    public static boolean returnBook(ArrayList<Book> books,String name,String author,double price){
        boolean isOK=false;
        Connection con = JDBCUtils.getConnection();
        Book newBook = new Book(0, name, author, "", price, 0, 0);
        int nowNum=0;int maxNum=0;
        for(Book book:books){
            if(newBook.equals(book)){
                nowNum = book.getNowNum();
                maxNum = book.getMaxNum();
            }
        }
        PreparedStatement preparedStatement = null;
        if(maxNum-nowNum>0){
            try{
                preparedStatement = con.prepareStatement("update books set now_num = now_num+1 where name=? and author=? and price=?");
                preparedStatement.setString(1,name);
                preparedStatement.setString(2,author);
                preparedStatement.setDouble(3,price);
                preparedStatement.execute();
                isOK=true;
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                JDBCUtils.close(con,preparedStatement);
            }
        }
        return isOK;
    }

}

2.4 Controller控制层

2.4.1 登录

package com.zwk_zsj.Project.Controller;

import com.zwk_zsj.Project.Dao.LoadData;
import com.zwk_zsj.Project.POJO.User;

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 java.io.IOException;
import java.util.ArrayList;

@WebServlet("/login")
public class LoginController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userName = req.getParameter("userName");
        String userPassword = req.getParameter("userPassword");
        ArrayList<User> users = LoadData.findUsersByNameAndPassword(userName, userPassword);
        if(users.size() == 1){
            //登陆成功
            int  userType = users.get(0).getUserType();
            if (userType == 0){
                //管理员登录
                resp.sendRedirect("http://localhost:8080/Project/GLYMenu.html");
            }else if(userType == 1){
                //普通用户登录
                resp.sendRedirect("http://localhost:8080/Project/YHMenu.html");
            }else{
                //发生未知错误,账号数据出问题了
                resp.sendRedirect("http://localhost:8080/Project/Error.html");
            }
        }else{
            resp.sendRedirect("http://localhost:8080/Project/login.html");
        }
    }
}

2.4.2 获取图书列表

package com.zwk_zsj.Project.Controller;

import com.alibaba.fastjson.JSON;
import com.zwk_zsj.Project.Dao.LoadData;
import com.zwk_zsj.Project.POJO.Book;

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 java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

@WebServlet("/selectBooks")
public class SelectBooksController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException,IOException {
        ArrayList<Book> books = LoadData.findAllBooks();
        String s = JSON.toJSONString(books);
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter w = resp.getWriter();
        w.write(s);
        w.flush();
        w.close();
    }
}

2.4.3 增加图书

package com.zwk_zsj.Project.Controller;

import com.zwk_zsj.Project.Dao.LoadData;
import com.zwk_zsj.Project.Dao.UpdateData;
import com.zwk_zsj.Project.POJO.Book;

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 java.io.IOException;
import java.util.ArrayList;

@WebServlet("/add")
public class AddBooksController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        String author = req.getParameter("author");
        String description = req.getParameter("description");
        double price = Double.parseDouble(req.getParameter("price")) ;
        int add_num = Integer.parseInt(req.getParameter("add_num"));
        ArrayList<Book> books = LoadData.findAllBooks();
        UpdateData.addBooks(books,name,author,description,price,add_num);
    }
}

2.4.4 删除图书

package com.zwk_zsj.Project.Controller;

import com.zwk_zsj.Project.Dao.LoadData;
import com.zwk_zsj.Project.Dao.UpdateData;
import com.zwk_zsj.Project.POJO.Book;

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 java.io.IOException;
import java.util.ArrayList;

@WebServlet("/delete")
public class DeleteBooksController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        String author = req.getParameter("author");
        String price = req.getParameter("price");
        String delNum = req.getParameter("delNum");
        if(!price.equals("") && ! delNum.equals("")){
            double priceDouble = Double.parseDouble(price);
            int delNumInt = Integer.parseInt(delNum);
            ArrayList<Book> books = LoadData.findAllBooks();
            boolean isDelete = UpdateData.delete(books, name, author, priceDouble, delNumInt);
            if(isDelete){
                resp.sendRedirect("http://localhost:8080/Project/delete.html");
            }else{
                resp.sendRedirect("http://localhost:8080/Project/error.html");
            }
        }else{
            resp.sendRedirect("http://localhost:8080/Project/delete.html");
        }

    }
}

2.4.5 借阅图书

package com.zwk_zsj.Project.Controller;

import com.zwk_zsj.Project.Dao.LoadData;
import com.zwk_zsj.Project.Dao.UpdateData;
import com.zwk_zsj.Project.POJO.Book;

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 java.io.IOException;
import java.util.ArrayList;

@WebServlet("/borrow")
public class BorrowBookController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        String author = req.getParameter("author");
        double price = Double.parseDouble(req.getParameter("price"));
        ArrayList<Book> books = LoadData.findAllBooks();
        boolean isOK = UpdateData.borrow(books, name, author, price);
        if(isOK){
            resp.sendRedirect("http://localhost:8080/Project/YHMenu.html");
        }else{
            resp.sendRedirect("http://localhost:8080/Project/error.html");
        }
    }
}

2.4.6 归还图书

package com.zwk_zsj.Project.Controller;

import com.zwk_zsj.Project.Dao.LoadData;
import com.zwk_zsj.Project.Dao.UpdateData;
import com.zwk_zsj.Project.POJO.Book;

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 java.io.IOException;
import java.util.ArrayList;

@WebServlet("/return")
public class ReturnBookController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        String author = req.getParameter("author");
        double price = Double.parseDouble(req.getParameter("price"));
        ArrayList<Book> books = LoadData.findAllBooks();
        boolean isOK = UpdateData.returnBook(books, name, author, price);
        if(isOK){
            resp.sendRedirect("http://localhost:8080/Project/YHMenu.html");
        }else{
            resp.sendRedirect("http://localhost:8080/Project/error.html");
        }
    }
}

三、直接上代码(前端

很丑的前端,因为只是写了html文件实现了功能,后面学了Vue和ELementUI以后可以套别人的前端模板快速开发

3.1 登录

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>idiot图书馆登录界面</title>
</head>
<body>
<div style="align-content: center">
    <div id="nb" style="font-size: 25px">想体验皇帝般的感觉吗?赶紧注册吧!一个人悄悄的打开!</div><br>
    <form action="http://localhost:8081/login" id="form">
        账号:<input type="text" id="userIn" onblur="tips()" name="userName">
        <div id="tip"  style="color:rebeccapurple;font-size: 10px">账号以字母开头,不少于6位</div><br>
        密码:<input type="password" id="userPassword"  onblur="pwdTips()" name="userPassword"> <br>
        <div id="passwordTip"  style="color:rebeccapurple;font-size: 10px">密码不少于6位</div><br>
        <button onclick="LoginResponse()">赶紧提交 </button>
    </form>

</div>
</body>
</html>
<script>
    var colors = ["red","orange","yellow","green","blue","aqua","purple"];
    var i =0;
    function nb(){
        document.getElementById("nb").style.color = colors[i++];
        if(i === colors.length){
            i=0;
        }
    }
    setInterval(nb,100);

    var userIsOK = false;
    var reg = /^[a-zA-Z]\w{5,}$/
    function tips(){
        userIsOK = false;
        var userIn = document.getElementById("userIn").value;
        var divTip = document.getElementById("tip");
        if(userIn.length===0){
            divTip.innerHTML="账号不能为空!"
        }else if(reg.test(userIn)){
            userIsOK = true;
            divTip.innerHTML="账号可用"
        }else{
            divTip.innerHTML="账号格式错误!以字母开头,不少于6位"
        }
    }

    var isOK = false;
    var pwReg = /^\w{6,}$/
    function pwdTips(){
        isOK = false;
        var userIn = document.getElementById("userPassword").value;
        var divTip = document.getElementById("passwordTip");
        if(userIn.length===0){
            divTip.innerHTML="密码不能为空!"
        }else if(pwReg.test(userIn)){
            isOK = true;
            divTip.innerHTML="密码可用"
        }else{
            divTip.innerHTML="账号格式错误!不少于6位"
        }
    }

    function LoginResponse(){
        if(isOK && userIsOK){
            setTimeout(function(){
                document.getElementById("form").submit();
            },2000);
        }else{
            alert("账号或者密码错误!")
        }
    }

</script>

3.2 管理员界面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>管理员界面</title>
</head>
<body>
    <h1 style="color:red;align-self: center">欢迎管理员登录!</h1>
    <table align="center" border="1px">
        <tr>
            <td><a href="http://localhost:8081/select.html">查看图书</a></td>
            <td><a href="http://localhost:8081/add.html">增加图书</a></td>
            <td><a href="http://localhost:8081/delete.html">删除图书</a></td>
            <td><a href="http://localhost:8081/login.html">退出登录</a></td>
        </tr>
    </table>
</body>
</html>

3.3 查看图书

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>查看图书</title>
</head>
<body>
<table id="table" align="center" border="1px">
    <tr>
        <td>书名</td><td>作者</td><td>描述</td><td>售价</td><td>库存</td>
    </tr>
</table>
</body>
</html>
<script>
    var xmlhttp;
    if (window.XMLHttpRequest)
        xmlhttp=new XMLHttpRequest();

    xmlhttp.open("GET","http://localhost:8081/selectBooks",true);

    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState===4 && xmlhttp.status===200)
        {
            const data = JSON.parse(xmlhttp.responseText);

            var table = document.getElementById("table");
            for (let i = 0; i < data.length; i++) {
                var _name = document.createTextNode(data[i].name);
                var _author = document.createTextNode(data[i].author);
                var _description = document.createTextNode(data[i].description);
                var _price = document.createTextNode(data[i].price);
                var _now_num = document.createTextNode(data[i].nowNum);
                var td_name = document.createElement("td");
                var td_author = document.createElement("td");
                var td_description = document.createElement("td");
                var td_price = document.createElement("td");
                var td_now_num = document.createElement("td");
                td_name.append(_name);
                td_author.append(_author);
                td_description.append(_description);
                td_price.append(_price);
                td_now_num.append(_now_num);
                var tr = document.createElement("tr");
                tr.append(td_name);
                tr.append(td_author);
                tr.append(td_description);
                tr.append(td_price);
                tr.append(td_now_num);
                table.append(tr);
            }

        }
    }

    xmlhttp.send();

</script>

3.4 增加图书

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>添加图书</title>
</head>
<body>
<table id="table" align="center" border="1px">
    <tr>
        <td>书名</td><td>作者</td><td>描述</td><td>售价</td><td>库存</td>
    </tr>
</table>
<form action="http://localhost:8081/add">
    <input type="text" placeholder="请输入要添加的书名" name="name">
    <input type="text" placeholder="请输入添加书的作者" name="author">
    <input type="text" placeholder="请输入添加书的描述" name="description">
    <input type="text" placeholder="请输入添加书的价格" name="price">
    <input type="text" placeholder="请输入要添加的数量" name="add_num">
    <input type="submit" value="我不检查数据合不合法了啊">
</form>
</body>
</html>
<script>
    var xmlhttp;
    if (window.XMLHttpRequest)
        xmlhttp=new XMLHttpRequest();

    xmlhttp.open("GET","http://localhost:8081/selectBooks",true);

    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState===4 && xmlhttp.status===200)
        {
            const data = JSON.parse(xmlhttp.responseText);

            var table = document.getElementById("table");
            for (let i = 0; i < data.length; i++) {
                var _name = document.createTextNode(data[i].name);
                var _author = document.createTextNode(data[i].author);
                var _description = document.createTextNode(data[i].description);
                var _price = document.createTextNode(data[i].price);
                var _now_num = document.createTextNode(data[i].nowNum);
                var td_name = document.createElement("td");
                var td_author = document.createElement("td");
                var td_description = document.createElement("td");
                var td_price = document.createElement("td");
                var td_now_num = document.createElement("td");
                td_name.append(_name);
                td_author.append(_author);
                td_description.append(_description);
                td_price.append(_price);
                td_now_num.append(_now_num);
                var tr = document.createElement("tr");
                tr.append(td_name);
                tr.append(td_author);
                tr.append(td_description);
                tr.append(td_price);
                tr.append(td_now_num);
                table.append(tr);
            }

        }
    }

    xmlhttp.send();

</script>

3.5 删除图书

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>删除图书</title>
</head>
<body>
<table id="table" border="1px" style="position: absolute;margin-left:5%;margin-right:5%;width:90%;z-index:0;display: block">
    <tr>
        <td>书名</td><td>作者</td><td>描述</td><td>售价</td><td>库存</td>
    </tr>
    <button onclick="showForm()">删除图书</button>
</table>

<form action="http://localhost:8081/delete" id="form" style="border: 1px red solid;background-color:darkorchid;position: absolute;margin-left:30%;margin-right:30%;width:40%;z-index:1;display: none">
    书名:<input type="text" name="name"> <br>
    作者:<input type="text" name="author"> <br>
    价格:<input type="text" name="price"> <br>
    数量:<input type="text" name="delNum"> <br>
    <button onclick="hiddenForm()">取消</button> <input type="submit" value="确认删除"> <br>
</form>
</body>
</html>
<script>
    var xmlhttp;
    if (window.XMLHttpRequest)
        xmlhttp=new XMLHttpRequest();

    xmlhttp.open("GET","http://localhost:8081/selectBooks",true);

    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState===4 && xmlhttp.status===200)
        {
            const data = JSON.parse(xmlhttp.responseText);

            var table = document.getElementById("table");
            for (let i = 0; i < data.length; i++) {
                var _name = document.createTextNode(data[i].name);
                var _author = document.createTextNode(data[i].author);
                var _description = document.createTextNode(data[i].description);
                var _price = document.createTextNode(data[i].price);
                var _now_num = document.createTextNode(data[i].nowNum);

                var td_name = document.createElement("td");
                var td_author = document.createElement("td");
                var td_description = document.createElement("td");
                var td_price = document.createElement("td");
                var td_now_num = document.createElement("td");

                td_name.append(_name);
                td_author.append(_author);
                td_description.append(_description);
                td_price.append(_price);
                td_now_num.append(_now_num);

                var tr = document.createElement("tr");
                tr.append(td_name);
                tr.append(td_author);
                tr.append(td_description);
                tr.append(td_price);
                tr.append(td_now_num);

                table.append(tr);

            }

        }
    }

    xmlhttp.send();

    function hiddenForm(){
        var form = document.getElementById("form");
        form.style.display="none";
    }
    function showForm(){
        var form = document.getElementById("form");
        form.style.display="block";
    }

</script>

3.6 用户界面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户界面</title>
</head>
<body>
<table id="table" align="center" border="1px">
    <tr>
        <td>书名</td><td>作者</td><td>描述</td><td>售价</td><td>库存</td><td>借阅</td><td>归还</td>
    </tr>
</table>
</body>
</html>
<script>
    var xmlhttp;
    if (window.XMLHttpRequest)
        xmlhttp=new XMLHttpRequest();

    xmlhttp.open("GET","http://localhost:8081/selectBooks",true);

    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState===4 && xmlhttp.status===200)
        {
            const data = JSON.parse(xmlhttp.responseText);

            var table = document.getElementById("table");
            for (let i = 0; i < data.length; i++) {
                var _name = document.createTextNode(data[i].name);
                var _author = document.createTextNode(data[i].author);
                var _description = document.createTextNode(data[i].description);
                var _price = document.createTextNode(data[i].price);
                var _now_num = document.createTextNode(data[i].nowNum);


                var a = document.createElement("a");
                a.innerText="借阅";
                a.href = "http://localhost:8081/borrow?name=" + data[i].name+
                    "&author=" + data[i].author+
                    "&price=" + data[i].price ;
                var b = document.createElement("a");
                b.innerText="归还";
                b.href = "http://localhost:8081/return?name=" + data[i].name+
                    "&author=" + data[i].author+
                    "&price=" + data[i].price ;

                var td_name = document.createElement("td");
                var td_author = document.createElement("td");
                var td_description = document.createElement("td");
                var td_price = document.createElement("td");
                var td_now_num = document.createElement("td");
                var td_a = document.createElement("td");
                var td_b = document.createElement("td");

                td_name.append(_name);
                td_author.append(_author);
                td_description.append(_description);
                td_price.append(_price);
                td_now_num.append(_now_num);
                td_a.append(a);
                td_b.append(b);

                var tr = document.createElement("tr");
                tr.append(td_name);
                tr.append(td_author);
                tr.append(td_description);
                tr.append(td_price);
                tr.append(td_now_num);
                tr.append(td_a);
                tr.append(td_b);

                table.append(tr);
            }

        }
    }

    xmlhttp.send();

</script>

3.7 报错

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>发生错误</title>
</head>
<body>
<h1>发生了错误</h1>
</body>
</html>

嘿嘿嘿,我越来越厉害啦!

  • 7
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值