Hi, excuse-me for my poor English, but I�m trying to improve this!
I�m solved this problem in my EMP beans appliyng these methods:
//pk is the primary key field.
//field_1 and field_2 are other database fields.
//table is the database�s name.
public
String ejbCreate(String field_1, String field_2 ...) throws CreateException { //Don�t pass the Primary Key.
boolean exists = false;
try {
_insert(field_1, field_2);
pk = _getPrimaryKey();
}
catch(Exception e) {
e.printStackTrace();
}
return pk;
}
private void _insert(String field_1, String field_2) throws SQLException {
String insertStatement = "insert into table (field_1, field_2) values (?, ?)";
PrepareStatement pstm = con.PrepareStatement(insertStatement);
pstm.setString(1, field_1);
pstm.setString(2, field_2);
pstm.executeUpdate();
pstm.close();
}
private String _getPrimaryKey() throws SQLException {
String pk = "";
String selectStatement = "select max(primary_key_field) from table";
ResultSet rs = db.executeQuery(selectStatement);
if (rs.next()) {
pk = rs.getString(1);
return pk;
} else {
throw new EJBException("Unable to find a primary key!");
}
}
With me it�s work fine, did you understood? The basic logic is a method what find the last primary before de insert statement.
SPARC