2020.11.25

一、今日学习内容

    今天对代码进行完善

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>首页</title>

</head>
<body>
    <%
          Object message = request.getAttribute("message");
          if(message!=null && !"".equals(message)){
      
     %>
          <script type="text/javascript">
               alert("<%=request.getAttribute("message")%>");
          </script>
     <%} %>
     <div align="center">
        <h1 style="color: black;">登录页面</h1>
        <div class="content">
        <div class="main">
        <form name="form" action="ManagerServelet?method=login" method="post" >
            <table>
                  <tr>
                    <td>用户ID</td>
                    <td><input type="text" id="IDuser" name="IDuser" placeholder="八位数字组成" /></td>
                </tr>
                <tr>
                    <td>密码</td>
                    <td><input type="password" id="userpassword" name="userpassword"/></td>
                </tr>  
                <tr>
                    <td>身份</td>
                    <td>
                         <select name="flag" class="shortselect">
                            <option value="读者">读者</option>
                            <option value="管理员">管理员</option>
                        </select>
                    </td>
                 </tr>
             <tr>
                 <td><button type="submit" class="b">提      交<tton></td>
             </tr>
         </table>
      </form>
      <script type="text/javascript"></script>
        </div>
    </div>
</body>
</html>
//bookadd
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
   <%
          Object message = request.getAttribute("message");
          if(message!=null && !"".equals(message)){
      
     %>
          <script type="text/javascript">
               alert("<%=request.getAttribute("message")%>");
          </script>
     <%} %>
     <div align="center">
        <h1 style="color: black;">添加新书信息</h1>
        <a href="manager.jsp">返回管理员页面</a>

    <div class="content">
        <div class="main">
        <form name="form" action="ManagerServelet?method=bookadd" method="post" >
            <table>
                <tr>
                    <td>图书编号</td>
                    <td><input type="text" id="IDbook" name="IDbook" placeholder="八位数字组成" /></td>
                </tr>
                <tr>
                    <td>书名</td>
                    <td><input type="text" id="bookname" name="bookname"/></td>
                </tr>   
                <tr>
                    <td>作者名</td>
                    <td><input type="text" id="bookwriter" name="bookwriter"/></td>
                </tr>   
                <tr>
                    <td>出版社名称</td>
                    <td><input type="text" id="bookchubanshe" name="bookchubanshe"/></td>
                </tr>  
                <tr>
                    <td>可借阅数量</td>
                    <td><input type="text" id="booknumber" name="booknumber"/></td>
                </tr>          
                <tr>
                    <td><button type="submit" class="b">提      交<tton></td>
                </tr>
            </table>
        </form>
<script type="text/javascript"></script>
        </div>
    </div>
</body>
</html>
//readeradd
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
   <%
          Object message = request.getAttribute("message");
          if(message!=null && !"".equals(message)){
      
     %>
          <script type="text/javascript">
               alert("<%=request.getAttribute("message")%>");
          </script>
     <%} %>
     <div align="center">
        <h1 style="color: black;">添加读者信息</h1>
        <a href="manager.jsp">返回管理员页面</a>

    <div class="content">
        <div class="main">
        <form name="form" action="ManagerServelet?method=readeradd" method="post" >
            <table>
                <tr>
                    <td>学号</td>
                    <td><input type="text" id="IDreader" name="IDreader" placeholder="八位数字组成" /></td>
                </tr>
                <tr>
                    <td>读者姓名</td>
                    <td><input type="text" id="readername" name="readername"/></td>
                </tr>               
                <tr>
                    <td>读者性别</td>
                    <td>
                    <input type="radio"name="readersex"value="男"checked><input type="radio"name="readersex"value="女"></td>
                </tr>
                <tr>
                    <td>所在学院</td>
                    <td><input type="text" id="readerxueyuan" name="readerxueyuan"/></td>
                </tr>   
                <tr>
                    <td><button type="submit" class="b">提      交<tton></td>
                </tr>
            </table>
        </form>
<script type="text/javascript"></script>
        </div>
    </div>
</body>
</html>
//manager
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>首页</title>

