Beruflich Dokumente
Kultur Dokumente
ResultSet
a b c
---------- ------------ -----------
12345 Cupertino 2459723.495
83472 Redmond 1.0
83492 Boston 35069473.43
297
298 RESULTSET
int i = rs.getInt("a");
String s = rs.getString("b");
float f = rs.getFloat("c");
System.out.println("ROW = " + i + " " + s + " " + f);
}
String s = rs.getString(2);
String s = rs.getString("title");
Note that columns are numbered from left to right starting with column 1.
Also, column names used as input to getXXX methods are case insensitive.
The option of using the column name was provided so that a user who speci-
fies column names in a query can use those same names as the arguments to
getXXX methods. If, on the other hand, the select statement does not specify column
names (as in “select * from table1” or in cases where a column is derived), column
numbers should be used. In such situations, there is no way for the user to know
for sure what the column names are.
RESULTSET 299
In some cases, it is possible for an SQL query to return a result set that has
more than one column with the same name. If a column name is used as the
parameter to a getXXX method, getXXX will return the value of the first matching
column name. Thus, if there are multiple columns with the same name, one needs
to use a column index to be sure that the correct column value is retrieved. It may
also be slightly more efficient to use column numbers.
If the name of a column is known but not its index, the method findColumn can
be used to find the column number.
Information about the columns in a ResultSet is available by calling the method
ResultSet.getMetaData. The ResultSetMetaData object returned gives the number, types,
and properties of its ResultSet object’s columns. Refer to “ResultSetMetaData” on
page 319 for more information.
T S I B R F D D N B C V L B V L D T T
I M N I E L O E U I H A O I A O A I I
N A T G A O U C M T A R N N R N T M M
Y L E I L A B I E R C G A B G E E E
I L G N T L M R H V R I V S
N I E T E A I A A Y N A T
T N R L C R R A R A
T C R B M
H Y I P
A N
R A
R
Y
getByte X x x x x x x x x x x x x
getShort x X x x x x x x x x x x x
getInt x x X x x x x x x x x x x
getLong x x x X x x x x x x x x x
getFloat x x x x X x x x x x x x x
getDouble x x x x x X X x x x x x x
getBigDecimal x x x x x x x X X x x x x
getBoolean x x x x x x x x x X x x x
getString x x x x x x x x x x X X x x x x x x x
getBytes X X x
getDate x x x X x
getTime x x x X x
getTimestamp x x x x x X
getAsciiStream x x X x x x
getUnicodeStream x x X x x x
getBinaryStream x x X
getObject x x x x x x x x x x x x x x x x x x x
• getBinaryStream—returns a stream that simply provides the raw bytes from the
database without any conversion.
• getAsciiStream—returns a stream that provides one-byte ASCII characters.
• getUnicodeStream—returns a stream that provides two-byte Unicode characters.
Note that this differs from Java streams, which return untyped bytes and can
(for example) be used for both ASCII and Unicode characters. Note also that
JDBC Unicode streams expect big-endian data; that is, they expect data with the
302 RESULTSET
high byte first and the low byte second. This conforms to the standard endian
defined by Java, which is important if a program is to be portable.
The following code demonstrates using the method getAsciiStream:
• A Java null value for those getXXX methods that return Java objects (getString, get-
BigDecimal, getBytes, getDate, getTime, getTimestamp, getAsciiStream, getUnicodeStream, get-
BinaryStream, getObject).
• A zero value for getByte, getShort, getInt, getLong, getFloat, and getDouble.
• A false value for getBoolean.
ments, which return a count of the rows updated, or for data definition language
statements). However, under some circumstances an application may not know
whether a given statement will return a result set until the statement has executed. In
addition, some stored procedures may return several different result sets and/or
update counts.
To accommodate these situations, JDBC provides a mechanism so that an
application can execute a statement and then process an arbitrary collection of
result sets and update counts. This mechanism is based on first calling a fully gen-
eral execute method, and then calling three other methods, getResultSet, getUpdateCount,
and getMoreResults. These methods allow an application to explore the statement
results one at a time and to determine if a given result was a ResultSet or an update
count. Most programmers will never need to use these methods. For those who do,
a full explanation with a code example can be found in the section “Using the
Method execute” on page 348.
NOTE: All getXXX methods except getCursorName, getMetaData, and getWarnings have two
versions, one that takes a column name and one that takes a column number. Since
this is the only difference between them, the two versions have been combined into
one entry to avoid unnecessary repetition. Also, the explanation of parameters for all
getXXX methods is given here once and is not repeated in the individual method
explanations.
The parameter for each getXXX method is one of the following:
clearWarnings
Clears any warnings for the ResultSet. After calling clearWarnings, a call to get-
306 RESULTSET
Warnings will return null until a new warning is reported for this ResultSet.
EXAMPLE:
rs.clearWarnings();
close
findColumn
getAsciiStream
getBigDecimal
EXAMPLES:
java.math.BigDecimal num = rs.getBigDecimal(1, 8);
// retrieves a BigDecimal with 8 digits after the decimal point
// from column 1 of the ResultSet rs
or
java.math.BigDecimal num = rs.getBigDecimal("Number", 2);
// retrieves a BigDecimal with 2 digits after the decimal point
308 RESULTSET
getBinaryStream
getBoolean
getByte
getBytes
getCursorName
getDate
getDouble
double d = rs.getFloat(3);
or
double d = rs.getDouble("Square_Root");
getFloat
getInt
getLong
getMetaData
SEE:
ResultSetMetaData
getObject
Gets the value of the column columnIndex (or columnName) as a Java Object.
This method may be used to read database-specific abstract datatypes. It
is also useful when an application wants to be generic and be able to read any
datatype.
RETURNS:
RESULTSET 313
SEE:
Table 21.3, “JDBC Types Mapped to Java Object Types” on page 395
getShort
getString
getTime
getTimestamp
getUnicodeStream
RESULTSET 315
getWarnings
Gets the first SQLWarning that has been reported for this ResultSet object.
Subsequent warnings, if there are any, are chained to the first warning. The
warning chain is automatically cleared each time a new row is read.
NOTE: This warning chain covers only warnings caused by ResultSet meth-
ods. Any warning caused by Statement methods (such as CallableStatement.getXXX
methods for reading OUT parameters) will be chained on the Statement object.
RETURNS:
the first SQLWarning or null if there are no warnings.
EXAMPLE:
SQLWarning w = rs.getWarnings();
316 RESULTSET
next
Since the cursor of a ResultSet object is initially positioned before the first
row, the first call to next makes the first row the current row; the second call
makes the second row the current row, and so on. If a getXXX method is called
before the first call to next, an exception will be thrown. An exception will also
be thrown if a getXXX method is called after the method next returns false.
If an input stream from the previous row is open, it is implicitly closed.
The ResultSet object’s warning chain is cleared when a new row is read;
that is, the warning chain is cleared each time the method next is called.
RETURNS:
if the move to the next row was successful; false if there are no more rows.
true
This allows the use of next in a while loop, which is how it is typically used.
EXAMPLE:
ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM Table1");
while (rs.next()) {
// retrieve data with getXXX methods as long as next()
// returns true (there is a valid current row)
}
wasNull
Checks to see if the last value read was SQL NULL or not.
RETURNS:
true if the last value read was SQL NULL and false otherwise.
EXAMPLE:
ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM Table1");
while (rs.next()) {
int x = rs.getInt(1);
if (rs.wasNull()) { // check to see if x was SQL NULL
// do something if x was SQL NULL
RESULTSET 317
}else {
// do something else if x was not SQL NULL
}
String s = rs.getString(2); // if s = null, it was SQL NULL
Date d = rs.getDate(3); // if d = null, it was SQL NULL
}