Column Properties
The ResultSetMetaData interface provides many methods for obtaining information about the properties of the columns of a result set.
ResultSetMetaData
boolean isAutoIncrement(int column);
boolean isCaseSensitive(int column);
boolean isSearchable(int column);
boolean isCurrency(int column);
int isNullable(int column);
boolean isSigned(int column);
boolean isReadOnly(int column);
boolean isWritable(int column);
boolean isDefinitelyWritable(int column);
Note that there is no way to get information on rows of data or their numbers. Such information is only available on columns. The method in Listing 7-7 gets the number and label of each column.
Listing 7-7: ResultSetMetaData.
...
...
int i;
// we have a ResultSet
ResultSetMetaData rsmd = rs.getMetaData();
int numCols = rsmd.getColumnCount();
// display column headers
for (i = 1; i <= numCols; i++)
{
if (i > 1) System.out.print(, );
System.out.print(rsmd.getColumnLabel(i));
}
System.out.println();
...
...
Dynamic Data Access
While the DatabaseMetaData and ResultSetMetaData interfaces provide numerous methods to access a database without knowing its schema, other methods exist to support generic data access. These methods allow data to be accessed dynamically through generic Java objects. They are of the form getObject() and setObject() to retrieve and to set data, respectively, in a table or result sets columns.
The usual methods to send and retrieve data allow you to map one or more SQL data types to specific Java types. The methods explained here allow you to use Java objects, regardless of what type SQL data is accessed. Precise mapping rules are used. Indeed, specific SQL data types must correspond to specific Java types, and specific Java types must correspond to specific SQL types.
Dynamically Typed Data Retrieval
Dynamically typed data retrieval is performed using ResultSet.getObject() to access columns, or CallableStatement.getObject() to access OUT parameters of stored procedures. Table 7-1 shows the default mapping from SQL types to Java types that are subtypes of Object. No mapping to Java streams is provided.
Table 7-1 DEFAULT MAPPING FROM SQL TYPES TO JAVA OBJECT TYPES
|
SQL type
| Java object type
|
CHAR
| String
|
VARCHAR
| String
|
LONGVARCHAR
| String
|
NUMERIC
| java.math.BigDecimal
|
DECIMAL
| java.math.BigDecimal
|
BIT
| Boolean
|
TINYINT
| Integer
|
SMALLINT
| Integer
|
INTEGER
| Integer
|
BIGINT
| Long
|
REAL
| Float
|
FLOAT
| Double
|
DOUBLE
| Double
|
BINARY
| byte[ ]
|
VARBINARY
| byte[ ]
|
LONGVARBINARY
| byte[ ]
|
DATE
| java.sql.Date
|
TIME
| java.sql.Time
|
TIMESTAMP
| java.sql.Timestamp
|
|
ResultSet
Object getObject(int columnIndex);
Object getObject(String columnName);
The getObject() method is used like other getXXX() methods but returns a Java object whose type may be discovered using Table 7-1. It corresponds to the SQL data type of the accessed result set column.
CallableStatement
void registerOutParameter(int parameterIndex, int sqlType);
void registerOutParameter(int parameterIndex, int sqlType, int scale);
Object getObject(int parameterIndex);
As seen earlier with callable statements, it is necessary to register OUT parameters before executing the call and accessing them. A Java object type corresponding to the SQL data type passed as parameter is returned.
Dynamically Typed Data Insertion/Update
Dynamically typed data insertion or update may only be performed through IN parameters of stored procedures or prepared statements. Specific SQL data types may be explicitly targeted in accordance with Table 7-2.
Table 7-2 CONVERSIONS BETWEEN JAVA OBJECT TYPES AND TARGET SQL TYPES
|
| TINYINT
| SMALLINT
| INTEGER
| BIGINT
| REAL
| FLOAT
|
String
| J
| J
| J
| J
| J
| J
|
BigDecimal
| J
| J
| J
| J
| J
| J
|
Boolean
| J
| J
| J
| J
| J
| J
|
Integer
| J
| J
| J
| J
| J
| J
|
Long
| J
| J
| J
| J
| J
| J
|
Float
| J
| J
| J
| J
| J
| J
|
Double
| J
| J
| J
| J
| J
| J
|
byte[ ]
|
|
|
|
|
|
|
java.sql.Date
|
|
|
|
|
|
|
java.sql.Time
|
|
|
|
|
|
|
java.sql.Timestamp
|
|
|
|
|
|
|
|
| DOUBLE
| DECIMAL
| NUMERIC
| BIT
| CHAR
| VARCHAR
|
String
| J
| J
| J
| J
| J
| J
|
BigDecimal
| J
| J
| J
| J
| J
| J
|
Boolean
| J
| J
| J
| J
| J
| J
|
Integer
| J
| J
| J
| J
| J
| J
|
Long
| J
| J
| J
| J
| J
| J
|
Float
| J
| J
| J
| J
| J
| J
|
Double
| J
| J
| J
| J
| J
| J
|
byte[ ]
|
|
|
|
|
|
|
java.sql.Date
|
|
|
|
| J
| J
|
java.sql.Time
|
|
|
|
| J
| J
|
java.sql.Timestamp
|
|
|
|
| J
| J
|
|
| LONGVARCHAR
| BINARY
| VARBINARY
| LONGVARBINARY
|
String
| J
| J
| J
| J
|
BigDecimal
| J
|
|
|
|
Boolean
| J
|
|
|
|
Integer
| J
|
|
|
|
Long
| J
|
|
|
|
Float
| J
|
|
|
|
Double
| J
|
|
|
|
byte[ ]
|
| J
| J
| J
|
java.sql.Date
| J
|
|
|
|
java.sql.Time
| J
|
|
|
|
java.sql.Timestamp
| J
|
|
|
|
|
| DATE
| TIME
| TIMESTAMP
|
String
| J
| J
| J
|
BigDecimal
|
|
|
|
Boolean
|
|
|
|
Integer
|
|
|
|
Long
|
|
|
|
Float
|
|
|
|
Double
|
|
|
|
byte[ ]
|
|
|
|
java.sql.Date
| J
|
| J
|
java.sql.Time
|
| J
|
|
java.sql.Timestamp
| J
| J
| J
|
|
A J means the conversion can be done. No support for Java streams is provided.
|
|