【问题标题】:Oracle: Integer field returning NUMBEROracle:返回 NUMBER 的整数字段
【发布时间】:2013-05-26 05:01:00
【问题描述】:

我正在 oracle 中创建下表:

CREATE TABLE TEST
(
 DKEY INTEGER NOT NULL PRIMARY KEY,
 NOM VARCHAR2(255) NOT NULL
)

当我尝试使用此 java 代码获取 DKEY 字段的类型时,我得到以下结果:

DatabaseMetaData md = cnx.getMetaData();
ResultSet rsmd = md.getColumns(null, strSchema, strTableName, "%");
while (rsmd != null && rsmd.next()) {
    String strTmp = rsmd.getString("SQL_DATA_TYPE");  // this return 0 for field DKEY
    int intDType = rsmd.getInt("DATA_TYPE");          // this return 3 for field DKEY
    String strFType = rsmd.getString("TYPE_NAME")     // this return NUMBER for field DKEY
}

为什么我得到的是 NUMBER 而不是 INTEGER?

【问题讨论】:

  • 您是否阅读了 Oracle 手册中描述可用数据类型的章节?因为那确实回答了你的问题。

标签: java oracle sqldatatypes


【解决方案1】:

虽然您可以在 SQL 语句中指定 INTEGER 作为数据类型,但 Oracle 在其实现中将使用 NUMBER(38)

请参阅 Oracle 数据类型 herethis 文章。

【讨论】:

  • 感谢您的回答,还有一个问题:为什么 rsmd.getString("COLUMN_SIZE") 返回 0 而不是 38?是否有另一种方法来获取尺寸?我试过 getPrecision 但它也返回 0
  • 如果您阅读 getPrecision 的 javadoc,docs.oracle.com/javase/6/docs/api/java/sql/…,它说 对于列大小不适用的数据类型返回 0。我不知道还有什么办法。使用 SQL 通过 DESCRIBE 语句或 Oracle 使用的任何方式获取表元数据。
  • 如果您指定整数、数字或数字(*,n),其中 n >= 0,getPrecision 将返回 0。sqlfiddle.com/#!4/be559/2
【解决方案2】:

我认为表列的 DKEY 属性类型是 NUMBER

但是试试

while (rsmd != null && rsmd.next()) {
    String strTmp = rsmd.getString("SQL_DATA_TYPE");  // this return 0 for field DKEY
    int intDType = Integer.parseInt(rsmd.getLong("DATA_TYPE"));          // this return 3 for field DKEY
    String strFType = rsmd.getString("TYPE_NAME")     // this return NUMBER for field DKEY
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-11
    • 2015-09-08
    • 1970-01-01
    • 2018-01-17
    • 2023-02-25
    • 1970-01-01
    • 2019-01-29
    • 1970-01-01
    相关资源
    最近更新 更多