You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/docs/asciidoc/chapters/statements/statements.adoc
+31-10Lines changed: 31 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,10 +4,11 @@
4
4
After obtaining a connection, the next thing to do is to execute an SQL statement.
5
5
The JDBC specification distinguishes three kinds of statements:
6
6
7
-
1. Regular statements to execute SQL statements without parameters,
8
-
2. prepared statements to execute SQL statements with parameters,
9
-
3. and callable statements to execute stored procedures.
7
+
1. <<stmt-statement,Regular statements>> to execute SQL statements without parameters,
8
+
2. <<stmt-prepared,prepared statements>> to execute SQL statements with parameters,
9
+
3. and <<stmt-callable,callable statements>> to execute stored procedures.
10
10
11
+
[#stmt-statement]
11
12
=== The java.sql.Statement interface
12
13
13
14
The `java.sql.Statement` interface is the simplest interface to execute SQL statements.
@@ -52,10 +53,11 @@ A statement can be executed using the following methods:
52
53
53
54
* `Statement.executeQuery(String)` -- executes a `SELECT` or other result set producing statement, and returns a result set.
54
55
If the specified statement does not produce a result set, an `SQLException` is thrown after statement executionfootnote:[This is an implementation detail and may change in the future to throw an exception _before_ execution].
55
-
* `Statement.executeUpdate(String)` -- executes other DML (Data Manipulation Language, e.g. `INSERT`, `UPDATE`, `DELETE`) or DDL (Data Definition Languagefootnote:[This term is used to group all statements that are used to manipulate database schema, i.e. creation of tables, indices, views, etc.]) statements and returns the number of updated rows.
56
+
* `Statement.executeUpdate(String)` -- executes other DML (Data Manipulation Language, e.g. `INSERT`, `UPDATE`, `DELETE`) or DDL (Data Definition Languagefootnote:[This term is used to group all statements that are used to manipulate database schema, i.e. creation of tables, indices, views, etc.]) statements and returns the number of updated rows -- as `int`.
56
57
If the specified statement is a query or otherwise produces a result set, an `SQLException` is thrown.
58
+
* `Statement.executeLargeUpdate(String)` -- same as previous, except it returns the update count as `long`.
57
59
* `Statement.execute(String)` -- executes a statement and returns `true` when the statement returned a result set, or `false` if the result has no result set, but has an update count or nothing as its result.
58
-
You can use `Statement.getResultSet()` to get the result of the executed query, or you can use `Statement.getUpdateCount()` when you have executed an update statement.
60
+
You can use `Statement.getResultSet()` to get the result of the executed query, or you can use `Statement.getUpdateCount()` or `getLargeUpdateCount()` when you have executed an update statement.
59
61
+
60
62
Formally, this method is also used for statements with multiple results (result sets and update counts), but Firebird only supports at most one result set and at most one update count.
61
63
@@ -216,6 +218,7 @@ In that case, statements are not released even if the `close()` method is called
216
218
Likely, the only possibility to close pooled statements is to close the pooled connections.
217
219
Please check the documentation of your connection pool for more information.
218
220
221
+
[#stmt-prepared]
219
222
=== The java.sql.PreparedStatement interface
220
223
221
224
As we have seen, Jaybird already performs internal optimization when it comes to multiple statement execution -- it can reuse the allocated statement handle in subsequent calls.
@@ -340,7 +343,7 @@ A JDBC driver can ignore the request to close the prepared statement, save it in
340
343
341
344
NOTE: Jaybird currently does not perform statement caching
342
345
343
-
[[callable-statement]]
346
+
[#stmt-callable]
344
347
=== The java.sql.CallableStatement interface
345
348
346
349
The `CallableStatement` interface extends `PreparedStatement` with methods for executing and retrieving results from stored procedures.
@@ -359,7 +362,7 @@ Though the interface is generic enough to support database engines that can retu
359
362
These features are of no interest to Jaybird users, since Firebird does not support them.
360
363
361
364
The IN and OUT parameters are specified in one statement.
362
-
The syntax above does not allow to specify the type of the parameter, therefore additional facilities are needed to tell the driver which parameter is will contain output values, the rest are considered to be IN parameters.
365
+
The syntax above does not allow to specify the type of the parameter, therefore additional facilities are needed to tell the driver which parameter contains output values, the rest are considered to be IN parameters.
363
366
364
367
==== Firebird stored procedures
365
368
@@ -526,6 +529,17 @@ Not that it makes much sense to do this, but it might help in some cases when po
526
529
It is also allowed to use a procedure call parameter both as an input and output parameter.
527
530
It is recommended to use this only when porting applications from the database servers that allow INOUT parameter types, such as Oracle.
528
531
532
+
[WARNING]
533
+
====
534
+
Future Jaybird versions may become stricter in how they handle IN and OUT parameters.
535
+
They will likely require a strict ordering of parameters to match the actual procedure definition, so interleaving IN and OUT, or marking a parameter position as both IN and OUT may no longer be supported.
536
+
537
+
Our recommendation: always use the order of IN parameters in the procedure definition, followed by the OUT parameters in the `RETURNS` clause of the definition.
538
+
539
+
Mixing order of parameters will then probably require use of Firebird 6.0 and its native `CALL` statement.
540
+
This is not final yet, and we may offer a backwards compatibility option, for a transition period of at least one major Jaybird version.
541
+
====
542
+
529
543
The actual stored procedure call using the `CallableStatement` is equivalent to the call using the prepared statement as shown in the first example.
530
544
There is no measurable performance differences when using the callable statement interface.
531
545
@@ -550,13 +564,20 @@ Note, that input parameter now has index 2, and not 1 as in the previous example
550
564
This syntax seems to be more intuitive, as it looks like a function call.
551
565
It is possible to use this syntax for stored procedures that return more than one parameter by combining code from the second and the last examples.
552
566
567
+
[WARNING]
568
+
====
569
+
Future Jaybird versions may become stricter in how they handle IN and OUT parameters, and especially the `++{?=call ...}++` syntax may be restricted to only support procedures with a single OUT parameter (i.e. a single parameter in the `RETURNS` clause of its definition).
570
+
571
+
This is not final yet, and we may offer a backwards compatibility option, for a transition period of at least one major Jaybird version.
572
+
====
573
+
553
574
Firebird stored procedures can also return result sets.
554
-
This is achieved by using the SUSPEND keyword inside the procedure body.
575
+
This is achieved by using the `SUSPEND` keyword inside the procedure body.
555
576
This keyword returns the current values of the output parameters as a single row to the client.
556
577
557
578
The following example is more complex and shows a stored procedure that computes a set of factorial of the numbers up to the specified number of rows.
558
579
559
-
The SELECT SQL statement is the natural way of accessing the selectable procedures in Firebird.
580
+
The `SELECT` SQL statement is the natural way of accessing the selectable procedures in Firebird.
560
581
You "`select`" from such procedures using the `Statement` or `PreparedStatement` objects.
561
582
562
583
// TODO Simplify example below
@@ -819,7 +840,7 @@ SELECT * FROM {oj tableA a
819
840
[#stmt-escape-sp]
820
841
==== Stored procedures
821
842
822
-
The escaped syntax for stored procedures is described in details in section <<callable-statement>>.
843
+
The escaped syntax for stored procedures is described in details in section <<stmt-callable>>.
0 commit comments