Java Reference
In-Depth Information
ResultSet rs = sqlStmt.executeQuery(
"SELECT POSTING_ACCOUNT_ID, CURRENCY_ID FROM POSTING_ACCOUNTS WHERE
COMPANY_ID = 'CC1'");
The ResultSet class contains a next() method. This method positions the cur-
sor (the current position in the result set) in the next available row. When the re-
sult set is exhausted, next() returns false. This method is similar to the READ
NEXT statement in COBOL.
while (rs.next()) {}
You cannot retrieve any data from the ResultSet until you have done the first
next() . The next() method, however, does not actually return any data. To get
data into your own variables, you will need to call the proper getXXX() method
where XXX is the specific data type to return (e.g., getFloat() , getLong() , getInt() ,
etc.). JDBC is a strongly typed interface, meaning that each get() method is specific
to the data type referenced. There is a separate getXXX() method for strings, another
for floats, and another for ints.
The statements to retrieve information from the ResultSet named accounts
might look as follows. (Of course, each iteration of the next loop as written will over-
write the results of the previous iteration, but I want to keep the example simple.)
int postingAccountID;
String currencyID;
while (rs.next()) {
postingAccountID = acounts.getInt("POSTING_ACCOUNT_ID");
currencyID = acounts.getString("CURRENCY_ID");
}
In the preceding example, you are retrieving data from the result set by pass-
ing a column name as a string. You can also retrieve data by passing a column
number. Column numbers begin with one rather than zero, so the following code
would be equivalent.
while (rs.next()) {
postingAccountID = acounts.getInt (1);
currencyID = acounts.getString(2);
}
Search WWH ::




Custom Search