Listing 1.18-a shows an SPL procedure that loops through the emp table and gives each employee a 10% raise. As each employee is processed,
adjustSalary executes a
RAISE NOTICE statement (in this case, the message contained in the notification reports progress to the client application). Listing 1.18-b will demonstrate how to create a
NoticeListener that intercepts each notification.
The NoticeExample() method is straightforward; it expects a single argument, a
Connection object, from the caller:
NoticeExample() begins by preparing a call to the
adjustSalary procedure shown in example 1.10-a. As you would expect,
con.prepareCall() returns a
CallableStatement object. Before executing the
CallableStatement, you must create an object that implements the
NoticeListener interface and add that object to the list of
NoticeListeners associated with the
CallableStatement:
Once the NoticeListener is in place,
NoticeExample method executes the
CallableStatement (invoking the
adjustSalary procedure on the server) and displays a message to the user:
Each time the adjustSalary procedure executes a
RAISE NOTICE statement, the server sends the text of the message (
"Salary increased for ...") to the
Statement (or derivative) object in the client application. JDBC invokes the
noticeReceived() method (possibly many times)
before the call to
stmt.execute() completes.
When JDBC calls the noticeReceived() method, it creates an
SQLWarning object that contains the text of the message generated by the
RAISE NOTICE statement on the server.
Notice that each Statement object keeps a
list of
NoticeListeners. When the JDBC driver receives a notification from the server, it consults the list maintained by the
Statement object. If the list is empty, the notification is saved in the
Statement object (you can retrieve the notifications by calling
stmt.getWarnings() once the call to
execute() completes). If the list is not empty, the JDBC driver delivers an
SQLWarning to each listener, in the order in which the listeners were added to the
Statement.