Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

DatabaseMetaData and ResultSetMetaData return different SQL Type codes for Number

Johannes DreverMar 3 2015 — edited Mar 6 2015

Hello,

I was testing some code of a colleague who used ResultSetMetaData which was retrieved with Statement.getMetaData() from a select statement. ResultSetMetaData.getColumnType(i) returned 2 (Numeric) for the SQL type Number. My test code used DatabaseMetaData retrieved from Connection.getMetaData(). DatabaseMetaData.getColumns returned 3 (Decimal) for SQL type Number.

Here is the a code snippet to illustrate the issue:

         // The database should contain a table MY_TABLE with a column of type 'Number'

        String user = "user";

        String password = "password";

        String host =  "host"

        String port = "port";

        String sid = "sid";


        DriverManager.registerDriver(new oracle.jdbc.OracleDriver());

        Connection connection =

                DriverManager.getConnection("jdbc:oracle:thin:@" + host + ":" + port + ":" + sid, user, password);

        // Get SQL type with DatabaseMetaData

        DatabaseMetaData metaData = connection.getMetaData();

        ResultSet tables = metaData.getTables(null, user, null, new String[] { "TABLE" });

        // MY_Table should be the first table

        if (tables.next()) {

            String tableName = tables.getString(3);

            log.info(tableName);

            ResultSet columns = metaData.getColumns(null, user, tableName, null);

            while (columns.next()) {

                String columnName = columns.getString(4);

                String columnType = columns.getString(5);

                System.out.println(columnName + " " + columnType);

               // Column type 3

            }

            columns.close();

        }

        tables.close();

         // Get SQL type with ResultSetMetaData

        Statement statement = connection.createStatement();

        String sql = "SELECT * FROM MY_TABLE";

        statement.execute(sql);

        ResultSet resultSet = statement.getResultSet();

        ResultSetMetaData metaData2 = resultSet.getMetaData();

        for (int i = 1; i <= metaData2.getColumnCount(); i++) {

            System.out.println(metaData2.getColumnName(i) + " " + metaData2.getColumnType(i));

            // Column type is 2

        }

Is there a reason for the discrepancy?

Regards,

Johannes

This post has been answered by Joe Weinstein-Oracle on Mar 6 2015
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 3 2015
Added on Mar 3 2015
7 comments
1,876 views