EasyManuals Logo

IBM DB2 User Manual

IBM DB2
585 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Page #52 background imageLoading...
Page #52 background image
v The number of columns in a ResultSet
v The qualifier for the underlying table of the ResultSet
v Information about a column, such as the data type, length, precision, scale, and
nullability
v Whether a column is read-only
After you invoke the executeQuery method to generate a ResultSet for a query on
a table, follow these basic steps to determine the contents of the ResultSet:
1. Invoke the getMetaData method on the ResultSet object to create a
ResultSetMetaData object.
2. Invoke the getColumnCount method to determine how many columns are in the
ResultSet.
3. For each column in the ResultSet, execute ResultSetMetaData methods to
determine column characteristics.
The results of ResultSetMetaData.getColumnName call reflects the column name
information that is stored in the DB2 catalog for that data source.
The following code demonstrates how to determine the data types of all the
columns in the employee table. The numbers to the right of selected statements
correspond to the previously-described steps.
Related tasks
“Retrieving multiple result sets from a stored procedure in a JDBC application”
on page 46
“Calling stored procedures in JDBC applications” on page 45
“Retrieving data from tables using the Statement.executeQuery method” on
page 32
String s;
Connection con;
Statement stmt;
ResultSet rs;
ResultSetMetaData rsmtadta;
int colCount
int mtadtaint;
int i;
String colName;
String colType;
...
stmt = con.createStatement(); // Create a Statement object
rs = stmt.executeQuery("SELECT * FROM EMPLOYEE");
// Get the ResultSet from the query
rsmtadta = rs.getMetaData(); // Create a ResultSetMetaData object 1
colCount = rsmtadta.getColumnCount(); 2
// Find number of columns in EMP
for (i=1; i<= colCount; i++) { 3
colName = rsmtadta.getColumnName(); // Get column name
colType = rsmtadta.getColumnTypeName();
// Get column data type
System.out.println("Column="+colName +
" is data type " + colType);
// Print the column value
}
Figure 14. Using ResultSetMetaData methods to get information about a ResultSet
36 Application Programming Guide and Reference for Java

Table of Contents

Questions and Answers:

Question and Answer IconNeed help?

Do you have a question about the IBM DB2 and is the answer not in the manual?

IBM DB2 Specifications

General IconGeneral
DeveloperIBM
Initial Release1983
LicenseProprietary
Written inC, C++
CategoryDatabase Management System (RDBMS)
Operating SystemLinux, Windows
Programming LanguagesC, C++
Data ModelsRelational
EditionsCommunity, Standard, Advanced
Key Featureshigh availability, scalability