📄 shoppingcartbean.java
字号:
package shopcart.ejbs;import javax.ejb.*;import java.io.Serializable;import java.util.*;import java.sql.*;import javax.sql.*;import javax.naming.*;import DebugLog;public class ShoppingCartBean implements SessionBean{ transient protected DebugLog logger; transient protected SessionContext context; transient protected Context jndi; protected Hashtable items; protected String cartName; protected String logServer; public void ejbActivate() { } public void ejbRemove() { try { logger.closeLog(); logger = null; } catch(Exception exp) { } } public void ejbPassivate() { try { logger.closeLog(); logger = null; } catch(Exception exp) { } } public void setSessionContext(SessionContext ctx) { try { jndi = new InitialContext(); } catch(Exception exp) { } context = ctx; logger = new DebugLog(); try { logServer = (String) jndi.lookup("java:comp/env/logserver"); } catch(Exception exp) { } if(logServer != null) { logger.logTo(logServer); } } public void ejbCreate(String nm) { cartName = nm; if(items == null) items = new Hashtable(); else items.clear(); logToServer("Created bean: "+cartName); } public ShoppingCartItem addItem(ShoppingCartItem item) { ShoppingCartItem test=null; ResultSet rs=null; Statement statement=null; ShoppingCartItem retVal = null; Connection conn=null; double curPrice; int curQuantity; if(items == null) items = new Hashtable(); logToServer("Adding item: "+item); if(item != null) test = (ShoppingCartItem) items.get(item.desc); if(test == null) { try { conn = connectToDB(); statement = conn.createStatement(); rs = statement.executeQuery("select * from inventory" +" where item_desc=" +"\'"+item.desc+"\'"); //Only take the first one if(rs.next()) { curPrice = item.price; //reset price from DB item.price = rs.getDouble("PRICE"); curQuantity = rs.getInt("QUANTITY"); logToServer("Adding "+item.desc +" at price "+item.price); if(curQuantity < item.quantity) item.quantity = curQuantity; items.put(item.desc,item); retVal = item; } else { logToServer("Item "+item.desc +" not in database."); } } catch(Exception exp) { logToServer(exp.toString()); retVal = null; } finally { try { if(rs!=null) rs.close(); if(statement != null) statement.close(); if(conn!=null) conn.close(); } catch(Exception exp) { logToServer(exp.toString()); } } } else { test.quantity+=item.quantity; retVal = test; } return retVal; } public ShoppingCartItem[] getItems() { Enumeration keys; ShoppingCartItem[] retVal=null; int i=0; if(items != null) { retVal = new ShoppingCartItem[items.size()]; keys=items.keys(); while(keys.hasMoreElements()) { retVal[i] = (ShoppingCartItem) items.get(keys.nextElement()); i++; } } return retVal; } public boolean deleteItem(ShoppingCartItem item) { ShoppingCartItem test=null; boolean retVal = false; if((items != null)&&(item != null)) test = (ShoppingCartItem) items.get(item.desc); if(test != null) { test.quantity-=item.quantity; if(test.quantity <= 0) items.remove(test.desc); retVal = true; } return retVal; } public boolean purchaseCart(String user) throws Exception, BadCreditException { boolean retVal = false; ShoppingCartItem curItem; Enumeration cursor; ResultSet rs=null; Statement statement=null; Connection conn=null; double curPrice; int curQuantity, curSales; double total=0; double max, used; if(items == null) return false; logToServer("Purchasing items for: "+user); try { conn = connectToDB(); statement = conn.createStatement(); rs = statement.executeQuery("select * from customers" +" where CUST_NAME=" +"\'"+user+"\'"); if(rs.next()) { used = rs.getDouble("CREDIT_USED"); max = rs.getDouble("CREDIT_LIMIT") - used; } else { throw new Exception("Trigger failure, no credit."); } cursor = items.elements(); while(cursor.hasMoreElements()) { curItem = (ShoppingCartItem) cursor.nextElement(); statement = conn.createStatement(); rs = statement.executeQuery("select * from inventory" +" where item_desc=" +"\'"+curItem.desc+"\'"); //Only take the first one if(rs.next()) { curPrice = curItem.price; //reset price from DB curItem.price = rs.getDouble("PRICE"); curQuantity = rs.getInt("QUANTITY"); if((curQuantity < curItem.quantity) ||(curPrice != curItem.price)) { throw new Exception("Trigger failure, bad q/p."); } total += curItem.getTotal(); } else { throw new Exception("Trigger failure item not in DB."); } } //make sure there is enough credit if(total > max) { throw new BadCreditException(max); } //update the user's credit statement.executeUpdate("update customers set credit_used=" + (total+used) +" where cust_name=" +"\'"+user+"\'"); //update the inventory cursor = items.elements(); while(cursor.hasMoreElements()) { curItem = (ShoppingCartItem) cursor.nextElement(); rs = statement.executeQuery("select * from inventory" +" where item_desc=" +"\'"+curItem.desc+"\'"); //Only take the first one if(rs.next()) { curQuantity = rs.getInt("QUANTITY"); curSales = rs.getInt("SALES"); statement.executeUpdate("update inventory " +"set quantity=" + (curQuantity-curItem.quantity) +" , sales=" + (curSales+curItem.quantity) +" where item_desc=" +"\'"+curItem.desc+"\'"); } else { throw new Exception("Trigger failure item not in DB."); } } retVal = true; } catch(BadCreditException exp) { logToServer(exp.toString()); throw exp; } catch(Exception exp) { logToServer(exp.toString()); retVal = false; } finally { try { if(rs!=null) rs.close(); if(statement != null) statement.close(); if(conn!=null) conn.close(); } catch(Exception exp) { logToServer(exp.toString()); } } return retVal; } protected void logToServer(String str) { if(logger == null) { try { logger = new DebugLog(); logger.logTo(logServer); } catch(Exception exp) { } } logger.log(str); } public Object[] getInventory() { Vector items; Object[] retVal=null; ShoppingCartItem curItem; double price; int quantity; int sales; String desc; ResultSet rs=null; Statement statement=null; Connection conn=null; items = new Vector(); try { conn = connectToDB(); statement = conn.createStatement(); rs = statement.executeQuery("select * from inventory"); //Only take the first one while(rs.next()) { price = rs.getDouble("PRICE"); quantity = rs.getInt("QUANTITY"); sales = rs.getInt("SALES"); desc = rs.getString("ITEM_DESC"); curItem = new ShoppingCartItem(desc,price,quantity); curItem.sales = sales; items.addElement(curItem); } retVal = new Object[items.size()]; items.copyInto(retVal); } catch(Exception exp) { logToServer(exp.toString()); retVal = null; } finally { try { if(rs!=null) rs.close(); if(statement != null) statement.close(); if(conn!=null) conn.close(); } catch(Exception exp) { logToServer(exp.toString()); } } return retVal; } protected Connection connectToDB() { Connection retVal = null; try { DataSource ds = null; ds = (DataSource) jndi.lookup("java:comp/env/jdbc/ejava"); retVal = ds.getConnection(); } catch(Exception exp) { retVal = null; logToServer(exp.toString()); } return retVal; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -