• 大小: 31.19MB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2023-07-17
  • 语言: Java
  • 标签: tomcat  java  Web  

资源简介

《Tomcat与Java Web开发技术详解(第2版)》.(孙卫琴).源码

资源截图

代码片段和文件信息

/** 直接通过JDBC API 访问MySQL数据库 */
package mypack;
import java.sql.*;
import javax.naming.*;
import javax.sql.*;
import java.util.*;

public class BookDB {

  private String dbUrl =  “jdbc:mysql://localhost:3306/BookDB“;
  private String dbUser=“dbuser“;
  private String dbPwd=“1234“;

  public BookDB () throws Exception{
     Class.forName(“com.mysql.jdbc.Driver“);
  }

  public Connection getConnection()throws Exception{
      return java.sql.DriverManager.getConnection(dbUrldbUserdbPwd);
  }

  public void closeConnection(Connection con){
    try{
        if(con!=null) con.close();
      }catch(Exception e){
        e.printStackTrace();
      }
  }

  public void closePrepStmt(PreparedStatement prepStmt){
    try{
        if(prepStmt!=null) prepStmt.close();
      }catch(Exception e){
        e.printStackTrace();
      }
  }

  public void closeResultSet(ResultSet rs){
    try{
        if(rs!=null) rs.close();
      }catch(Exception e){
        e.printStackTrace();
      }
  }

  public int getNumberOfBooks() throws Exception {
    Connection con=null;
    PreparedStatement prepStmt=null;
    ResultSet rs=null;
    int count=0;

    try {
      con=getConnection();
      String selectStatement = “select count(*) “ + “from BOOKS“;
      prepStmt = con.prepareStatement(selectStatement);
      rs = prepStmt.executeQuery();

      if (rs.next()) 
        count = rs.getInt(1);
 
    }finally{
      closeResultSet(rs);
      closePrepStmt(prepStmt);
      closeConnection(con);
    }
    return count;
  }


  public Collection getBooks()throws Exception{
    Connection con=null;
    PreparedStatement prepStmt=null;
    ResultSet rs =null;
    ArrayList books = new ArrayList();
    try {
      con=getConnection();
      String selectStatement = “select * “ + “from BOOKS“;
      prepStmt = con.prepareStatement(selectStatement);
      rs = prepStmt.executeQuery();

      while (rs.next()) {

        BookDetails bd = new BookDetails(rs.getString(1) rs.getString(2) rs.getString(3)
           rs.getFloat(4) rs.getInt(5) rs.getString(6)rs.getInt(7));
        books.add(bd);
      }

    }finally{
      closeResultSet(rs);
      closePrepStmt(prepStmt);
      closeConnection(con);
    }

    Collections.sort(books);
    return books;
  }

  public BookDetails getBookDetails(String bookId) throws Exception {
    Connection con=null;
    PreparedStatement prepStmt=null;
    ResultSet rs =null;
    try {
      con=getConnection();
      String selectStatement = “select * “ + “from BOOKS where ID = ? “;
      prepStmt = con.prepareStatement(selectStatement);
      prepStmt.setString(1 bookId);
      rs = prepStmt.executeQuery();

      if (rs.next()) {
        BookDetails bd = new BookDetails(rs.getString(1) rs.getString(2) rs.getString(3)
          rs.getFloat(4) rs.getInt(5) rs.getString(6)rs.getInt(7));
        prepStmt.close();

        

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件        909  2008-07-12 12:42  sourcecode\bookstores\books.sql

     文件         41  2008-07-12 12:42  sourcecode\bookstores\version0\bookstore\banner.jsp

     文件       1099  2008-07-12 12:42  sourcecode\bookstores\version0\bookstore\bookdetails.jsp

     文件        509  2008-07-12 12:42  sourcecode\bookstores\version0\bookstore\bookstore.jsp

     文件       1392  2008-07-12 12:42  sourcecode\bookstores\version0\bookstore\build.xml

     文件       1255  2008-07-12 12:42  sourcecode\bookstores\version0\bookstore\build0.xml

     文件        832  2008-07-12 12:42  sourcecode\bookstores\version0\bookstore\cashier.jsp

     文件       1540  2008-07-12 12:42  sourcecode\bookstores\version0\bookstore\catalog.jsp

     文件        349  2008-07-12 12:42  sourcecode\bookstores\version0\bookstore\common.jsp

     文件        353  2008-07-12 12:42  sourcecode\bookstores\version0\bookstore\errorpage.jsp

     文件      29382  2008-07-12 12:42  sourcecode\bookstores\version0\bookstore\logo.bmp

     文件        600  2008-07-12 12:42  sourcecode\bookstores\version0\bookstore\receipt.jsp

     文件       2402  2008-07-12 12:42  sourcecode\bookstores\version0\bookstore\showcart.jsp

     文件       5015  2008-07-12 12:42  sourcecode\bookstores\version0\bookstore\src\mypack\BookDB.java

     文件       1612  2008-07-12 12:42  sourcecode\bookstores\version0\bookstore\src\mypack\BookDetails.java

     文件       1981  2008-07-12 12:42  sourcecode\bookstores\version0\bookstore\src\mypack\ShoppingCart.java

     文件        762  2008-07-12 12:42  sourcecode\bookstores\version0\bookstore\src\mypack\ShoppingCartItem.java

     文件       5186  2008-07-12 12:42  sourcecode\bookstores\version0\bookstore\WEB-INF\classes\mypack\BookDB.class

     文件       1590  2008-07-12 12:42  sourcecode\bookstores\version0\bookstore\WEB-INF\classes\mypack\BookDetails.class

     文件       2420  2008-07-12 12:42  sourcecode\bookstores\version0\bookstore\WEB-INF\classes\mypack\ShoppingCart.class

     文件        788  2008-07-12 12:42  sourcecode\bookstores\version0\bookstore\WEB-INF\classes\mypack\ShoppingCartItem.class

     文件     215441  2008-07-12 12:42  sourcecode\bookstores\version0\bookstore\WEB-INF\lib\mysqldriver.jar

     文件        212  2008-07-12 12:42  sourcecode\bookstores\version0\bookstore\WEB-INF\web.xml

     文件         42  2008-07-12 12:42  sourcecode\bookstores\version1\bookstore\banner.jsp

     文件       1099  2008-07-12 12:42  sourcecode\bookstores\version1\bookstore\bookdetails.jsp

     文件        509  2008-07-12 12:42  sourcecode\bookstores\version1\bookstore\bookstore.jsp

     文件       1255  2008-07-12 12:42  sourcecode\bookstores\version1\bookstore\build.xml

     文件        832  2008-07-12 12:42  sourcecode\bookstores\version1\bookstore\cashier.jsp

     文件       1540  2008-07-12 12:42  sourcecode\bookstores\version1\bookstore\catalog.jsp

     文件        348  2008-07-12 12:42  sourcecode\bookstores\version1\bookstore\common.jsp

............此处省略1479个文件信息

评论

共有 条评论