</head>
<body>
    <%
          Object message = request.getAttribute("message");
          if(message!=null && !"".equals(message)){
      
     %>
          <script type="text/javascript">
               alert("<%=request.getAttribute("message")%>");
          </script>
     <%} %>
     <div align="center">
        <h1 style="color: black;">管理员功能页面</h1>
        <a href="login.jsp">返回登录页面</a>

        <div class="a">
            <a href="mareaadd.jsp">添加读者信息</a>
        </div>
        <div class="a">
            <a href="mabookadd.jsp">添加新书信息</a>
        </div>
        <div class="a">
            <a href="mareturn.jsp">催还书目信息</a>
        </div>
    </div>
</body>
</html>
<%@ 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>Insert title here</title>
</head>
<body>
<%
          Object message = request.getAttribute("message");
          if(message!=null && !"".equals(message)){
      
     %>
          <script type="text/javascript">
               alert("<%=request.getAttribute("message")%>");
          </script>
     <%} %>
     <div align="center">
     <h1>图书信息</h1>
        <a href="reader.jsp">返回读者页</a>
        <table align="center">
            <tr>
                <th>图书编号</th>
                <th>书名</th>
                <th>作者名</th>
                <th>出版社名称</th>
                <th>可借阅数量</th>
            </tr>
                <c:forEach items="${bookss}" var="item">
                <tr>
                    <td>${item.IDbook}</td>
                    <td>${item.bookname}</a></td>
                    <td>${item.bookwriter}</td>
                    <td>${item.bookchubanshe}</td>
                    <td>${item.booknumber}</td>
                </tr>
                </c:forEach>
        </table>
    </div>
</body>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
          Object message = request.getAttribute("message");
          if(message!=null && !"".equals(message)){
      
     %>
          <script type="text/javascript">
               alert("<%=request.getAttribute("message")%>");
          </script>
     <%} %>
     <div align="center">
        <h1>查询图书</h1>
    <div class="content">
        <div class="main">
        <a href="reader.jsp">返回读者页</a>
        <br><br>
        <form name="form" action="ManagerServelet?method=chaxun" method="post" ">
            <a>书名</a>&nbsp;&nbsp;<input type="text" name="bookname" />
            <br><br>
            <a>作者名</a>&nbsp;&nbsp;<input type="text" name="bookwriter" />
            <br><br>
            <input type="submit" value="查找" /></td>
        </form>
        </div>
    </div>
</body>
</html>

servlet:

package Servelet;

import java.util.Date; 
import java.text.SimpleDateFormat; 

