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 List<Book> liulanbook() { 75 String sql = "select * from Book"; 76 List<Book> list = new ArrayList<>(); 77 Connection conn = DBUtil.getConn(); 78 Statement state = null; 79 ResultSet rs = null; 80 81 try { 82 state = conn.createStatement(); 83 rs = state.executeQuery(sql); 84 Book bean = null; 85 while (rs.next()) { 86 int id = rs.getInt("id"); 87 String bianhao = rs.getString("bianhao"); 88 String bname = rs.getString("bname"); 89 String wname = rs.getString("wname"); 90 String bhome=rs.getString("bhome"); 91 int num = rs.getInt("num"); 92 bean = new Book(id,bianhao,bname,wname,bhome,num); 93 list.add(bean); 94 } 95 } catch (SQLException e) { 96 e.printStackTrace(); 97 } finally { 98 DBUtil.close(rs, state, conn); 99 } 100 101 return list; 102 } 103 }
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 } 30 } 31 private void addBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 32 req.setCharacterEncoding("utf-8"); 33 String bianhao = req.getParameter("bianhao"); 34 String bname = req.getParameter("bname"); 35 String wname = req.getParameter("wname"); 36 String bhome = req.getParameter("bhome"); 37 int num = Integer.parseInt(req.getParameter("num")); 38 Book book = new Book(bianhao, bname, wname, bhome, num); 39 if (dao.addBook(book)) { 40 req.setAttribute("book", book); 41 req.setAttribute("message", "添加成功"); 42 req.getRequestDispatcher("addBook.jsp").forward(req, resp); 43 } else { 44 req.setAttribute("message", "书籍信息重复,请重新输入"); 45 req.getRequestDispatcher("addBook.jsp").forward(req, resp); 46 } 47 } 48 private void getbyname(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ 49 req.setCharacterEncoding("utf-8"); 50 String bname = req.getParameter("bname"); 51 Book b=new Book(); 52 b.setBname(bname); 53 b= dao.getbyname(bname); 54 if(b==null) 55 { 56 req.setAttribute("message", "未找到该书籍"); 57 req.getRequestDispatcher("xiugaiBook.jsp").forward(req, resp); 58 } 59 else 60 { 61 req.setAttribute("b", b); 62 req.getRequestDispatcher("xiugai2.jsp").forward(req,resp); 63 } 64 } 65 private void liulanbook(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { 66 req.setCharacterEncoding("utf-8"); 67 68 List<Book> tens = dao.liulanbook(); 69 req.setAttribute("tens", tens); 70 req.getRequestDispatcher("list.jsp").forward(req, resp); 71 } 72 }
list.jsp:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 4 <!DOCTYPE html> 5 <html> 6 <head> 7 <meta charset="UTF-8"> 8 <title>Insert title here</title> 9 10 </head> 11 <body> 12 <% 13 Object message = request.getAttribute("message"); 14 if(message!=null && !"".equals(message)){ 15 16 %> 17 <script type="text/javascript"> 18 alert("<%=request.getAttribute("message")%>"); 19 </script> 20 <%} %> 21 <h>书籍信息</h> 22 <div class="content"> 23 <div class="main"> 24 <a href="guanli.jsp">返回主页</a> 25 <table align="center"> 26 <tr> 27 <th>编号</th> 28 <th>书名</th> 29 <th>作者名</th> 30 <th>出版社</th> 31 <th>可借阅数量</th> 32 <th>操作</th> 33 <tr> 34 <c:forEach items="${tens}" var="item"> 35 <tr> 36 37 <td><a href="Servlet?method=getbyname&&bname=${item.bname}"></a></td> 38 <td>${item.bianhao}</td> 39 <td>${item.bname}</td> 40 <td>${item.wname}</td> 41 <td>${item.bhome }</td> 42 <td>${item.num}</td> 43 <td><a href="Servlet?method=deleteBook&bname=${item.bname }" onClick="return confirm('确定删除吗?');">删除</a> 44 <td><a href="Servlet?method=getbyname&bname=${item.bname }">修改</a> 45 </tr> 46 </c:forEach> 47 </table> 48 </div> 49 </body> 50 </html>