|
Im using MS SQL server and this is how i do
it...
The contacts table has an auto inc id
field.
Alter Procedure
CreateNewContact
( @type int, @info varchar(50), @treadID int ) As Insert into Contacts (Type,Info,ThreadID)
VALUES(@type, @info, @treadID)
return @@Identity And my entity bean
public ContactPK ejbCreate( int
type, String info, int threadID ) throws CreateException, RemoteException
{
System.out.println( "IN Contact.ejbCreate(int, string, int)" );
this.db_data.type = type;
this.db_data.info = info; this.db_data.threadID = threadID; this.db_data.props = new Properties();
Throwable rethrow = null;
int contactID = 0; Connection conn = null; CallableStatement ps = null; try { conn = getDBConnection(); ps = conn.prepareCall("{?=call CreateNewContact( ?,?,? ) }"); ps.registerOutParameter( 1, java.sql.Types.INTEGER ); ps.setInt(2, type); // ps.setString(3, info); ps.setObject(3, info, java.sql.Types.VARCHAR); // CHECK Bug Id 4274977 at java.sun.com, SetString() padds the db field even when its varchar!!! ps.setInt(4, threadID); ps.execute(); this.db_data.id = ps.getInt(1); System.out.println("new contact id from db is" + db_data.id ); } catch ( Exception e )
{
System.out.println("Error executing sql " + e); rethrow = e; } finally { if ( ps
!= null )
{
try { ps.close(); } catch(Exception e) {} ps = null; } if (
conn != null )
{
try { conn.close(); } catch(Exception e) {} conn = null; } if (
rethrow != null )
{
throw new CreateException( "Failed to create contact, " + rethrow.toString() ); } } return new ContactPK(
this.db_data.id );
} I hope this helps.
Peter Henderson.
|
- [jBoss-User] SQLServer getting autoincrement pk Bill Pfeiffer
- RE: [jBoss-User] SQLServer getting autoincrement pk Jay Walters
- Re: [jBoss-User] SQLServer getting autoincrement ... Peter Henderson
- Re: [jBoss-User] SQLServer getting autoincrem... Bill Pfeiffer
- RE: [jBoss-User] SQLServer getting autoincrement pk Wes Mckean
