Java Reference
In-Depth Information
compliant, a SQLException may be thrown.
ResultSetMetaData
Information about the columns in a ResultSet is available by calling the getMetaData()
method. The ResultSetMetaData object returned gives the number, types, and
properties of its ResultSet object's columns.
Some of the methods available to access ResultSetMetaData are as follows:
 
getColumnCount() — Returns the number of columns in the ResultSet
 
getColumnDisplaySize(int column)— Returns the column's normal max width in chars
 
getColumnLabel(int column) — Returns the column title for use in printouts and displays
 
getColumnName(int column) — Returns the column name
 
getColumnType(int column) — Returns the column's SQL data-type index
 
getColumnTypeName(int column)— Returns the name of the column's SQL data type
 
getPrecision(int column)— Returns the number of decimal digits in the column
 
getScale(int column) — Returns the number of digits to right of the decimal point
 
getTableName(int column) — Returns the table name
 
isAutoIncrement(int column) — Returns true if the column is automatically numbered
 
isCurrency(int column) — Returns true if the column value is a currency
 
isNullable(int column)— Returns true if the column value can be set to NULL
Listing 4-10 illustrates the use of the ResultSetMetaData methods getColumnCount
and getColumnLabel in an example where the column names and column count are
unknown.
Listing 4-10: Using ResultSetMetaData
public void printResultSet(String query){
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection
("jdbc:odbc:Inventory");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
ResultSetMetaData md = rs.getMetaData();
int nColumns = md.getColumnCount();
for(int i=1;i<=nColumns;i++){
System.out.print(md.getColumnLabel(i)+((i==nColumns)?"\n":"\t"));
}
while (rs.next()) {
for(int i=1;i<=nColumns;i++){
Search WWH ::




Custom Search