簡單的圖書管理系統
通過數據源和DAO對象訪問數據庫。其中JavaBeans實現模型,訪問數據庫,Servlet實現控制器,JSP頁面實現視圖。
? 模型包括2個JavaBean:BookBean用于存放圖書信息,BookDAO用于訪問數據庫。
? 控制器包括2個Servlet:BookQueryServlet根據請求參數查詢圖書信息、BookInsertServlet用來向數據庫中插入數據。
? 視圖包括4個JSP頁面:bookQuery.jsp顯示查詢頁面、bookInsert.jsp顯示插入頁面、display.jsp顯示查詢結果頁面和errorPage.jsp顯示錯誤頁面。
context.xml配置
<?xml version="1.0" encoding="UTF-8"?><Context><Resourceauth="Container"driverClassName="com.mysql.cj.jdbc.Driver"maxIdle="30"maxTotal="50"maxWaitMillis="-1"name="jdbc/book"username="rotdas"password="dasdash"type="javax.sql.DataSource"url="jdbc:mysql://127.0.0.1:3306/book?serverTimezone=UTC"/>
</Context>
源代碼
BookBean代碼public class BookBean implements Serializable {private String bookid = null;private String title = null;private String author = null;private String publisher = null;private float price = 0.0F;public BookBean(){}public BookBean(String bookId, String author,String title, String publisher, float price) {this.bookid = bookId;this.title = title;this.author = author;this.publisher = publisher;this.price = price;}public String getBookid() { return this.bookid; }public String getTitle() { return title; }public String getAuthor() { return this.author; }public float getPrice() { return price; }public String getPublisher () { return publisher; }public void setBookid(String bookid){ this.bookid=bookid; }public void setTitle(String title){this.title=title; }public void setAuthor(String author){ this. author = author; }public void setPrice(float price){this.price=price; }public void setPublisher (String publisher){ this.publisher = publisher;}
}
BookDAO代碼public class BookDAO{private static InitialContext context= null;private DataSource dataSource = null;public BookDAO(){try{if(context == null){context = new InitialContext();}dataSource = (DataSource)context.lookup("java:comp/env/jdbc/book");}catch(NamingException e2){}}// 根據書號查詢圖書信息public BookBean searchBook(String bookid){Connection conn = null;PreparedStatement pstmt = null;ResultSet rst = null;BookBean book = new BookBean();try{conn = dataSource.getConnection();pstmt = conn.prepareStatement("SELECT * FROM books WHERE bookid=?");pstmt.setString(1,bookid);rst = pstmt.executeQuery();if(rst.next()){book.setBookid(rst.getString("bookid"));book.setTitle(rst.getString("title"));book.setAuthor(rst.getString("author"));book.setPublisher(rst.getString("publisher"));book.setPrice(rst.getFloat("price"));return book;}else{return null;}}catch(SQLException se){se.printStackTrace();return null;}finally{try{if(conn != null){conn.close();}}catch(SQLException se){}}}// 插入一本圖書記錄public boolean insertBook(BookBean book){Connection conn = null;PreparedStatement pstmt = null;try{conn = dataSource.getConnection();pstmt = conn.prepareStatement("INSERT INTO books VALUES(?,?,?,?,?)");pstmt.setString(1,book.getBookid());pstmt.setString(2,book.getTitle());pstmt.setString(3,book.getAuthor());pstmt.setString(4,book.getPublisher());pstmt.setFloat(5,book.getPrice());pstmt.executeUpdate();pstmt.close();return true;}catch(SQLException se){se.printStackTrace();return false;}finally{try{if(conn != null){conn.close();}}catch(SQLException se){ }}}
}
BookInsertServlet代碼
public class BookInsertServlet extends HttpServlet {public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {request.setCharacterEncoding("gb2312");String message = null;BookBean book = new BookBean(request.getParameter("bookid"),request.getParameter("title"),request.getParameter("author"),request.getParameter("publisher"),Float.parseFloat(request.getParameter("price")));BookDAO bookdao = new BookDAO();boolean success = bookdao.insertBook(book);if(success){message = "成功插入一條記錄!";}else{message = "插入記錄錯誤!";}request.setAttribute("result",message);RequestDispatcher view = request.getRequestDispatcher("/bookInsert.jsp");view.forward(request, response);}
}
public class BookQueryServlet extends HttpServlet {public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {String bookid = request.getParameter("bookid");BookDAO bookdao = new BookDAO();BookBean book = bookdao.searchBook(bookid);if(book!=null){request.getSession().setAttribute("book", book);RequestDispatcher view = request.getRequestDispatcher("/display.jsp");view.forward(request, response);}else{RequestDispatcher view = request.getRequestDispatcher("/errorPage.jsp");view.forward(request, response);}}
}bookInsert.jsp代碼
<%@ page contentType="text/html; charset=gb2312" %>
<html><head> <title>Book Insert</title>
</head>
<body>
<h3>請輸入圖書信息:</h3>
<% if(request.getAttribute("result")!=null)out.print(request.getAttribute("result"));
%>
<form action = "bookinsert.do" method = "post"><table><tr><td>書號</td> <td><input type="text" name="bookid" ></td></tr><tr><td>書名</td><td><input type="text" name="title"></td></tr><tr><td>作者</td><td><input type="text" name="author" ></td></tr><tr><td>出版社</td><td><input type="text" name="publisher" ></td></tr><tr><td>單價</td><td><input type="text" name="price" ></td></tr><tr><td><input type="submit" value="確定" ></td><td><input type="reset" value="重置" ></td></tr></table>
</form>
</body></html>bookQuery.jsp代碼
<%@ page contentType="text/html; charset=gb2312" %>
<html><head> <title>Book Query</title>
</head>
<body>
請輸入一個書號:<br>
<form action="bookquery.do" method = "post"><input type="text" name="bookid"><br><input type="submit" value="提交">
</form>
</body>
</html>display.jsp 代碼
<%@ page contentType="text/html;charset=gb2312"%>
<jsp:useBean id="book" class="com.BookBean" scope="session"/>
<html><body>
書號:<jsp:getProperty name="book" property="bookid"/><br><br>
書名:<jsp:getProperty name="book" property="title"/><br><br>
作者:<jsp:getProperty name="book" property="author"/><br><br>
出版社:<jsp:getProperty name="book" property="publisher"/><br><br>
價格:<jsp:getProperty name="book" property="price"/><br><br>
</body></html>erorpage.jsp代碼
<%@ page contentType="text/html;charset=gb2312"%>
<html><body>
對不起,您查的圖書不存在!
</body></html>