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);
}
}
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);
}
}
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 是自定义的异常类,用来处理异常信息
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());
}\\按照书籍编号买书
}
}
在该方法中遍例出每一个对象,得到对象的id和num,调用buyBook()
如果数据库的数据>=num,数据库数据-num
Collection items=cart.getItems();
Iterator i=items.iterator(); \\这里是把买的书的相关信息封装在I里面
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();\\把记录封装到bean里面
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());
}这些东西很简单 建议你独立思考。自己设个断点 跟一下会很清楚地。
iterator
方法名称得到了改进。
你去看下JAVA。util 。Iterator 的API里面说的很清楚
删除,修改的动作。