import java.io.IOException;
import java.util.List;

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 entity.Reader;
import entity.Book;
import entity.User;
import service.ManagerService;
@WebServlet("/ManagerServelet")
public class ManagerServelet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    ManagerService service = new ManagerService();

    /**
     * 方法选择
     */
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String method = req.getParameter("method");

        if ("readeradd".equals(method)) {
            readeradd(req, resp);
        } else if ("bookadd".equals(method)) {
            bookadd(req, resp);
        } else if("liulantushu".equals(method)) {
            liulantushu(req,resp);
        }else if("chaxun".equals(method)) {
            chaxun(req,resp);
        }else if("login".equals(method)) {
            login(req,resp);
        }else if("jieyue".equals(method)) {
            jieyue(req,resp);
        }
    }

    /**
     * 添加
     * @param req
     * @param resp
     * @throws IOException
     * @throws ServletException
     */
    private void readeradd(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        req.setCharacterEncoding("utf-8");
        //获取数据
        String IDreader=req.getParameter("IDreader");
        String readername=req.getParameter("readername");
        String readersex = req.getParameter("readersex");
        String readerxueyuan = req.getParameter("readerxueyuan");
        Reader reader=new Reader(IDreader,readername,readersex,readerxueyuan);
        //添加后消息显示
        if(service.readeradd(reader)) {
            req.setAttribute("message", "添加成功");
            req.getRequestDispatcher("mareaadd.jsp").forward(req,resp);
        } else {
            req.setAttribute("message", "名称重复,请重新录入");
            req.getRequestDispatcher("mareaadd.jsp").forward(req,resp);
        }
    }
    private void bookadd(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        req.setCharacterEncoding("utf-8");
        //获取数据
        String IDbook=req.getParameter("IDbook");
        String bookname=req.getParameter("bookname");
        String bookwriter = req.getParameter("bookwriter");
        String bookchubanshe = req.getParameter("bookchubanshe");
        String booknumber= req.getParameter("booknumber");
        Book book=new Book(IDbook,bookname,bookwriter,bookchubanshe,booknumber);
        //添加后消息显示
        if(service.bookadd(book)) {
            req.setAttribute("message", "添加成功");
            req.getRequestDispatcher("mabookadd.jsp").forward(req,resp);
        } else {
            req.setAttribute("message", "名称重复,请重新录入");
            req.getRequestDispatcher("mabookadd.jsp").forward(req,resp);
        }
    }
    private void liulantushu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        List<Book> bookss = service.liulantushu();
        req.setAttribute("bookss", bookss);
        req.getRequestDispatcher("liulantushu.jsp").forward(req, resp);
    
    }
    private void chaxun(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
        req.setCharacterEncoding("utf-8");
        String bookname=req.getParameter("bookname");
        String bookwriter = req.getParameter("bookwriter");
        List<Book> books = service.chaxun(bookname,bookwriter);
        if(books==null)
        {
            req.setAttribute("message", "没有该图书");
            req.getRequestDispatcher("chaxun.jsp").forward(req,resp);
        }
        else {
            req.setAttribute("books", books);
            req.getRequestDispatcher("list.jsp").forward(req,resp);
        }
    }
    private void login(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        req.setCharacterEncoding("utf-8");
        //获取数据
        String IDuser=req.getParameter("IDuser");
        String userpassword=req.getParameter("userpassword");
        String flag=req.getParameter("flag");
        User user=new User(IDuser,userpassword,flag);
        service.login(user);
        //添加后消息显示
        req.setAttribute("message", "登录成功");
        if(flag.equals("读者"))
            req.getRequestDispatcher("reader.jsp").forward(req,resp);
        if(flag.equals("管理员"))
            req.getRequestDispatcher("manager.jsp").forward(req,resp);
        }
    public void jieyue(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
         req.setCharacterEncoding("utf-8");
         
    }
}
//Dao
package Dao;

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

import entity.Reader;
import DBUtil.DBUtil;
import entity.Book;
import entity.User;
import DBUtil.DBUtil;

public class ManagerDao {

    /**
     * 添加
     * @param course
     * @return
     */
    public boolean readeradd(Reader reader) {
        String sql = "insert into reader(IDreader,readername,readersex,readerxueyuan) values('" + reader.getIDreader() + "','" + reader.getReadername() + "','" 
    + reader.getReadersex() +"','"+ reader.getReaderxueyuan()+ "')";
        //创建数据库链接
        Connection conn = DBUtil.getConn();
        Statement state = null;
        boolean f = false;
        int a = 0;

        try {
            state = conn.createStatement();
            state.executeUpdate(sql);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭连接
            DBUtil.close(state, conn);
        }

        if (a > 0) {
            f = true;
        }
        return f;
    }
    public boolean bookadd(Book book) {
        String sql = "insert into book(IDbook,bookname,bookwriter,bookchubanshe,booknumber) values('" + book.getIDbook() + "','" + book.getBookname() + "','" 
    +book.getBookwriter() +"','"+ book.getBookchubanshe()+"','"+book.getBooknumber()+ "')";
        //创建数据库链接
        Connection conn = DBUtil.getConn();
        Statement state = null;
        boolean f = false;
        int a = 0;

        try {
            state = conn.createStatement();
            state.executeUpdate(sql);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭连接
            DBUtil.close(state, conn);
        }

        if (a > 0) {
            f = true;
        }
        return f;
    }
    
    public boolean IDreader(String IDreader) {
        boolean flag = false;
        String sql = "select name from reader where IDreader = '" + IDreader + "'";
        Connection conn = DBUtil.getConn();
        Statement state = null;
        ResultSet rs = null;

        try {
            state = conn.createStatement();
            rs = state.executeQuery(sql);
            while (rs.next()) {
                flag = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, state, conn);
        }
        return flag;
    }
    public boolean IDbook(String IDbook) {
        boolean flag = false;
        String sql = "select name from book where IDbook = '" + IDbook + "'";
        Connection conn = DBUtil.getConn();
        Statement state = null;
        ResultSet rs = null;

        try {
            state = conn.createStatement();
            rs = state.executeQuery(sql);
            while (rs.next()) {
                flag = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, state, conn);
        }
        return flag;
    }
    
