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.

• First, instead of using the one statement object we have to use three – one per<br />

statement.<br />

• Second, before executing the statement we have to set parameters first. As it is<br />

showed on the example, parameters are referenced by their position. The<br />

PreparedStatement interface provides setter methods for all primitive types in<br />

<strong>Java</strong> as well as for some widely used SQL data types (BLOBs, CLOBs, etc.).<br />

The NULL value is set by calling the PreparedStatement.setNull(int)<br />

method.<br />

• Third, we are forced now to use three nested try/finally blocks, which<br />

makes code less readable.<br />

So, where's the advantage? First of all, we can redesign our application to prepare<br />

those statements before calling that code (for example in a constructor) and close<br />

them when application is shut down. In this case the code is even more compact<br />

(see Illustration 4.5). Unfortunately the application is now responsible for<br />

prepared statement management. When connection is closed, the prepared<br />

statement object will be invalidated, but the application will not be notified about<br />

this fact. Also, if the application uses similar statements in different parts of the<br />

application, the refactoring might affect many classes, possibly destabilizing the<br />

Chapter 4. Executing statements 40

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

Saved successfully!

Ooh no, something went wrong!