tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Matt Brennan <>
Subject servlet-jdbc-SQL server
Date Mon, 19 Aug 2002 15:36:24 GMT
Just taking some stumbling first steps talking to SQL server from a
servlet using the Microsoft JDBC driver. Servlet container is tomcat
4.0.4. Quick question if I may:

I need to insert a row into an SQL Server table and have the servlet
generate a page that includes an automatically generated IDENTITY from
the newly created row. How after the insert can I find the generated ID
without conducting a subsequent query (which would have no guarantee of
returning uniquely the newly inserted record)?

  // 'con' is a connection to MS-SQLserver with SelectMethod=cursor
  Statement state = con.createStatement(

  // Generate an empty result set
  ResultSet content  = state.executeQuery(sqlQuery);

  // Column 1 is id int IDENTITY NOT NULL PRIMARY KEY
  // Column 2 is name VARCHAR(20)
  content.updateString (2, "something");

  // If I then query the inserted row, to find the newly generated
  // IDENTITY value I get MIN_INT instead of '1' (the default first
  // identity generated by SQL server if no seed is given)
  int id = content.getInt (1);

Is this some peculiarity of the servlet interface to the driver, to the
driver itself or just my bad undestanding of the insert process? Is it
possible to configure the Statement so that the Insert row in the Result
Set gets updated when the insert takes place and the 'id' gets generated?

Please excuse the fact that this query starts to look more like a JDBC
query than a tomcat query - not quite sure where the problem lies -
tomcat+driver? driver? my understanding (most likely!). I am hoping that
other servlet developers have crossed this bridge before.


To unsubscribe, e-mail:   <>
For additional commands, e-mail: <>

View raw message