首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

请帮帮忙!请解释一下下面的代码!非常非常感谢!这是小弟我所有的分数了!

2012-01-20 
请各位高手帮帮忙!!!!请解释一下下面的代码!!!非常非常感谢!!!!这是我所有的分数了!!!packagedatabaseimp

请各位高手帮帮忙!!!!请解释一下下面的代码!!!非常非常感谢!!!!这是我所有的分数了!!!
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){
throw   new   OrderException( "rollback   failed: "+sqx.getMessage());
}
}

}

public   void   buyBook(String   bookId,int   quantity)throws   OrderException{
try{
String   selectStatement= "select   id,name,title,price,onSale,year,description,nventory "+ "from   books   where   id=? ";


PreparedStatement   prepStmt=con.prepareStatement(selectStatement);
prepStmt.setString(1,bookId);

ResultSet   rs=prepStmt.executeQuery();

if(rs.next()){
int   inventory=rs.getInt(8);
prepStmt.close();

if((inventory-quantity)> =0){
String   updateStatement= "update   books   set   inventory=inventory-?where   id=? ";
prepStmt=con.prepareStatement(updateStatement);
prepStmt.setInt(1,   quantity);
prepStmt.setString(2,   bookId);
prepStmt.executeUpdate();
prepStmt.close();
}else{
throw   new   OrderException( "not   enough   of "+bookId+ "in   stock   to   comp;ete   order. ");
}
}
}catch(Exception   ex){
throw   new   OrderException( "couldn 't   purchase   book: "+bookId+ex.getMessage());
}
}
}


其中BookNotFoundException和OrderException是自己定义的,类文件如下:
package   database;

public   class   BookNotFoundException   extends   Exception   {
public   BookNotFoundException()   {

}
public   BookNotFoundException(String   msg)   {
super(msg);
}
}




[解决办法]
创建了一个BookDBAO类,目的是为对数据库的操作,里面有几个方法:
1.public BookDBAO()throws Exception 构造函数,里面的内容,创建与数据库的连接
2.public void remove() 里面的内容,关闭与数据库的连接
3.public List getBooks()throws BookNotFoundException 里面的内容是对某张表的查询,并把结果放在BookDetails对象里.
4.public BookDetails getBookDetails(String bookId)throws BookNotFoundException 根据bookId这个编号来查询表里有这个编号的记录.
5.public void buyBooks(ShoppingCart cart)throws OrderException 购物车原理,ShoppingCart cart是个集合,把数据存放在cart里,让后再放到数据表里BookDetails
6.public void buyBook(String bookId,int quantity) 根据编号来操作
7.BookNotFoundException和OrderException 是自定义的异常类,用来处理异常信息
[解决办法]
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();、、建立一个新的 BOOKS类的LIST
try{
String selectStatement= "selcet id,name,title,price,onSale,year,descripton,inventory "+ "from books ";\\定义一条SQL在数据库中读出书籍的纪录
PreparedStatement prepStmt=con.prepareStatement(selectStatement);提交这条SQL语句
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);\\把类串联到list中
}
}
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){
throw new OrderException( "rollback failed: "+sqx.getMessage());
}
}\\把脉的书加入到购物篮中 

}

public void buyBook(String bookId,int quantity)throws OrderException{
try{
String selectStatement= "select id,name,title,price,onSale,year,description,nventory "+ "from books where id=? ";\\找书
PreparedStatement prepStmt=con.prepareStatement(selectStatement);
prepStmt.setString(1,bookId);

ResultSet rs=prepStmt.executeQuery();

if(rs.next()){
int inventory=rs.getInt(8);
prepStmt.close();

if((inventory-quantity)> =0){
String updateStatement= "update books set inventory=inventory-?where id=? ";\\买书后书籍本数-1
prepStmt=con.prepareStatement(updateStatement);
prepStmt.setInt(1, quantity);
prepStmt.setString(2, bookId);
prepStmt.executeUpdate();
prepStmt.close();
}else{
throw new OrderException( "not enough of "+bookId+ "in stock to comp;ete order. ");
}
}
}catch(Exception ex){
throw new OrderException( "couldn 't purchase book: "+bookId+ex.getMessage());
}\\按照书籍编号买书
}
}

[解决办法]
自己到脑子看,
[解决办法]
buyBooks():把要买的书id和num封装到对象ShoppingCartItem 中,再把每个对象放到集合ShoppingCart 中.
在该方法中遍例出每一个对象,得到对象的id和num,调用buyBook()
如果数据库的数据> =num,数据库数据-num
[解决办法]
mark,正需要
[解决办法]
只是一些数据库的操作,persistence层内的东西,为逻辑层准备的一些方法。简单来说就是添加
删除,修改的动作。

热点排行