在编写bmp时,通常都是另外写一个*helper.java文件来负责操作那些sql语句,可是在bmp中调用的helper中的方法是通过EJB容器来操作的。而每个bmp只允许有一个EJBCreate()、ejbLoad()、ejbPassivate()等方法。当我需要对几数据表插入数据,我写了几个insert方法放在helper中,可是我如何在EJBCreate()中调用?(因为这几个insert语句,我不希望同时调用)
CustomersBean.java中的一段代码: public String EJBCreate(CustomersRecord data) throws CreateException { System.out.println("BMPCustomers.ejbCreate executing" + " (data:" + data + ")");
// Use the helper class to perform the INSERT CustomersJDBCHelper.insert(data);????????? CustomersJDBCHelper.insert1(data);?????????//是不是两者都必须执行,还可以只执行其中一个 // Save the state while we're here. The container can skip the EJBLoad. // By definition we are now running in a transactional context, and so // this is the appropriate thing to do. name = data.getName(); address = data.getAddress();
// Return the primary key of the new row to the container return data.getCustID(); }
CustomersJDBCHelper.java中的一段代码: static void insert(CustomersRecord rec) throws DuplicateKeyException, CreateException {
Connection conn; int count; StringWriter swBuf = new StringWriter(); PrintWriter pwBuf = new PrintWriter(swBuf); pwBuf.println("INSERT into CUSTOMERS "); pwBuf.println(" (CUSTID, NAME, ADDRESS) "); pwBuf.println("VALUES ("); pwBuf.println("'" + rec.getCustID() + "', "); pwBuf.println("'" + rec.getName() + "', "); pwBuf.println("'" + rec.getAddress() + "')");
conn = getConnection(); try { count = conn.createStatement().executeUpdate(swBuf.toString()); conn.close();
} catch (SQLException sqle) { try { conn.close(); } catch (SQLException sqle1) { } throw new EJBException("SQLException while creating record: " + sqle.getMessage()); }
if (count != 1) throw new EJBException("Record not created, no error reported from database"); } 如果再写一个insert1方法也是负责往数据库中插入数据
|
|