日期:2014-05-18  浏览次数:20696 次

请各位高手帮帮忙!!!!请解释一下下面的代码!!!非常非常感谢!!!!这是我所有的分数了!!!
package   database;
import   java.sql.*;
import   java.util.*;
public   class   BookDBAO   {
private   ArrayList   books;
Connection   con;

public   BookDBAO()throws   Exception{
try{
Class.forName(Constans.dbdriver).newInstance();
con=DriverManager.getConnection(Constans.dburl,Constans.username,Constans.password);
}catch(Exception   ex){
throw   new   Exception( "Couldn 't   open   connection   to   database: "+ex.getMessage());
}
}
public   void   remove(){
try{
con.close();
}catch(SQLException   ex){
System.out.println(ex.getMessage());
}
}
public   List   getBooks()throws   BookNotFoundException{
books=new   ArrayList();
try{
String   selectStatement= "selcet   id,name,title,price,onSale,year,descripton,inventory "+ "from   books ";
PreparedStatement   prepStmt=con.prepareStatement(selectStatement);
ResultSet   rs=prepStmt.executeQuery();

while(rs.next()){
BookDetails   bd=new   BookDetails(rs.getString(1),rs.getString(2),rs.getString(3),rs.getFloat(4),rs.getBoolean(5),rs.getInt(6),rs.getString(7),rs.getInt(8));

if(rs.getInt(8)> 0){
books.add(bd);
}
}
prepStmt.close();
}catch(SQLException   ex){
throw   new   BookNotFoundException(ex.getMessage());
}
Collections.sort(books);
return   books;
}

public   BookDetails   getBookDetails(String   bookId)throws   BookNotFoundException{
try{
String   selectStatement= "selcet   id,name,title,price,onSale,year,description,inventory "+ "from   books   where   id=? ";
PreparedStatement   prepStmt=con.prepareStatement(selectStatement);
prepStmt.setString(1,   bookId);

ResultSet   rs=prepStmt.executeQuery();

if(rs.next()){
BookDetails   bd=new   BookDetails(rs.getString(1),rs.getString(2),rs.getString(3),rs.getFloat(4),rs.getBoolean(5),rs.getInt(6),rs.getString(7),rs.getInt(8));
prepStmt.close();
return   bd;
}else{
prepStmt.close();
throw   new   BookNotFoundException( "couldn 't   find   book: "+bookId);

}
}catch(SQLException   ex){
System.err.println(ex.getMessage());
throw   new   BookNotFoundException( "couldn 't   find   book: "+bookId+ " "+ex.getMessage());

}
}

public   void   buyBooks(ShoppingCart   cart)throws   OrderException{
Collection   items=cart.getItems();
Iterator   i=items.iterator();
try{
con.setAutoCommit(false);

while(i.hasNext()){
ShoppingCartItem   sci=(ShoppingCartItem)i.next();
BookDetails   bd=(BookDetails)sci.getItem();
String   id=bd.getBookId();
int   quantity=sci.getQuantity();
buyBook(id,quantity);
}
con.commit();
con.setAutoCommit(true);
}catch(Exception   ex){
try{
con.rollback();
throw   new   OrderException( "transaction   failed: "+ex.getMessage());

}catch(SQLException   sqx)