• 图书管理系统


    图书管理系统

    BaseDao.java

    package com.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;
        }
      
    }

    BookMannageDao.java

    package com.dao;
    
    import java.util.List;
    
    import com.entity.BookManage;
    
    public interface BookManageDao {
    
        public List<BookManage> selectBookAll();
    
        public int insertBook(BookManage bm);
    
        public int deleteBook(int id);
    }

    BookManageDaolmpl.java

    package com.dao;
    
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.List;
    
    import com.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);
        }
    
    }

    BookManage.java

    package com.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;
        }
    }

    BookManageService.java

    package com.service;
    
    import java.util.List;
    
    import com.entity.BookManage;
    
    public interface BookManageService {
        
        /**
         * 查询说有图书信息
         * @return
         */
        public List<BookManage> finSelectBookAll();
        
        /**
         * 增加图书信息
         * @param bm
         * @return    true增加成功    false 增加失败
         */
        public boolean finInsertBook(BookManage bm);
        
        /**
         * 删除指定的图书信�?
         * @param id
         * @return    true删除成功    false 删除失败
         */
        public boolean finDeleteBook(int id);
    }

    InsertServlet.java

    package com.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.entity.BookManage;
    import com.service.BookManageService;
    import com.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);
        }
    
    }

    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{
                 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","#007FFF");
            $("#table tr:eq(0)").css("background","#007FFF");
            $("#table tr:eq(1)").css("background","#C0C0C0");
        })
        
        $(".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{
                 300px;
                margin-left: 40%;
            }
            table{
                 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>

     

  • 相关阅读:
    现代程序设计 作业1
    现代程序设计课程简介
    ubuntu 下解决安装包依赖问题
    centos下安装nginx和php-fpm
    如何在本机上将localhost改为www.dev.com
    神器
    脚本监测 前十名磁盘空间用户的列表
    使用xml来显示获取的mysql数据
    linux使用脚本自动连接数据库
    shell script的连接符是逗号,不是英文的句号
  • 原文地址:https://www.cnblogs.com/LILY321/p/14766350.html
Copyright © 2020-2023  润新知