⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bookdbauthor.java~4~

📁 网上书店,JSP,MYSQL,struct,spring,hibernate
💻 JAVA~4~
字号:
package bookshoppingsys;import java.sql.*;import javax.naming.*;import javax.sql.*;import java.util.*;public class BookDBAuthor {  private ArrayList books;  public BookDBAuthor () throws Exception{     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  }  public Connection getConnection()throws Exception{      return java.sql.DriverManager.getConnection("jdbc:odbc:Shopping");  }  public void closeConnection(Connection con){    try{        if(con!=null) con.close();      }catch(Exception e){        e.printStackTrace();      }  }  public void closePrepStmt(Statement Stmt){    try{        if(Stmt!=null) Stmt.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;    books = new ArrayList();    try {      con=getConnection();      String selectStatement = "select * from book";      prepStmt = con.prepareStatement(selectStatement);      rs = prepStmt.executeQuery();      while (rs.next()) {        BookDetails bd = new BookDetails(rs.getInt(1), rs.getString(2), rs.getString(3),         rs.getString(4),rs.getString(5), rs.getString(6), rs.getDate(7),rs.getFloat(8),         rs.getFloat(9),rs.getFloat(10),rs.getInt(11),rs.getString(12));        books.add(bd);      }    }finally{      closeResultSet(rs);      closePrepStmt(prepStmt);      closeConnection(con);    }    return books.size();  }  public Collection getBooks()throws Exception{    Connection con=null;    PreparedStatement prepStmt=null;    ResultSet rs =null;    books = new ArrayList();    try {      con=getConnection();      String selectStatement = "select * from book";      prepStmt = con.prepareStatement(selectStatement);      rs = prepStmt.executeQuery();      while (rs.next()) {        BookDetails bd = new BookDetails(rs.getInt(1), rs.getString(2), rs.getString(3),         rs.getString(4),rs.getString(5), rs.getString(6), rs.getDate(7),rs.getFloat(8),         rs.getFloat(9),rs.getFloat(10),rs.getInt(11),rs.getString(12) );        books.add(bd);      }    }finally{      closeResultSet(rs);      closePrepStmt(prepStmt);      closeConnection(con);    }    Collections.sort(books);    return books;  }//得到书的详细信息  public BookDetails getBookDetails(String author) throws Exception {    Connection con=null;    Statement Stmt=null;    ResultSet rs =null;    try {      con=getConnection();      String selectStatement = "select * from book where author like '"+author+"%' ";      Stmt = con.createStatement() ;      rs = Stmt.executeQuery(selectStatement);      if (rs.next()) {        BookDetails bd = new BookDetails(rs.getInt(1), rs.getString(2), rs.getString(3),         rs.getString(4),rs.getString(5), rs.getString(6), rs.getDate(7),rs.getFloat(8),         rs.getFloat(9),rs.getFloat(10),rs.getInt(11),rs.getString(12));        Stmt.close();        return bd;      }      else {        return null;      }    }finally{      closeResultSet(rs);      closePrepStmt(Stmt);      closeConnection(con);    }  }  public void buyBooks(ShoppingCart cart)throws Exception {    Connection con=null;    Collection items = cart.getItems();    Iterator i = items.iterator();    try {      con=getConnection();      //自动提交模式      con.setAutoCommit(false);//默认的话为自动提交, 当没执行一个update ,delete或者insert的时候都会自动提交到数据库,无法回滚事务。 设置connection.setautocommit(false);只有程序调用connection.commit()的时候才会将先前执行的语句一起提交到数据库,这样就实现了数据库的事务。      while (i.hasNext()) {        ShoppingCartItem sci = (ShoppingCartItem)i.next();        BookDetails bd = (BookDetails)sci.getItem();        String book_name = bd.getbook_name();        int quantity = sci.getQuantity();        buyBook(book_name, quantity,con);      }      con.commit();      con.setAutoCommit(true);    } catch (Exception ex) {      con.rollback();      throw ex;    }finally{       closeConnection(con);    }  }  public void buyBook(String book_name, int quantity,Connection con) throws Exception {    PreparedStatement prepStmt=null;    ResultSet rs=null;    try{      String selectStatement = "select * from book where book_name = ? ";      prepStmt = con.prepareStatement(selectStatement);      prepStmt.setString(1, book_name);      rs = prepStmt.executeQuery();      if (rs.next()) {          prepStmt.close();          String updateStatement =                  "update book set sale_number = sale_number + ? where book_name = ?";//可能有错          prepStmt = con.prepareStatement(updateStatement);          prepStmt.setInt(1, quantity);          prepStmt.setString(2, book_name);          prepStmt.executeUpdate();          prepStmt.close();       }    }finally{      closeResultSet(rs);      closePrepStmt(prepStmt);    }  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -