2020.11.27收获

添加新书 (addbook.jsp)

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    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>
</head>
<body>
<%
         Object message = request.getAttribute("message");
         if(message!=null && !"".equals(message)){
     
    %>
         <script type="text/javascript">
              alert("<%=request.getAttribute("message")%>");
         </script>
    <%
    } 
   %>
  <table align="center" border="0px" cellpadding="10px" cellspacing="10px">
  <form action="Servlet?method=addBook"  method="post"  onsubmit="return check()">
 
   <tr>
    <td>唯一编号:</td>
    <td><input type="text" name="number" id="number"></td>
   </tr>
   <tr>
   <td>书名:</td>
   <td><input type="text" name="bookname" id="bookname"></td>
   </tr>
   
   <tr>
   <td>作者名</td>
  <td><input type="text" name="writername" id="writername"></td>
   </tr>

   <tr>
   <td>出版社名称:</td>
   <td><input type="text" name="publishname" id="publishname"></td>
   </tr>
 
  <tr>
   <td>可借阅数量:</td>
   <td><input type="text" name="shuliang" id="shuliang"></td>
   </tr>
    <tr align="center">
    <th colspan="2">
    <input type="submit" value="添加">
    </th>
    </tr>
     <tr>
  <td><a href="guanliyuan.jsp">返回菜单</a></td>
  </tr>
    </form>
</table>
</body>
</html>

 

Servlet.java

private void addBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            req.setCharacterEncoding("utf-8");
            String number=req.getParameter("number");
            String bookname = req.getParameter("bookname");
            String writername = req.getParameter("writername");
            String publishname=req.getParameter("publishname");
            int shuliang= Integer.parseInt(req.getParameter("shuliang"));
            Book book=new Book(number,bookname,writername,publishname,shuliang);
            if(dao.addBook(book)) {
                req.setAttribute("book",book);
                req.setAttribute("message","添加成功" );
                req.getRequestDispatcher("addbook.jsp").forward(req, resp);
            }else {
                req.setAttribute("message","书籍信息重复,请重新输入" );
                req.getRequestDispatcher("addbook.jsp").forward(req, resp);
            }
        }

 

dao.java

//添加图书
    public boolean addBook(Book book) {
        Connection conn = DBUtil.getConn();
        PreparedStatement pstmt = null;
        boolean f = false;
        int a = 0;
        try {
            String sql = "insert into book(number,bookname,writername,publishname,shuliang) value(?,?,?,?,?)";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, book.getNumber());
            pstmt.setString(2, book.getBookname());
            pstmt.setString(3, book.getWritername());
            pstmt.setString(4, book.getPublishname());
            pstmt.setLong(5, book.getShuliang());
            a = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(pstmt, conn);
        }
        if (a > 0)
            f = true;

        return f;
    }

    

 

添加读者信息大致相同

posted @ 2020-11-27 19:54  第厘  阅读(68)  评论(0编辑  收藏  举报