📄 orderejb.java
字号:
package apusic.myshop.order.ejb;import java.util.Collection;import java.util.Properties;import java.util.Iterator;import java.util.ArrayList;import java.sql.SQLException;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.Statement;import java.sql.ResultSet;import java.rmi.RemoteException;import javax.naming.InitialContext;import javax.naming.Context;import javax.naming.NamingException;import javax.ejb.EntityBean;import javax.ejb.EntityContext;import javax.ejb.EJBException;import javax.ejb.FinderException;import javax.ejb.CreateException;import javax.ejb.RemoveException;import javax.ejb.DuplicateKeyException;import javax.sql.DataSource;import apusic.myshop.util.Calendar;import apusic.myshop.util.CreditCard;import apusic.myshop.util.Debug;import apusic.myshop.util.JNDINames;import apusic.myshop.order.model.LineItem;import apusic.myshop.order.model.OrderModel;import apusic.myshop.customer.ejb.Customer;import apusic.myshop.customer.ejb.CustomerHome;import apusic.myshop.util.EJBUtil;public class OrderEJB implements EntityBean { private int orderId; private String userId; private java.sql.Date orderDate; private String shipName; private String shipAddr; private String shipProvince; private String shipCity; private String shipZip; private String shipCountry; private String billName; private String billAddr; private String billProvince; private String billCity; private String billZip; private String billCountry; private CreditCard chargeCard; private double totalPrice; private Collection lineItems; private EntityContext context; private Connection conn = null; protected transient boolean dirty; public OrderEJB() {} public void setEntityContext(EntityContext ec) { context = ec; } private void setDirty(boolean flag) { dirty = flag; } public boolean isModified() { return dirty; } public void unsetEntityContext() {} public void ejbActivate() {} public void ejbPassivate() {} public void ejbPostCreate(String userId, String shipName, String shipAddr, String shipProvince, String shipCity, String shipZip, String shipCountry, String billName, String billAddr, String billProvince, String billCity, String billZip, String billCountry, CreditCard chargeCard, double totalPrice, Collection lineItems) {} public OrderPK ejbFindByPrimaryKey(OrderPK pk) throws FinderException { try{ conn = getDBConnection(); if (orderIdExists(pk.orderId)) return pk; throw new SQLException ("primary key not found:" + orderId); }catch (java.sql.SQLException se) { Debug.print(se); throw new FinderException ("SQL Exception in find by primary key"); } finally { try { conn.close(); } catch (SQLException se) { Debug.print(se); throw new FinderException ("SQL Exception in find by primary key"); } } } public OrderPK ejbFindByOrderId(int orderId) throws FinderException { try{ conn = getDBConnection(); if (orderIdExists(orderId)) return (new OrderPK(orderId)); throw new SQLException ("primary key not found:" + orderId); }catch (java.sql.SQLException se) { Debug.print(se); throw new FinderException ("SQL Exception in find by primary key"); } finally { try { conn.close(); } catch (SQLException se) { Debug.print(se); throw new FinderException ("SQL Exception in find by primary key"); } } } public Collection ejbFindUserOrders( String userId ) throws FinderException { try{ conn = getDBConnection(); return(getOrderIds(userId)); }catch (java.sql.SQLException se) { Debug.print(se); throw new FinderException("SQL Exception in finding collection of primary keys"); } finally { try { conn.close(); } catch (SQLException se) { Debug.print(se); throw new FinderException("SQL Exception in finding collection of primary keys"); } } } public OrderPK ejbCreate(String userId, String shipName, String shipAddr, String shipProvince, String shipCity, String shipZip, String shipCountry, String billName, String billAddr, String billProvince, String billCity, String billZip, String billCountry, CreditCard chargeCard, double totalPrice, Collection lineItems) throws CreateException { this.userId = userId; this.orderDate = new java.sql.Date((new java.util.Date()).getTime()); this.shipName = shipName; this.shipAddr = shipAddr; this.shipProvince = shipProvince; this.shipCity = shipCity; this.shipZip = shipZip; this.shipCountry = shipCountry; this.billName = billName; this.billAddr = billAddr; this.billProvince = billProvince; this.billCity = billCity; this.billZip = billZip; this.billCountry = billCountry; this.chargeCard = chargeCard; this.totalPrice = totalPrice; this.lineItems = lineItems; try{ int m_orderId = insertOrder(); insertLineItem(m_orderId); orderId = m_orderId; return (new OrderPK(orderId)); }catch(SQLException se){ Debug.print(se); context.setRollbackOnly(); throw new CreateException ("SQL Exception in create"); }finally{ try { if (conn != null) conn.close(); } catch (SQLException se) { Debug.print(se); throw new CreateException("SQL Exception in create"); } } } public void ejbLoad() { try{ selectOrder(); selectLineItem(); }catch (java.sql.SQLException se) { Debug.print(se); throw new EJBException (se); } finally { try { if (conn != null) conn.close(); } catch (SQLException se) { Debug.print(se); throw new EJBException (se); } } } public void ejbRemove() throws RemoveException { try{ conn = getDBConnection(); deleteLineItem(); deleteOrder(); }catch (java.sql.SQLException se) { Debug.print(se); context.setRollbackOnly(); throw new RemoveException ("SQL Exception in remove"); } finally { try { if (conn != null) conn.close(); } catch (SQLException se) { Debug.print(se); throw new RemoveException ("SQL Exception in remove"); } } } public void ejbStore() { try{ conn = getDBConnection(); updateOrder(); updateLineItem(); }catch (java.sql.SQLException se) { Debug.print(se); throw new EJBException (se); } finally { try { if (conn != null) conn.close(); } catch (SQLException se) { Debug.print(se); throw new EJBException (se); } } } public OrderModel getDetails() { return(new OrderModel( orderId, userId, orderDate, shipName, shipAddr, shipProvince, shipCity, shipZip, shipCountry, billName, billAddr, billProvince, billCity, billZip, billCountry, chargeCard, totalPrice, lineItems)); } public Customer getCustomer() { try { CustomerHome custHome = EJBUtil.getCustomerHome(); return custHome.findByUserId(userId); } catch (FinderException fe) { // Account mysterious disappeared.. Debug.print(fe); throw new EJBException(fe); } catch (RemoteException re) { Debug.print(re); throw new EJBException(re); } } private int insertOrder() throws SQLException { conn = getDBConnection(); int m_orderId = -1; PreparedStatement pstmt = null; String queryStr = " insert into shp_order(" + " userId, orderDate, shipName, shipAddr, shipProvince, " + " shipCity, shipZip, shipCountry, billName, billAddr, " + " billProvince, billCity, billZip, billCountry, creditCard, " + " cardType, exprDate, totalPrice) " + " values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; pstmt = conn.prepareStatement(queryStr); pstmt.setString(1, userId); pstmt.setDate(2, orderDate); pstmt.setString(3, shipName); pstmt.setString(4, shipAddr); pstmt.setString(5, shipProvince); pstmt.setString(6, shipCity); pstmt.setString(7, shipZip); pstmt.setString(8, shipCountry); pstmt.setString(9, billName); pstmt.setString(10, billAddr); pstmt.setString(11, billProvince); pstmt.setString(12, billCity); pstmt.setString(13, billZip); pstmt.setString(14, billCountry); pstmt.setString(15, chargeCard.getCardNo()); pstmt.setString(16, chargeCard.getCardType()); pstmt.setString(17, chargeCard.getExpiryDateString()); pstmt.setDouble(18, totalPrice); int resultCount = pstmt.executeUpdate(); if (pstmt != null) pstmt.close(); if ( resultCount != 1 ) throw new SQLException("ERROR in ORDER_TABLE INSERT !! resultCount = " + resultCount); else { Statement stmt = conn.createStatement(); queryStr = "SELECT @@identity"; ResultSet rs = stmt.executeQuery(queryStr); if ( !rs.next() ) { throw new SQLException("ERROR in selecting OrderId !!"); } else { m_orderId = rs.getInt(1); if (m_orderId < 1) throw new SQLException("ERROR in getting OrderId !! orderId = "+ orderId); } Debug.println("--->>>Current order id is: " + m_orderId); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -