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.

:<br />

1<br />

2<br />

3<br />

Statement stmt = connection.createStatement();<br />

try {<br />

ResultSet rs = stmt.executeQuery(<br />

"SELECT firstName, lastName FROM users" +<br />

" WHERE userId = 5");<br />

rs.next();<br />

String firstName = rs.getString(1);<br />

String lastName = rs.getString(2);<br />

} finally {<br />

stmt.close();<br />

}<br />

Illustration 4.1: Typical way to execute query to get information about the user.<br />

1. Create a Statement object by calling the createStatement() method of the<br />

Connection object.<br />

2. Use the Statement object by calling its methods, in our case we execute simple<br />

query SELECT firstName, lastName FROM users WHERE userId = 5.<br />

Processing of the query result will be discussed in details in the next chapter.<br />

3. Close the statement to release all allocated resources. In our example this is<br />

done in the finally section of the try/finally block.<br />

The fact that connection object is a factory for the statement objects puts a<br />

constraint on the object lifetime – statements are bound to the connection; when<br />

the connection is closed, all statements that were created by that connection<br />

become invalid and the resources allocated by them are released. However,<br />

despite that fact that the resources are finally released, it is strongly recommended<br />

to use the try/finally block, to guarantee that resources are released as soon as<br />

possible because of the reasons that will be discussed later.<br />

Statement can be executed using the following methods:<br />

• Statement.executeQuery(String) – executes a SELECT statement and<br />

returns a result set. If the specified statement is not a SELECT statement, an<br />

SQLException is thrown after the statement execution.<br />

• Statement.executeUpdate(String) – executes INSERT, UPDATE,<br />

DELETE or DDL 1 statements and returns the number of updated rows. If the<br />

specified statement is a query, an SQLException is thrown.<br />

• Statement.execute(String) – executes a statement and returns true when<br />

the statement returned a result set, otherwise an update was executed and false<br />

is returned. You can use Statement.getResultSet() method to get the result<br />

of the executed query or you can use Statement.getUpdateCount() when you<br />

have executed update statement.<br />

Statement is closed by calling the Statement.close() method. After this the<br />

statement object is invalid and cannot be used anymore.<br />

1 DDL – Data Definition Language. This term is used to group all statements that are used to<br />

manipulate database schema, i.e. creation of tables, indices, views, etc.<br />

Chapter 4. Executing statements 34

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

Saved successfully!

Ooh no, something went wrong!