将ORACLE数据类型映射到JAVA数据类型

时间:2014-03-22 10:11:59

标签: java oracle orm metadata type-conversion

根据以下代码和表格:

ResultSetMetaData rsmd = rs.getMetaData();
int numberOfColumns = rsmd.getColumnCount();

for( int i=1; i <= numberOfColumns; i++ ) {
    // label if defined else name of column
    String columnLabel = rsmd.getColumnLabel( i ); 

    // data type in database
    String columnType = rsmd.getColumnTypeName( i ); 

    // java equivalent class name
    String columnClassName = rsmd.getColumnClassName( i ); 

    System.out.println( "Column Name : " + columnLabel );
    System.out.println( "Column Type : " + columnType );
    System.out.println( "Column Class: " + columnClassName );

    System.out.println();
} // for count of columns

// ---------------------表----------------------- -----------

CREATE TABLE  "TEST123" 
   (    "ID" NUMBER NOT NULL ENABLE, 
    "NAME" VARCHAR2(20), 
    "AGE" NUMBER(2,0), 
    "AVERAGE" NUMBER(2,2)
   ) ; 

// --------------------------------------------- -----------------

为什么输出数字字段,如上表所示,显示java.math.BigDecimal?我们希望程序显示整数,双精度,短数和...结果。

-------------output------------------
Column Name : ID
Column Type : NUMBER
Column Class: java.math.BigDecimal

Column Name : NAME
Column Type : VARCHAR2
Column Class: java.lang.String

Column Name : AGE
Column Type : NUMBER
Column Class: java.math.BigDecimal

Column Name : AVERAGE
Column Type : NUMBER
Column Class: java.math.BigDecimal

1 个答案:

答案 0 :(得分:0)

使用recordSet.getBigDecimal(index).doubleValue()或直接使用recordSet.getDouble(index)

阅读记录集