Java Reference
In-Depth Information
System.out.print(rs.getString(i)+((i==nColumns)?"\n":"\t"));
}
}
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
catch(SQLException e){
e.printStackTrace();
}
}
Notice in particular the use of the getColumnLabel method. This method returns the
preferred display name for the column, defaulting to the column name if no specific
label is assigned.
ParameterMetaData
The PreparedStatement method getMetaData() retrieves a ResultSetMetaData
object containing a description of the columns that will be returned when the
PreparedStatement is executed. Here's an example:
PreparedStatement ps = con.PrepareStatement("SELECT * FROM CUSTOMERS");
ResultSetMetaData md = ps.getMetaData();
int cols = md.getColumnCount();
The method getParameterMetaData() returns a ParameterMetaData object
containing descriptions of the IN and OUT parameters the PreparedStatement uses,
as shown here:
PreparedStatement ps = con.PrepareStatement("SELECT * FROM CUSTOMERS");
ParameterMetaData pd = ps.getParameterMetaData();
int pType = pd.getParameterType(1);
Support for ParameterMetaData is provided as part of the JDBC 3.0 API,
and requires JDK 1.4
Note
JDBC Mapping of SQL Data Types
The JDBC Core API provides automatic type conversion between SQL data types
and Java data types. Table 4-5 summarizes these conversions.
Table 4-5: Standard Mapping from SQL Types to Java
SQL type
Java Type
Description
CHAR
String
Fixed-length character string. For a CHAR type
Search WWH ::




Custom Search