I am a new member in this forum. I have just one question.
I am using oracle8.i and i insert data in this db. The pb is that in a table, i use a trigger which controle automatically primary key of this table.After doing an insertion in this table, i want to have the value of Primary key.
id = new IDENTIFICATION(fic); long identification_id = id.getId();
return identification_id;
} -------- the pb is the result of identification_id is always "0". I want to have the real value of identification_id which is conform to the code_sicovam, ....
I am building a constructor which takes those values and i am trying to extract the value by using .get_id().
If this is logic or not? If there are any another way, please assign it to me. Cest urgent!!!!!!!!!
I am not 100% sure what you are looking for by reading your post. My best guess is that you are inserting a record in identification table. Its primary key "id" is generated by trigger. after inserting the record you want to have the newly generated primary key for that table. If I guesed correctly then here is the solution. I am copying code for insert() method that inserts an email in Email table. The email_id is its primary key and it is generated by a trigger. After insert we get email_id in the program. This is a working code. You can reuse to work for your case.
publicvoid insert(Connection connection) throws SQLException {
String query = "begin " +
"insert into email " +
"(email_id, person_id, email_address, comments, inactive_date)" +
"values" +
"(null, ?, ?, ?, ?) returning email_id into ? ; " +
" end;";
// Prepare a statement
CallableStatement statement = connection.prepareCall(query);
statement.setLong(1, this.personId);
statement.setString(2, this.emailAddress);
statement.setString(3, this.comments);
statement.setDate(4, this.inactiveDate);
statement.registerOutParameter(5, Types.NUMERIC);
int rowsUpdated = statement.executeUpdate();
if (rowsUpdated != 1) {
// Give customized message as well as wrap the SQLException as its cause
SQLException e = new SQLException("Insert failed. Query:" + query);
e.initCause(statement.getWarnings());
// Close the statement
statement.close();
// Now throw the exception
throw e;
}
else {
// Let us get the newly generated email_id
this.emailId = statement.getLong(5);
}
// Close the statement
statement.close();
// We don;t close connection here. The caller is responsible for closing
// the connection
}