Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to map oracle dataType to java dataType?

how can mapping oracle dataType to 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;

Best way to convert oracle dataType to java dataType?

like image 748
user3184842 Avatar asked Jan 11 '14 11:01

user3184842


1 Answers

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 NUMBER type to java types:

+---------------+--------------------+
| 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 NUMBER as a specific java type, we need to use an appriopriate getXXX method from the ResultSet interface, for example:

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

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

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

getBigDecimal retrieves 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.

like image 96
krokodilko Avatar answered Oct 26 '22 23:10

krokodilko