jsp图书管理

 index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>会员信息管理系统</title>
<script type="text/javascript" src="js/jquery-1.12.4.js"></script>
<style>
        table,
        table tr th,
        table tr td {
            border: 1px solid #cccccc;
            border-collapse: collapse; 
        }
        table{
            width: 100%;
        }
        .ddd{
        padding-left: 10%;
        padding-right: 10%;    
        height: 600px;
    }
    td{
        padding-left: 60px;
        padding-right: 60px;
    }
    </style>
</head>
<body>
    <div class="ddd">
        <table border="1" id="table">
            <tr>
                <th colspan="6">图书信息</th>
            </tr>
            <tr>
                <th>图书名称</th>
                <th>图书作者</th>
                <th>购买时间</th>
                <th>图书分类</th>
                <th>操作</th>
            </tr>
            <c:forEach var="i" items="${requestScope.bookmanage }">
                <tr>
                    <td hidden id="id2">${i.id }</td>
                    <td>${i.name }</td>
                    <td>${i.author }</td>
                    <td>${i.time }</td>
                    <td>
                    <c:if test="${i.type eq 1 }">计算机/软件</c:if>
                    <c:if test="${i.type eq 2 }">小说/文摘</c:if>
                    <c:if test="${i.type eq 3 }">杂项</c:if>
                    </td>
                    <td><a href="UpdateServlet?bid=${i.id }">删除</a></td>
                </tr>
            </c:forEach>
        </table>
        <h2><a href="insert.jsp">新增图书信息</a></h2>
    </div>
</body>
<script type="text/javascript">
    $(document).ready(function () {
        $("#table tr:even").not("tr:first").css("background","#80ffff");
        $("#table tr:eq(0)").css("background","#ffcccc");
        $("#table tr:eq(1)").css("background","#ffcc99");
    })
    
    $(".shanchu").click(function(){
        event.preventDefault();
        conn = confirm("是否删除");
        if(conn == true){
            var id2 = $("#id2").val();
            $.ajax({
                url:"UpdateServlet",
                type:"post",
                data:{"bid":id2},
                success:function(result){
                    if(result == "true"){
                        alert("删除成功!");
                    }
                }
            })
        }
        
    })
</script>
</html>

insert.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>增加图书信息</title>
<script type="text/javascript" src="js/jquery-1.12.4.js"></script>
<style type="text/css">
        table,
        table tr th,
        table tr td {
            border: 1px solid #cccccc;
            border-collapse: collapse; 
        }
        div{
            width: 300px;
            margin-left: 40%;
        }
        table{
            width: 100%;
        }
        
        #tijiao{
            margin-left: 20%;
        }
</style>
</head>
<body>
<div>
    <form action="UpdateServlet2" method="post" id="form1">
        <table id="table">
            <tr>
                <th colspan="2">会员信息</th>
            </tr>
            <tr id="main">
                <td>图书名称</td>
                <td><input type="text" name="name"/></td>
            </tr>
            <tr>
                <td>图书作者</td>
                <td><input type="text" name="author"/></td>
            </tr>
            <tr>
                <td>购买日期</td>
                <td><input type="text" name="time"/></td>
            </tr>
            <tr>
                <td>图书类别</td>
                <td>
                    <select name="type">
                        <option value="0">选择所属分类</option>
                        <option value="1">计算机/软件</option>
                        <option value="2">小说/文摘</option>
                        <option value="3">杂项</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <input type="button" value="增加图书" onclick="red()" id="tijiao"/>
                </td>
            </tr>
        </table>
    </form>
</div>
<script type="text/javascript">
    function red(){
        $.ajax({
            url: "InsertServlet",
            type: "post",
            data: $("#form1").serialize(),
            success:function(rasult){
                if("true"==rasult){
                    alert("添加成功!");
                    window.location.href="http://localhost:8080/BookManage";
                }else{
                    alert("添加失败!请重试!");
                }
            }
        })
    }
</script>

</body>
</html>

basedao.java

