如何将oracle dataType 映射到java dataType?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21061890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:18:18  来源:igfitidea点击:

how can mapping oracle dataType to java dataType?

javaoraclejdbctype-conversion

提问by user3184842

how can mapping oracle dataType to java dataType?

如何将oracle dataType 映射到java dataType?

I tried as below but Not accurate .

我尝试如下但不准确。

 DatabaseMetaData databaseMetaData = this.getConnection().getMetaData(); 
 ResultSet rs = databaseMetaData.getColumns(catalog, schema, tableName, columnName);

 while (rs.next()) {
     if ( rs.getString("data_type").equals(Types.NUMERIC)){
           javaDataType = "java.math.BigDecimal";
     } }

for example : oracle => number(2,3) => javaDataType = BigDecimal; oracle => number(15) => javaDataType = BigDecimal;

例如:oracle => number(2,3) => javaDataType = BigDecimal; oracle => number(15) => javaDataType = BigDecimal;

Best way to convert oracle dataTypeto java dataType?

将 oracle 转换dataType为 java 的最佳方法dataType

回答by krokodilko

I am not quite sure what are you trying to do.
Oracle has default mappings between java clases and SQL types,
please take a look at this table: http://docs.oracle.com/cd/E11882_01/java.112/e16548/apxref.htm#JJDBC28906

For example there are valid conversions from a NUMBERtype to java types:

我不太确定你想做什么。
Oracle 有 java clases 和 SQL 类型之间的默认映射,
请查看此表:http: //docs.oracle.com/cd/E11882_01/java.112/e16548/apxref.htm#JJDBC28906

例如有有效的转换从NUMBER类型到 java 类型:

+---------------+--------------------+
| SQL data type | Java types         |
+---------------+--------------------+
|   NUMBER      |boolean             |
|               |char                |   
|               |byte                |
|               |short               |
|               |int                 |
|               |long                |
|               |float               |
|               |double              |
|               |java.lang.Byte      |
|               |java.lang.Short     |
|               |java.lang.Integer   |
|               |java.lang.Long      |
|               |java.lang.Float     |
|               |java.lang.Double    |
|               |java.math.BigDecimal|
|               |oracle.sql.NUMBER   |
+---------------+--------------------+

If we want to get a NUMBERas a specific java type, we need to use an appriopriate getXXXmethod from the ResultSetinterface, for example:

getIntretrieves the number as java int--> http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html#getInt%28int%29

getLongretrieves the number as java long --> http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html#getLong%28int%29

getDoubleretrieves the number as java long --> http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html#getDouble%28int%29

getBigDecimalretrieves the number as java BigDecimal --> http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html#getBigDecimal%28java.lang.String,%20int%29

This is your - the developer - responsibility to choose the proper method for your data.

如果我们想获取一个NUMBER特定的java类型,我们需要getXXXResultSet接口中使用一个合适的方法,例如:

getInt将数字检索为java int--> http://docs.oracle.com/javase/7/ docs/api/java/sql/ResultSet.html#getInt%28int%29

getLong将数字检索为 java long --> http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet .html#getLong%28int%29

getDouble将数字检索为 java long --> http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html#getDouble%28int%29

getBigDecimal将数字检索为 java BigDecimal -->http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html#getBigDecimal%28java.lang.String,%20int%29

这是您 - 开发人员 - 有责任选择正确的您的数据的方法。