5

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?

1 Answer 1

10

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I want to map "DATA TYPE" appear in "ORACEL" to exact "DATA TYPE" in "JAVA" soure code for instance "NUMBER(13,2)" to "FLOAT" by distinguishing in source code.
Your sentiments do not work for JPA/Hibernate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.