zoukankan      html  css  js  c++  java
  • 基于图书管理系统的改

    entity层和DBUtil层同增的操作,今天是修改操作(代码部分加上之前的):

    dao层:

      1 package dao;
      2 
      3 import java.sql.Connection;
      4 import java.sql.PreparedStatement;
      5 import java.sql.ResultSet;
      6 import java.sql.SQLException;
      7 import java.sql.Statement;
      8 import java.util.ArrayList;
      9 import java.util.List;
     10 
     11 import DBUtil.DBUtil;
     12 import entity.Book;
     13 import entity.Lendbook;
     14 
     15 
     16 
     17 
     18 public class Dao {
     19     //根据书的姓名查找
     20     public Book getbyname(String bname) {
     21         String sql = "select * from Book where bname ='" + bname + "'";
     22         Connection conn = DBUtil.getConn();
     23         Statement state = null;
     24         ResultSet rs = null;
     25         Book b = null;
     26         
     27         try {
     28             state = conn.createStatement();
     29             rs = state.executeQuery(sql);
     30             while (rs.next()) {
     31                 int id = rs.getInt("id");
     32                 String bianhao = rs.getString("bianhao");
     33                 String bname2 = rs.getString("bname");
     34                 String wname = rs.getString("wname");
     35                 String bhome=rs.getString("bhome");
     36                 int num = rs.getInt("num");
     37                 b = new Book(id,bianhao,bname2,wname,bhome,num);
     38             }
     39         } catch (Exception e) {
     40             e.printStackTrace();
     41         } finally {
     42             DBUtil.close(rs, state, conn);
     43         }
     44         
     45         return b;
     46     }
     47 
     48     //添加新书信息
     49     public boolean addBook(Book stu) {
     50         Connection conn = DBUtil.getConn();
     51         PreparedStatement pstmt = null;
     52         boolean f = false;
     53         int a = 0;
     54         try {
     55             String sql = "insert into Book(bianhao,bname,wname,bhome,num) value(?,?,?,?,?)";
     56             pstmt = conn.prepareStatement(sql);
     57             pstmt.setString(1, stu.getBianhao());
     58             pstmt.setString(2, stu.getBname());
     59             pstmt.setString(3, stu.getWname());
     60             pstmt.setString(4, stu.getBhome());
     61             pstmt.setLong(5, stu.getNum());
     62             a = pstmt.executeUpdate();
     63         } catch (SQLException e) {
     64             e.printStackTrace();
     65         } finally {
     66             DBUtil.close(pstmt, conn);
     67         }
     68         if (a > 0)
     69             f = true;
     70 
     71         return f;
     72     }
     73     //删除图书信息,根据书名删除
     74      public boolean deleteBook(String bname)
     75      {
     76          String sql="delete from Book where bname='" + bname + "'";
     77          Connection conn = DBUtil.getConn();
     78          Statement state = null;
     79          int a = 0;
     80          boolean f = false;
     81          try {
     82              state = conn.createStatement();
     83              a = state.executeUpdate(sql);
     84          } catch (SQLException e) {
     85              e.printStackTrace();
     86          } finally {
     87              DBUtil.close(state, conn);
     88          }
     89          
     90          if (a > 0) {
     91              f = true;
     92          }
     93          return f;
     94      }
     95 
     96     //更新图书信息,获取的是书的姓名
     97     public boolean updateBook(Book stu,String old_bname) {
     98         String sql = "update Book set bianhao='" + stu.getBianhao() + "', bname='" + stu.getBname() + "', wname='"
     99                 + stu.getWname() + "',bhome='" + stu.getBhome() + "',num='" + stu.getNum() + "'where bname='"+old_bname+"'";
    100         Connection conn = DBUtil.getConn();
    101         Statement state = null;
    102         boolean f = false;
    103         int a = 0;
    104         try {
    105             state = conn.createStatement();
    106             System.out.println("修改成功");
    107             a = state.executeUpdate(sql);
    108             System.out.println(a);
    109         } catch (SQLException e) {
    110             e.printStackTrace();
    111         } finally {
    112             DBUtil.close(state, conn);
    113         }
    114 
    115         if (a > 0) {
    116             f = true;
    117         }
    118 
    119         System.out.println(f);
    120         return f;
    121     }
    122     //浏览图书信息
    123     public List<Book> liulanbook() {
    124         String sql = "select * from Book";
    125         List<Book> list = new ArrayList<>();
    126         Connection conn = DBUtil.getConn();
    127         Statement state = null;
    128         ResultSet rs = null;
    129 
    130         try {
    131             state = conn.createStatement();
    132             rs = state.executeQuery(sql);
    133             Book bean = null;
    134             while (rs.next()) {
    135                 int id = rs.getInt("id");
    136                 String bianhao = rs.getString("bianhao");
    137                 String bname = rs.getString("bname");
    138                 String wname = rs.getString("wname");
    139                 String bhome=rs.getString("bhome");
    140                 int num = rs.getInt("num");
    141                 bean = new Book(id,bianhao,bname,wname,bhome,num);
    142                 list.add(bean);
    143             }
    144         } catch (SQLException e) {
    145             e.printStackTrace();
    146         } finally {
    147             DBUtil.close(rs, state, conn);
    148         }
    149 
    150         return list;
    151     }
    152 
    153 
    154 }

    servlet层:

      1 package servlet;
      2 
      3 import java.io.IOException;
      4 import java.util.List;
      5 
      6 import javax.servlet.ServletException;
      7 import javax.servlet.annotation.WebServlet;
      8 import javax.servlet.http.HttpServlet;
      9 import javax.servlet.http.HttpServletRequest;
     10 import javax.servlet.http.HttpServletResponse;
     11 
     12 import dao.Dao;
     13 import entity.Book;
     14 
     15 @WebServlet("/Servlet")
     16 public class Servlet extends HttpServlet {
     17     private static final long serialVersionUID = 1L;
     18     Dao dao = new Dao();
     19 
     20     protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
     21         req.setCharacterEncoding("utf-8");
     22         String method = req.getParameter("method");
     23         if ("addBook".equals(method)) {
     24             addBook(req, resp);
     25         } else if ("liulanbook".equals(method)) {
     26             liulanbook(req, resp);
     27         }  else if ("getbyname".equals(method)) {
     28             getbyname(req, resp);
     29         } else if ("deleteBook".equals(method)) {
     30             deleteBook(req, resp);
     31         }else if("updateBook".equals(method)) {
     32             updateBook(req,resp);
     33         }
     34     }
     35 
     36     private void deleteBook(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
     37         // TODO 自动生成的方法存根
     38         req.setCharacterEncoding("utf-8");
     39         String bname = req.getParameter("bname");
     40         if (dao.deleteBook(bname)) {
     41             req.setAttribute("message", "删除成功");
     42             req.getRequestDispatcher("Servlet?method=liulanbook2").forward(req, resp);
     43         } else {
     44             req.setAttribute("message", "删除失败");
     45             req.getRequestDispatcher("Servlet?method=liulanbook2").forward(req, resp);
     46         }
     47     }
     48 
     49     private void addBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
     50         req.setCharacterEncoding("utf-8");
     51         String bianhao = req.getParameter("bianhao");
     52         String bname = req.getParameter("bname");
     53         String wname = req.getParameter("wname");
     54         String bhome = req.getParameter("bhome");
     55         int num = Integer.parseInt(req.getParameter("num"));
     56         Book book = new Book(bianhao, bname, wname, bhome, num);
     57         if (dao.addBook(book)) {
     58             req.setAttribute("book", book);
     59             req.setAttribute("message", "添加成功");
     60             req.getRequestDispatcher("addBook.jsp").forward(req, resp);
     61         } else {
     62             req.setAttribute("message", "书籍信息重复,请重新输入");
     63             req.getRequestDispatcher("addBook.jsp").forward(req, resp);
     64         }
     65     }
     66 
     67 
     68    private void getbyname(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
     69        req.setCharacterEncoding("utf-8");
     70        String bname = req.getParameter("bname");
     71        Book b=new Book();
     72        b.setBname(bname);
     73        b= dao.getbyname(bname);
     74        if(b==null)
     75        {
     76            req.setAttribute("message", "未找到该书籍");
     77            req.getRequestDispatcher("xiugaiBook.jsp").forward(req, resp);
     78        }
     79        else 
     80        {
     81            req.setAttribute("b", b);
     82            req.getRequestDispatcher("xiugai2.jsp").forward(req,resp);
     83        }
     84    }  
     85     private void liulanbook(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
     86         req.setCharacterEncoding("utf-8");
     87 
     88         List<Book> tens = dao.liulanbook();
     89         req.setAttribute("tens", tens);
     90         req.getRequestDispatcher("list.jsp").forward(req, resp);
     91     }
     92 
     93      private void updateBook(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
     94             req.setCharacterEncoding("utf-8");
     95             String old_bname = req.getParameter("old_bname");
     96             String bianhao=req.getParameter("bianhao");
     97             String bname = req.getParameter("bname");
     98             String wname= req.getParameter("wname");
     99             String bhome = req.getParameter("bhome");
    100             int num = Integer.parseInt(req.getParameter("num"));
    101             Book b = new Book(bianhao, bname, wname, bhome, num);
    102             if (dao.updateBook(b,old_bname)) {
    103                 req.setAttribute("message", "修改成功");
    104                 req.getRequestDispatcher("Servlet?method=liulanbook2").forward(req, resp);
    105             }else {
    106                 req.setAttribute("message", "修改失败");
    107                 req.getRequestDispatcher("Servlet?method=liulanbook2").forward(req, resp);
    108             }
    109         }
    110 }

    xiugai2.jsp:

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3     <%@ page import="entity.*" %>
     4 <!DOCTYPE html>
     5 <html>
     6 <head>
     7 <meta charset="UTF-8">
     8 <title>修改学生信息</title>
     9 <script>
    10 </script>
    11 </head>
    12 <body>
    13 <%
    14          Object message = request.getAttribute("message");
    15          if(message!=null && !"".equals(message)){
    16      
    17     %>
    18          <script type="text/javascript">
    19               alert("<%=request.getAttribute("message")%>");
    20          </script>
    21     <%} %>
    22     <%
    23     Book b=(Book)request.getAttribute("b");
    24     %>
    25   <table align="center" border="0px" cellpadding="10px" cellspacing="10px">
    26   <form action="Servlet?method=updateBook"  method="post"  onsubmit="return check()">
    27   <tr>
    28   <td><a href="guanli.jsp">返回菜单</a></td>
    29   </tr>
    30   <input type="hidden" name="old_bname" value="<%=b.getBname()%>"/>
    31    <tr>
    32     <td>图书编号:</td>
    33     <td><input type="text" name="bianhao" id="bianhao" value="<%=b.getBianhao()%>"/></td>
    34    </tr>
    35    <tr>
    36    <td>书名:</td>
    37    <td><input type="text" name="bname" id="bname" value="<%=b.getBname()%>"></td>
    38    </tr>
    39  
    40    <tr>
    41    <td>作者名:</td>
    42    <td><input type="text" name="wname" id="wname" value="<%=b.getWname()%>"></td>
    43    </tr>
    44     <tr>
    45      <tr>
    46    <td>出版社名称:</td>
    47    <td><input type="text" name="bhome" id="bhome" value="<%=b.getBhome()%>"></td>
    48    </tr>
    49     <tr>
    50      <tr>
    51    <td>可借阅数量:</td>
    52    <td><input type="text" name="num" id="num" value="<%=b.getNum()%>"></td>
    53    </tr>
    54     <tr>
    55     <tr align="center">
    56     <th colspan="2">
    57     <input type="submit" value="修改">
    58     </th>
    59     </tr>
    60     </form>
    61 </table>
    62 </body>
    63 </html>

    在list页面中,删除功能直接调用函数就可以实现

    <td><a href="Servlet?method=getbyname&bname=${item.bname }">修改</a>

    运行截图:

    若要修改名称为老人与海的书籍的作者,点击操作的修改即可修改

    想要修改作者名称只需要修改作者即可:

     修改成功

     

  • 相关阅读:
    Cookie的作用范围、设置、创建、获取的方法
    2020寒假总结14
    MyBatis中foreach传入参数为数组
    Springmvc接收数组参数
    寒假每日总结十
    2020寒假每日总结
    2020寒假总结八
    2020寒假每日总结七
    2020寒假每日总结四
    2020寒假总结三
  • 原文地址:https://www.cnblogs.com/znjy/p/14171039.html
Copyright © 2011-2022 走看看