Retrieving one autoincremented key value...

G

gbattine

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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Pls critique 1
Slow compared to php 7
Scrollable list... 2
help me with this JSF form...... 0
What's wrong in this transaction? 12

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top