1、Head First Servlets & JSP, Bryan Basham中国电力出版社,98.00);INSERT INTO books VALUES (201Servlets 与JSP 核心教程Hall Marty清华大学出版社,45);202Tomcat与Java Web 开发技术祥解孙卫琴机械工业出版社203JSP 应用开发技术柳永坡人民邮电出版社,52);205J2EE 1.4 编程指南Spielman Sue电子工业出版社,68);注意:需要将数据库的JDBC驱动程序安装到应用程序的WEB-INFlib目录中。【步骤2】使用下面JSP页面displayBooks.jsp访问bo
2、oks表中的数据。% page import=java.sql.*%htmlhead Database Access Test/headbody%try Class.forName(org.postgresql.DriverString dburl = jdbc:postgresql:/localhost:5432/bookstore;Connection conn = DriverManager.getConnection(dburl, bookstore, Statement stmt = conn.createStatement();String sql = SELECT * FROM
3、books ;ResultSet rs = stmt.executeQuery(sql);out.println(table border=1trtd书号书名作者价格while (rs.next()+ rs.getString(1)+ rs.getString(2)+ rs.getString(3)+ rs.getString(5)+/tablers.close();stmt.close();conn.close();catch (Exception e) out.println(e.getMessage();/body/html运用mysql的代码如下: try Class.forName(
4、com.mysql.jdbc.Driver String dburl = mysql:3306/bookstore Connection conn = DriverManager .getConnection(dburl, root Statement stmt = conn.createStatement(); String sql = ResultSet rs = stmt.executeQuery(sql); out.println( while (rs.next() out.println( + rs.getString(1) + + rs.getString(2) + + rs.ge
5、tString(3) + + rs.getString(5) + rs.close(); stmt.close(); conn.close(); catch (Exception e) out.println(e.getMessage(); 图1 displayBooks.jsp(二)通过数据源访问数据库需要将数据库的JDBC驱动程序安装到Tomcat安装目录的lib目录中,并重新启动Tomcat服务器。【步骤1】建立局部数据源在Web应用程序中建立一个META-INF目录,在其中建立一个context.xml文件,内容如下:?xml version=1.0 encoding=utf-8Con
6、text reloadable = trueResource name=jdbc/bookDS type=javax.sql.DataSource maxActive=4 maxIdle=2 username= maxWait=5000 driverClassName= password= url=/Context【步骤2】使用下面的JSP页面displayBooks.jsp访问数据库java.sql.*, javax.sql.*,javax.naming.*DataSource TestContext context = new InitialContext();DataSource ds
7、= (DataSource) context.lookup(java:comp/env/jdbc/bookDSConnection conn = ds.getConnection();ResultSet rs = stmt.executeQuery(图2 displayBooks.jsp(三)综合应用。本实验采用MVC设计模式,通过数据源和DAO对象访问数据库。其中JavaBeans实现模型,访问数据库,Servlet实现控制器,JSP页面实现视图。 模型包括2个JavaBean:BookBean用于存放图书信息,BookDAO用于访问数据库。 控制器包括2个Servlet:BookQuery
8、Servlet根据请求参数查询图书信息、BookInsertServlet用来向数据库中插入数据。 视图包括4个JSP页面:bookQuery.jsp显示查询页面、bookInsert.jsp显示插入页面、display.jsp显示查询结果页面和errorPage.jsp显示错误页面。【步骤1】存放图书信息的JavaBeans代码BookBean.java,它也是一个传输对象。package com.beans;import java.io.*;public class BookBean implements Serializable private String bookid = null;
9、 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.p
10、ublisher = 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.
11、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;【步骤2】下面的BookDAO是一个简单的JavaBeans,它实现数据库的访问。import jav
12、a.sql.*;import javax.sql.*;import javax.naming.*;import java.util.ArrayList;import com.beans.BookBean;public class BookDAO private static InitialContext context= null; private DataSource dataSource = null; public BookDAO() try if(context = null) context = new InitialContext(); dataSource = (DataSour
13、ce)context.lookup( 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 W
14、HERE 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; cat
15、ch(SQLException se) return null; finally try conn.close(); catch(SQLException se) / 插入一本图书记录 public boolean insertBook(BookBean book) Connection conn = null; PreparedStatement pstmt = null; conn = dataSource.getConnection(); pstmt = conn.prepareStatement( INSERT INTO books VALUES(?,?) pstmt.setStrin
16、g(1,book.getBookid(); pstmt.setString(2,book.getTitle(); pstmt.setString(3,book.getAuthor(); pstmt.setString(4,book.getPublisher(); pstmt.setFloat(3,book.getPrice(); pstmt.executeUpdate(); pstmt.close(); return true; return false; catch(SQLException se) 【步骤3】下面的JSP页面bookQuery.jsp实现根据书号查询图书信息Book Que
17、ryform action=bookquery.do method = postinput type=textsubmit value=提交/form图3 bookQuery.jsp【步骤4】下面的JSP页面bookInsert.jsp实现向数据库中插入数据Book Insert请输入图书信息:/h3% if(request.getAttribute(result)!=null) out.print(request.getAttribute(form action = bookinsert.dotable 出版社单价确定reset重置图4 bookInsert.jsp 图5 插入成功【步骤5】
18、显示查询结果的JSP页面display.jsp:charset=gb2312jsp:useBean id=book class=com.beans.BookBean scope=request 书号:getProperty name= property= 书名: 作者: 出版社: 价格:图6 显示查询结果【步骤6】错误页面errorPage.jsp代码如下: 对不起,您查的图书不存在!图7 显示错误页面【步骤7】下面的Servlet实现从请求参数获得书号,然后从数据库中查找该书,最后根据查询结果将请求转发到显示页面(display.jsp)或错误页面(errorPage.jsp)。packag
19、e com.control;import javax.servlet.*;import javax.servlet.http.*;import com.beans.BookDAO;public class BookQueryServlet extends HttpServlet public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException String bookid = request.getParameter( BookDAO bookdao = new BookDAO();
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1