    public List<Book> liulantushu() {
        String sql = "select * from book";
        List<Book> list = new ArrayList<>();
        Connection conn = DBUtil.getConn();
        Statement state = null;
        ResultSet rs = null;

        try {
            state = conn.createStatement();
            rs = state.executeQuery(sql);
            Book bean = null;
            while (rs.next()) {
                String IDbook = rs.getString("IDbook");
                String bookname = rs.getString("bookname");
                String bookwriter=rs.getString("bookwriter");
                String bookchubanshe = rs.getString("bookchubanshe");
                String booknumber=rs.getString("booknumber");
                
                bean = new Book(IDbook,bookname,bookwriter,bookchubanshe,booknumber);
                list.add(bean);
                
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, state, conn);
        }
        
        return list;
    }
    
    public List<Book> search(String bookname,String bookwriter)
    {
        String sql = "select * from book where ";
        if (bookname != "") {
            sql += "bookname like '%" +bookname+ "%'";
        }
        else if (bookwriter != "") {
            sql += "bookwriter like '%" +bookwriter+ "%'";
        }
        List<Book> list = new ArrayList<>();
        Connection conn = DBUtil.getConn();
        Statement state = null;
        ResultSet rs = null;
        Book bean = null;
        try {
            state = conn.createStatement();
            rs = state.executeQuery(sql);
            while (rs.next()) {
                String IDbook1 = rs.getString("IDbook");
                String bookname1 = rs.getString("bookname");
                String bookwriter1=rs.getString("bookwriter");
                String bookchubanshe1 = rs.getString("bookchubanshe");
                String booknumber1=rs.getString("booknumber");
                bean =new Book(IDbook1,bookname1,bookwriter1,bookchubanshe1,booknumber1);
                list.add(bean);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, state, conn);
        }
        return list;
    }
    
    public Book getbybookname(String bookname) {
        String sql = "select * from book where bookname ='" + bookname + "'";
        Connection conn = DBUtil.getConn();
        Statement state = null;
        ResultSet rs = null;
        Book book = null;
        
        try {
            state = conn.createStatement();
            rs = state.executeQuery(sql);
            while (rs.next()) {
                String IDbook1 = rs.getString("IDbook");
                String bookwriter1=rs.getString("bookwriter");
                String bookchubanshe1 = rs.getString("bookchubanshe");
                String booknumber1=rs.getString("booknumber");
                book = new Book(IDbook1,bookname,bookwriter1,bookchubanshe1,booknumber1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, state, conn);
        }
        
        return book;
    }
    public Book getbybookwriter(String bookwriter) {
        String sql = "select * from book where bookwriter ='" + bookwriter + "'";
        Connection conn = DBUtil.getConn();
        Statement state = null;
        ResultSet rs = null;
        Book book = null;
        
        try {
            state = conn.createStatement();
            rs = state.executeQuery(sql);
            while (rs.next()) {
                String IDbook1 = rs.getString("IDbook");
                String bookname1 = rs.getString("bookname");
                String bookchubanshe1 = rs.getString("bookchubanshe");
                String booknumber1=rs.getString("booknumber");
                book = new Book(IDbook1,bookname1,bookwriter,bookchubanshe1,booknumber1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, state, conn);
        }        
        return book;
    }
    public boolean login(User user) {
        String sql = "insert into user(IDuser,userpassword,flag) values('" + user.getIDuser() + "','" + user.getUserpassword() + "','" 
                      + user.getFlag() + "')";
        //创建数据库链接
        Connection conn = DBUtil.getConn();
        Statement state = null;
        boolean f = false;
        int a = 0;

        try {
            state = conn.createStatement();
            state.executeUpdate(sql);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭连接
            DBUtil.close(state, conn);
        }

        if (a > 0) {
            f = true;
        }
        return f;
    }

}

二、遇到的问题

     还有一些功能没有实现,对于日期,不清楚怎么获取写入数据库

三、明日计划

    明天继续完善

posted @ 2020-11-25 14:27  小仙女W  阅读(81)  评论(0编辑  收藏  举报