20-Aug-03 (Created: 20-Aug-03) | More in 'Howto'

Revisting updates in aspire: How to get the last insert id in MicrosoftAccess

Step1

If you were to fire off a URL like


http://localhost:8080/aspire/servlet/UpdateServlet?request_name=doinsert&arg1=10

then the properties file need to have an entry identifying


doinsert

Property file entries


request.doinsert.classname=com.ai.db.DBPreTranslateArgsMultiRequestExectuor
request.doinsert.db=yourdb
request.doinsert.query_type=update
request.doinsert.request.1=INS-1
request.doinsert.request.2=GETKEY
request.doinsert.request.3=INS-2

#Redirect the user to a page once successful
request.doinsert.redirectURL=/aspire/servlet/DisplayServlet?url=SOMEASPIREURL&newkey={my-key}&(any additional args)

#Redirect the user to a different page if failed
request.doinsert.failureRedirectURL=/aspire/servlet/DisplayServlet?url=SOMEotherASPIREURL&(any additional args)


#INS-1
request.INS-1.classname=com.ai.db.DBRequestExecutor2
request.INS-1.db=yourdb
request.INS-1.query_type=update
request.INS-1.stmt=your-insert-statemnt

#GETKEY
request.GETKEY.classname=com.ai.db.DBRequestExecutor2
request.GETKEY.db=yourdb
request.GETKEY.stmt=\
select @@identity as my_key


#INS-2
request.INS-2.classname=com.ai.db.DBRequestExecutor2
request.INS-2.db=yourdb
request.INS-2.query_type=update
request.INS-2.stmt=your-insert-statemnt with my_key as one of its args

Explnations

The nature of the UpdateServlet is to execute the class identified by "doinsert" and redirect the page to either a success page or a failure page. In this example the "doinsert" is pointing to a pre-fabricated aspire part that knows how to execute multiple updates and selects.

In the example above these select, inserts are a) INS-1 b) GETKEY c)INS-2. The names are self explanatory. INS-1 will insert a row in to the database. GETKEY will retrieve the unique id that the previous insert used. INS-2 will use another insert statement using the key that is selected in GETKEY. If your intention is to pass this key back to the page then you do that using the redirectURL and passing the key value as one of the url arguments. At this point a variable called "my-key" is introduced into the parameter basket (hashtable).

The @@identity is a special word that works in microsoft related databases. For other databases this may be different.

Further references

1. How to guide word document
2. Aspire users guide word document