网站普查建设背景,网站建设在国内外研究现状,网络营销功能,郑州企业网站建设公司简单的图书管理系统 通过数据源和DAO对象访问数据库。其中JavaBeans实现模型#xff0c;访问数据库#xff0c;Servlet实现控制器#xff0c;JSP页面实现视图。 • 模型包括2个JavaBean#xff1a;BookBean用于存放图书信息#xff0c;BookDAO用于访问数据库。 • 控制器包…简单的图书管理系统 通过数据源和DAO对象访问数据库。其中JavaBeans实现模型访问数据库Servlet实现控制器JSP页面实现视图。 • 模型包括2个JavaBeanBookBean用于存放图书信息BookDAO用于访问数据库。 • 控制器包括2个ServletBookQueryServlet根据请求参数查询图书信息、BookInsertServlet用来向数据库中插入数据。 • 视图包括4个JSP页面bookQuery.jsp显示查询页面、bookInsert.jsp显示插入页面、display.jsp显示查询结果页面和errorPage.jsp显示错误页面。
context.xml配置
?xml version1.0 encodingUTF-8?ContextResourceauthContainerdriverClassNamecom.mysql.cj.jdbc.DrivermaxIdle30maxTotal50maxWaitMillis-1namejdbc/bookusernamerotdaspassworddasdashtypejavax.sql.DataSourceurljdbc:mysql://127.0.0.1:3306/book?serverTimezoneUTC/
/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.bookidbookid; }public void setTitle(String title){this.titletitle; }public void setAuthor(String author){ this. author author; }public void setPrice(float price){this.priceprice; }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 contentTypetext/html; charsetgb2312 %
htmlhead titleBook Insert/title
/head
body
h3请输入图书信息/h3
% if(request.getAttribute(result)!null)out.print(request.getAttribute(result));
%
form action bookinsert.do method posttabletrtd书号/td tdinput typetext namebookid /td/trtrtd书名/tdtdinput typetext nametitle/td/trtrtd作者/tdtdinput typetext nameauthor /td/trtrtd出版社/tdtdinput typetext namepublisher /td/trtrtd单价/tdtdinput typetext nameprice /td/trtrtdinput typesubmit value确定 /tdtdinput typereset value重置 /td/tr/table
/form
/body/htmlbookQuery.jsp代码
% page contentTypetext/html; charsetgb2312 %
htmlhead titleBook Query/title
/head
body
请输入一个书号:br
form actionbookquery.do method postinput typetext namebookidbrinput typesubmit value提交
/form
/body
/htmldisplay.jsp 代码
% page contentTypetext/html;charsetgb2312%
jsp:useBean idbook classcom.BookBean scopesession/
htmlbody
书号jsp:getProperty namebook propertybookid/brbr
书名jsp:getProperty namebook propertytitle/brbr
作者jsp:getProperty namebook propertyauthor/brbr
出版社jsp:getProperty namebook propertypublisher/brbr
价格jsp:getProperty namebook propertyprice/brbr
/body/htmlerorpage.jsp代码
% page contentTypetext/html;charsetgb2312%
htmlbody
对不起您查的图书不存在
/body/html