IBM DB2 Manual page 52

Table of Contents

Advertisement

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
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
2. Invoke the getColumnCount method to determine how many columns are in the
3. For each column in the ResultSet, execute ResultSetMetaData methods to
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.
String s;
Connection con;
Statement stmt;
ResultSet rs;
ResultSetMetaData rsmtadta;
int colCount
int mtadtaint;
int i;
String colName;
String colType;
...
stmt = con.createStatement();
rs = stmt.executeQuery("SELECT * FROM EMPLOYEE");
rsmtadta = rs.getMetaData();
colCount = rsmtadta.getColumnCount();
for (i=1; i<= colCount; i++) {
}
Figure 14. Using ResultSetMetaData methods to get information about a ResultSet
36
Application Programming Guide and Reference for Java
nullability
ResultSetMetaData object.
ResultSet.
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.
colName = rsmtadta.getColumnName();
colType = rsmtadta.getColumnTypeName();
System.out.println("Column = " + colName +
" is data type " + colType);
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
// Create a Statement object
// Get the ResultSet from the query
// Create a ResultSetMetaData object 1
// Find number of columns in EMP
// Get column name
// Get column data type
// Print the column value
2
3

Hide quick links:

Advertisement

Table of Contents
loading

Table of Contents