Each result set is described by its meta data. To get meta data for a result
set, we use the getMetaData() method for the result set. For example:
ResultSetMetaData rsmd=rs.getMetaData();
We can illustrate this best with some sample code. For the following
example code, we assume the connection
con
has been created (see ,
 Creating the DB2 connection  on page 84) and an output stream
out
defined.
a. First, we create the statement:
Statement=con.createStatement();
b. Next, we execute the statement of type query, with a return result of
type ResultSet:
ResultSet rs=stmt.executeQuery( select * from userinfo );
c. To print out the first column of each result set row, we have the
following code snippet:
while (rs.next()) {
out.print(rs.getString(1));
}
d. To get even more fancy, we can print out all of columns of each result
set row by obtaining the result set meta data, determining how many
columns are in the result set, and then printing out each column with
the getString method:
ResultSetMetaData=rs.getMetaData();
int numFields=rsmd.getColumnCount();
while (rs.next()) {
for (int currentField=1; i<=numFields; currField++) {
out.print(rs.getString(currField));
}
// next row
out.println();
}
Fields/Columns start at 1
Note that the field/column count starts at 1. Hence, the for loop in the
preceding sample code starts looping from 1.
The getString method can be used with a column number as we have
done or with a column name, for example:
rs.getString( first_name )
You will see this method used in both forms in later examples.
86
Linux Web Hosting with WebSphere, DB2, and Domino




  

Home

About Services Network Support FAQ Order Contact
 

Linux Web Hosting

Our partners:Jsp Web Hosting Unlimited Web Hosting Cheapest Web Hosting  Java Web Hosting Web Templates Best Web Templates PHP Mysql Web Hosting Interland Web Hosting Cheap Web Hosting PHP Web Hosting Tomcat Web Hosting Quality Web Hosting Best Web Hosting  Mac Web Hosting 

Lunarwebhost.net  Business web hosting division of Vision Web Hosting Inc. All rights reserved