Hi,
Illustrated in the code below for a BMP Entity bean.
The primary key is passed to the ejbCreate method and the same is used for ejbFind too.
public
String ejbCreate(String accountId, double initialBalance) throws CreateException
{
this.accountId = accountId;
this.balance = initialBalance;
Connection con = null;
PreparedStatement ps = null;
try
{
con = getConnection();
ps = con.prepareStatement("insert into xxx (sampid, sampbal) values (?, ?)");
ps.setString(1, accountId);
ps.setDouble(2, balance);
if (ps.executeUpdate() != 1)
{
String error = "Row error";
log(error);
throw new CreateException (error);
}
return accountId;
}
catch (SQLException sqe)
{
try
{
ejbFindByPrimaryKey(accountId);
}
catch(ObjectNotFoundException onfe) {}
}
xxxxxxxxxxxxxxxxxxxxxxxxxxx
}
public String ejbFindByPrimaryKey(String pk) throws ObjectNotFoundException
{ Connection con = null;
PreparedStatement ps = null;
try
{ con = getConnection();
ps = con.prepareStatement("select sampbal from xxx where sampid = ?");
ps.setString(1, pk);
ps.executeQuery();
ResultSet rs = ps.getResultSet();
if (rs.next())
{ balance = rs.getDouble(1);
}
else
{ throw new xxxxxxxxxxxxxxException (error);
}
}
catch (SQLException sqe) { }
finally {
cleanup(con, ps);
}
return pk;
}
Rgds,
Seetesh