27.10.2013 Views

Jaybird 2.1 JDBC driver Java Programmer's Manual - Firebird

Jaybird 2.1 JDBC driver Java Programmer's Manual - Firebird

Jaybird 2.1 JDBC driver Java Programmer's Manual - Firebird

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

is no measurable performance differences when using the callable statement<br />

interface.<br />

Also the <strong>JDBC</strong> specification allows another syntax for the stored procedure calls:<br />

CallableStatement stmt = connection.prepareCall(<br />

"{?= call factorial(?}");<br />

stmt.registerOutParameter(1, Types.INTEGER);<br />

stmt.setInt(2, 2);<br />

stmt.execute();<br />

int result = stmt.getInt(1);<br />

Illustration 4.13.: Calling stored procedure using different syntax.<br />

Note, that input parameters have now indices 2 and 3, and not 1 and 2 as in the<br />

previous example. This syntax seems to be more intuitive, as it looks like a<br />

function call. It is also possible to use this syntax for stored procedures that return<br />

more than one parameter by combining code from the second and the last<br />

examples.<br />

<strong>Firebird</strong> stored procedures can also return result sets. This is achieved by using the<br />

SUSPEND keyword inside the procedure body. This keyword returns the current<br />

values of the output parameters as a single row to the client.<br />

The following example is more complex and shows a stored procedure that<br />

computes a set of factorial of the numbers up to the specified number of rows.<br />

The SELECT SQL statement is the natural way of accessing the selectable<br />

procedures in <strong>Firebird</strong>. You “select” from such procedures using the Statement<br />

or PreparedStatement objects.<br />

With minor issues it is also possible to access selectable stored procedures<br />

through the CallableStatement interface. The escaped call must include all IN<br />

and OUT parameters. After the call is prepared, parameters are set the same way.<br />

However application must explicitly tell the <strong>driver</strong> that selectable procedure is<br />

used and access to the result set is desired. This is done by calling a <strong>Jaybird</strong>specific<br />

method as showed in the example below. When this is not done,<br />

application has access only to the first row of the result set.<br />

The getter methods from the CallableStatement interface will provide you<br />

access only to the first row of the result set. In order to get access to the complete<br />

result set you have to either call the executeQuery method or the execute<br />

method followed by getResultSet method.<br />

Chapter 4. Executing statements 48

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!