Retrieving one autoincremented key value...
Hi guys,
i'm developing a jsf application and i have a problem.
I insert some values into a table which has an autoincremented id.
Later i've to retrieve this autoid.
This is my bean
public String AddNewExp() throws SQLException {
DataSource dataSource = Singleton.getInstance().getDataSource();
Connection conn = dataSource.getConnection();
if (conn != null) { AuthenticationBean bean2 = (AuthenticationBean)
ViewUtils.eval("#{authenticationBean}");
PreparedStatement pst2 = null;
pst2 = conn.prepareStatement("INSERT INTO
esperimento(username,nometeam,piattaforma,links_url,tipoesperimento,controlliqualita,brevedescrizione,numeroibridazioni,autore,laboratorio,contatti,protocolloscansione,softwareanalisi,tiponormalizzazione,scalalogaritmica,test,parametri,fattoredicorrezione,fattore1,fattore2,fattore3,fattore4)
VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",Statement.RETURN_GENERATED_KEYS);
pst2.setString(1, bean2.getLoginName());
pst2.setString(2,bean2.getTeamName());
pst2.setString(3, platformName);
pst2.setString(4,links);
pst2.setString(5, toe);
pst2.setString(6, qcst);
pst2.setString(7, description);
pst2.setInt(8, hybridationNumbers);
pst2.setString(9, author);
pst2.setString(10, laboratory);
pst2.setString(11, contact);
pst2.setString(12, scanning);
pst2.setString(13, software);
pst2.setString(14, normalization);
pst2.setString(15, logarithmic);
pst2.setString(16, test);
pst2.setString(17, parameter);
pst2.setString(18, correction);
pst2.setString(19, factor1);
pst2.setString(20, factor2);
pst2.setString(21, factor3);
pst2.setString(22, factor4);
u=1; // pst2.setBytes(25, x);
pst2.executeUpdate();
u=2;
int autoIncKeyFromApi = -1;
u=3;
ResultSet rs = null;
rs =
pst2.getGeneratedKeys();
if (rs==null){
u=100;
}
if (rs.next()) {
autoIncKeyFromApi = rs.getInt(1);
else { // throw an exception from here }
}
rs.close();
pst2.close();
conn.close();
}
return "go";
}
I've tried to output the autoIncKeyFromApi in a jsp page but i see only
value 0...also if i add the sixth row,the seventh one etc...
Why?
Can you help me?
Thanks very much...