• 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;
        }
    
        

    添加读者信息大致相同

  • 相关阅读:
    redhat 7.2 内网安装docker
    使用dockerfile 创建ubuntu ssh镜像
    docker 离线环境安装oracle
    redhat 6.6 、7、Centos7离线安装docker
    用命令行管理aws s3
    Anaconda介绍、安装及使用教程
    python2 编码问题万能钥匙
    从mongo数据库中导出数据的方法
    MongoDB学习第三篇 --- Insert操作
    MongoDB学习笔记(一)-Insert操作
  • 原文地址:https://www.cnblogs.com/ltw222/p/14067757.html
Copyright © 2020-2023  润新知