請教各位高手一個問題

alibaba2002發表於2003-02-14


在編寫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方法也是負責往資料庫中插入資料

相關文章