📄 bookdbao.java
字号:
package database;import java.sql.*;import java.util.*;import exception.*;import cart.*;import config.*;public class BookDBAO { private ArrayList books; Connection con; /** * 初始化与mysql数据库连接,获得Connection对象 */ public BookDBAO() throws Exception { try { Class.forName(Constants.dbdriver).newInstance(); con=DriverManager.getConnection(Constants.dburl); } catch (Exception ex) { throw new Exception("Couldn't open connection to database: " + ex.getMessage()); } } /** * 关闭与mysql数据库的连接 */ public void remove() { try { con.close(); } catch (SQLException ex) { System.out.println(ex.getMessage()); } } /** * 查询数据库,取得书目列表 */ public List getBooks() throws BooksNotFoundException { books = new ArrayList(); try { String selectStatement = "select id,name,title,price,onSale,year,description,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 BooksNotFoundException(ex.getMessage()); } Collections.sort(books); return books; } /** * 取得书目的详细信息 */ public BookDetails getBookDetails(String bookId) throws BookNotFoundException { try { String selectStatement = "select 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,inventory " + "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 complete order."); } } } catch (Exception ex) { throw new OrderException("Couldn't purchase book: " + bookId + ex.getMessage()); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -