0

With using JAVA Programming Language, I want to print to console, all Data types which are used in a table of ORACLE to their equivalent JAVA Data types; would you help me?

For example:

Create table Student (
    STD_ID number (8) primary Key,
    NAME varchar2 (15) not null,
    AGE number (2),
    AVERAGE number (2, 3)
);

After running program, the output will be similar below:

  1. Integer
  2. String
  3. Short
  4. Double

I reach oracle data type via the following code, but to converting Number data type to Integer, Double, Short and other Numeric Data type; I have a problem.

resultset.getMetaData.getColumnType(columnCount);

0

3 Answers 3

1

Why output of numeric field, at table above, show java.math.BigDecimal?

As per Oracle documentation on Datatypes and their Java Mappings:

NUMBER:

The NUMBER class provides conversions between the Oracle Number (lnxnum_t) data type and Java types byte[], byte, short, integer, long, float, double, String, BigInteger, and BigDecimal.

Understanding Type Maps:

The type map relates a Java class to the SQL type name of an Oracle object. This is a one-to-one mapping that is stored in a hash table as a key-value pair. When you read data from an Oracle object, the JDBC driver considers the type map to determine which Java class to use to materialize the data from the SQL object type. When you write data to an Oracle object, the JDBC driver gets the SQL type name from the Java class by calling the getSQLTypeName() method of the SQLData interface. The actual conversion between SQL and Java is handled by the driver.

This is default behaviour and you can find it on the getObject methods underlying return type. For a data type of NUMBER the return type is java.math.BigDecimal.

+-----------------+------------------------+------------------------+
| Oracle SQL Type | getObject()            | getOracleObject()      |
|                 | Underlying Return Type | Underlying Return Type |
+-----------------+------------------------+------------------------+
| CHAR            | String                 | oracle.sql.CHAR        |
| VARCHAR2        | String                 | oracle.sql.CHAR        |
| LONG            | String                 | oracle.sql.CHAR        |
| NUMBER          | java.math.BigDecimal   | oracle.sql.NUMBER      |
| RAW             | byte[]                 | oracle.sql.RAW         |
| LONGRAW         | byte[]                 | oracle.sql.RAW         |
| DATE            | java.sql.Timestamp     | oracle.sql.DATE        |
+-----------------+------------------------+------------------------+

An example of understanding the Java return types on database specific data types are shown in an example on the Summary page referred below. Similar example was presented in my previous answer on this post.

Conclusion:
Return types are database driver specific. If you want to cast them for your convenience or for any other reason, you can make an explicit call, for example, like getDouble( columnNumber ) on NUMBER data type columns. You may require to handle precision and scale parts, of the returned number value, explicitly.

Refer to:

  1. Oracle's Built-in Data Types
  2. Summary of getObject() and getOracleObject() Return Types
  3. Valid SQL Datatype-Java Class Mappings
Sign up to request clarification or add additional context in comments.

Comments

0

Once you know you have a numeric column, you can guess the actual type with getPrecision and getScale:

  • if getScale() > 0: double
  • if getPrecision() > some value: short
  • if getPrecision() between some value and some other value: integer
  • if getPrecision() > some other value: long.

Comments

0

Detect data type java from Data Type oracle in run time.

I want to print to console, all Data types which are used in a table of ORACLE to their equivalent JAVA Data types

You can call getColumnClassName to get the JAVA qualified class match for the data type of any database table column.

Example:

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

Output Example: something like:

Column Name : t
Column Type : TIMESTAMP
Column Class: java.sql.Timestamp

Column Name : i
Column Type : INT
Column Class: java.lang.Integer

Column Name : d
Column Type : DATE
Column Class: java.sql.Date

Column Name : e
Column Type : CHAR
Column Class: java.lang.String

Column Name : s
Column Type : VARCHAR
Column Class: java.lang.String

Refer to:

5 Comments

please see answer 3 carefully .
Why output of numeric field, at table above, show java.math.BigDecimal? You should have added this to your posting rather than adding in an answer. That answer looks like a question but not answer. Better add it to your question.
@user3184842: I have added another answer on this new question here. Please check.
dear @user3184842, would you please add link to your answer?
@user3184842: It is on this post, a second answer from me.

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.