package com.liu.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class BaseDao {
    
    
    private final String URL = "jdbc:mysql://localhost:3306/bookmanage";
    private final String USERNAME = "root";
    private final String PASSWORD = "root";
    
    //数据库连接字符串
    public Connection connection() {
        Connection conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(URL,USERNAME,PASSWORD);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }
    
    //?通用增删改操??
    public int updatesql(String sql,Object[] parm){
        int rowcont = -1;
        PreparedStatement pstmt = null;
        Connection conn = null;
        try {
            conn = this.connection();
            pstmt = conn.prepareStatement(sql);
            int parm1 = parm.length;
            if(parm!=null) {
                for (int i = 0; i < parm1; i++) {
                    pstmt.setObject(i+1, parm[i]);
                }
            }
            rowcont = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            this.closeConn(conn, pstmt, null);
        }
        return rowcont;
    }
    
    //关闭服务
    public void closeConn(Connection conn,PreparedStatement pstmt , ResultSet rs) {
        try {
            if(rs != null)rs.close();
            if(pstmt != null)pstmt.close();
            if(conn != null)conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    //?查询封装
    public ResultSet executeQuery(String sql, Object[] parms) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = this.connection();
            pstmt = conn.prepareStatement(sql);
            if(parms!=null) {
                for (int i = 0; i < parms.length; i++) {
                    pstmt.setObject(i+1, parms[i]);
                }
            }
            rs = pstmt.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return rs;
    }
  
}

BookManageDao.java

 

package com.liu.dao;

import java.util.List;

import com.liu.entity.BookManage;

public interface BookManageDao {

    public List<BookManage> selectBookAll();

    public int insertBook(BookManage bm);

    public int deleteBook(int id);
}

BookManageDaolmpl.java

package com.liu.dao;

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

import com.liu.entity.BookManage;

public class BookManageDaoImpl extends BaseDao implements BookManageDao{

    @Override
    public List<BookManage> selectBookAll() {
        List<BookManage> list = new ArrayList<BookManage>();
        ResultSet rs = null ; 
        try {
            String sql  = "SELECT b_id,b_name,b_author,b_time,b_type FROM bookmanage";
            rs = this.executeQuery(sql, null);
            while(rs.next()) {
                BookManage bookm = new  BookManage(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getInt(5));
                list.add(bookm);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            this.closeConn(null, null, rs);
        }
        return list;
    }

    @Override
    public int insertBook(BookManage bm) {
        String sql = "INSERT INTO bookmanage(b_name,b_author,b_time,b_type) VALUES(?,?,?,?)";
        Object[] parm = {bm.getName(),bm.getAuthor(),bm.getTime(),bm.getType()};
        return this.updatesql(sql, parm);
    }

    @Override
    public int deleteBook(int id) {
        String sql = "DELETE FROM bookmanage WHERE b_id=?";
        Object[] parm = {id};
        return this.updatesql(sql, parm);
    }

}

BookManageService.java

package com.liu.service;

import java.util.List;

import com.liu.entity.BookManage;

public interface BookManageService {

    public List<BookManage> finSelectBookAll();

    public boolean finInsertBook(BookManage bm);

    public boolean finDeleteBook(int id);
}

BookManageServicelmpl.java

package com.liu.service;

import java.util.List;

import com.liu.dao.BookManageDao;
import com.liu.dao.BookManageDaoImpl;
import com.liu.entity.BookManage;

public class BookManageServiceImpl implements BookManageService{
    BookManageDao bmd = new BookManageDaoImpl();
    
    @Override
    public List<BookManage> finSelectBookAll() {
        return bmd.selectBookAll();
    }

    @Override
    public boolean finInsertBook(BookManage bm) {
        int row = bmd.insertBook(bm);
        if(row>0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean finDeleteBook(int id) {
        int row = bmd.deleteBook(id);
        if(row>0) {
            return true;
        }
        return false;
    }

}

BookManage.java

package com.liu.entity;

public class BookManage {
    private int id;
    private String name;
    private String author;
    private String time;
    private int type;
    public BookManage() {
        
    }
    
    public BookManage(String name,String author,String time,int type) {
        this.name = name;
        this.author = author;
        this.time = time ;
        this.type = type;
    }
    
    public BookManage(int id,String name,String author,String time,int type) {
        this.id = id;
        this.name = name;
        this.author = author;
        this.time = time ;
        this.type = type;
    }
    
    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 getTime() {
        return time;
    }
    public void setTime(String time) {
        this.time = time;
    }
    public int getType() {
        return type;
    }
    public void setType(int type) {
        this.type = type;
    }
}

InsertServlet.java

package com.liu.servlet;

import java.io.IOException;
import java.io.PrintWriter;

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 com.liu.entity.BookManage;
import com.liu.service.BookManageService;
import com.liu.service.BookManageServiceImpl;

@WebServlet("/InsertServlet")
public class InsertServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        
        BookManageService bms = new BookManageServiceImpl();
        String name = request.getParameter("name");
        String author = request.getParameter("author");
        String time = request.getParameter("time");
        int type = Integer.parseInt(request.getParameter("type"));
        System.out.println(type);
        BookManage bm = new BookManage(name,author,time,type);
        boolean row = bms.finInsertBook(bm);
        PrintWriter out = response.getWriter();
        if(row == true) {
            out.print("true");
        }else {
            out.print("false");
        }
        out.flush();
        out.close();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

SelectBookServlet.java

package com.liu.servlet;

import java.io.IOException;
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 com.liu.service.BookManageService;
import com.liu.service.BookManageServiceImpl;

@WebServlet("/SelectBookServlet")
public class SelectBookServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        
        BookManageService bms = new BookManageServiceImpl();
        request.setAttribute("bookmanage", bms.finSelectBookAll());
        request.getRequestDispatcher("index.jsp").forward(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

UpdateServlet.java

package com.liu.servlet;

import java.io.IOException;
import java.io.PrintWriter;

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 com.liu.service.BookManageService;
import com.liu.service.BookManageServiceImpl;

@WebServlet("/UpdateServlet")
public class UpdateServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");

        BookManageService bms = new BookManageServiceImpl();
        int id = Integer.parseInt(request.getParameter("bid"));
        System.out.println(id);
        boolean row = bms.finDeleteBook(id);
        response.sendRedirect("SelectBookServlet");
        PrintWriter out = response.getWriter();
        if (row == true) {
            out.print("删除成功!");
        } else {
            out.print("删除失败!请重试");
        }
        out.flush();
        out.close();

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

}

 

 

jsp图书管理

 

jsp图书管理

 jsp图书管理

 

jsp图书管理

上一篇:httpclient5:信任所有证书,调用公众号接口


下一篇:PHP出现 Notice: Undefined index:...的原因及解决办法