Databases Reference
In-Depth Information
The JDBC API provides the following methods of fetching data from a result set:
•
Generic data type method, such as
getObject()
•
Specific data type method, such as
getInt()
,
getLong()
, and
getString()
Because the
getObject()
method is generic, it provides poor performance
when nondefault data type mappings are specified. The driver must perform
extra processing to determine the data type of the value being fetched and gener-
ate the appropriate mapping. This process is called
boxing
. When boxing occurs,
memory is allocated from the Java heap on the database client to create an object,
which can force a garbage collection to occur. See “Garbage Collection,” page 79,
for more information about the impact garbage collection has on performance.
Performance Tip
Use a specific method of fetching data for the data type instead of a
generic method. For example, use the
getInt()
method to fetch an
Integer value instead of the
getObject()
method.
You can also improve performance if you provide the column number of the
result column being fetched instead of the column name, such as
getString(1)
,
getLong(2)
, and
getInt(3)
. If column names are specified, the number of net-
work round trips doesn't increase, but costly lookups do. For example, suppose
that you specify the following:
getString("foo")...
If the column name is uppercase in the database, the driver must convert
foo
to uppercase (
FOO
) and then compare
FOO
to all the columns in the column
list. That's a costly operation, especially if the result set contains many columns.
If the driver can go directly to result column 23, a significant amount of process-
ing is saved.
For example, suppose you have a result set that has 15 columns and 100
rows. You want to retrieve data from only three columns:
employee_name
(string),
employee_number
(bigint),
and
salary
(integer).
If
you specify
getString("Employee_Name")
,
getLong("Employee_Number")
,
and