getColumnType of ResultSetMetaData returns NUMERIC type for all integer, double types.

Posted on 2008-11-17
Last Modified: 2013-12-29
My application is running fine with Java 1.5, Oracle JDBC driver 10i with Oracle 10i database.  In this application, there is a part of code which uses getColumnType of ResultSetMetaData function to find out the column type of the particular column. Based on the column type value, that column value is retrieved.
For example If the return value is "Double", resultSet.getDouble(columnName) method is used to retrieve. If it is "Long", then resultSet.getLong(columnName) is used.

Now I want to use the same code to access the database Oracle 8i. When I used the application as it is to access the 8i database, the getColumnType method returned "Numeric" instead of either Double/ Long.
I thought the problem was with JDBC driver jar file. so I replaced Oracle 10i jdbc driver with Oracle driver. But Still I had the same problem. Now I tried replacing Java 1.5 with Java 1.4 too. But still that return type was "Numeric". no luck. Is it the problem with Oracle 8i database?

I almost tried all the combination of Java 1.4/ Java 1.5, Oracle jdbc driver 10i/8i and Oracle database 10i / 8i.  No luck. Please help me to solve this problem.

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.ResultSetMetaData;

import java.sql.SQLException;

import java.sql.Types;

import java.util.Date;

import oracle.jdbc.pool.OracleDataSource;

public class DBAccessPool 



     public static void main(String args[]) throws ClassNotFoundException, SQLException 


			ResultSetMetaData metaData = null;

			String columnName = null;

			int columnType;

			String columnValue = null;

			OracleDataSource ds = new OracleDataSource();









			Connection conn = ds.getConnection();

			PreparedStatement stmt = conn.prepareStatement(selectQuery);



			ResultSet rset = stmt.executeQuery();

			metaData = rset.getMetaData();

			if( {

			   for(int columnCount = 1; columnCount <= metaData.getColumnCount();columnCount++)


				   columnValue = null;

				   columnName = metaData.getColumnName(columnCount);

				   columnType = metaData.getColumnType(columnCount);


				   if(columnType == Types.CHAR || columnType == Types.VARCHAR || columnType == Types.LONGVARCHAR)


					   columnValue = rset.getString(columnName);


				   else if(columnType == Types.INTEGER || columnType == Types.BIGINT || columnType == Types.SMALLINT || columnType == Types.NUMERIC)


					   long templong = rset.getLong(columnName);



						   columnValue  = Long.toString(templong);



				   else if(columnType == Types.DECIMAL || columnType == Types.DOUBLE || columnType == Types.FLOAT || columnType == Types.REAL)


					   double tempDouble1 = rset.getDouble(columnName);



						   columnValue  = Double.toString(tempDouble1);



				   else if(columnType == Types.TIME || columnType == Types.TIMESTAMP || columnType == Types.DATE)


					   Timestamp sqlTimeStamp = rset.getTimestamp(columnName);



						   columnValue = sqlTimeStamp.toString();



				   System.out.println("Column Name: "+columnName+ " Column Type: "+columnType + "  Column Value: "+columnValue + " "+metaData.getColumnClassName(columnCount));








Open in new window

Question by:makeshkumar_r
    LVL 86

    Accepted Solution

    What does it matter - your code is only turning it into a String anyway? You could probably call getString or at least getObject(n).toString

    Author Comment

    Thank you. That's a good catch. I was trying too hard to find out how to get the right value for getColumnType() method. I didn't think of other ways.
    LVL 86

    Expert Comment

    If you want to be more type-specific:

    case Types.NUMERIC:
    	Number n = (Number)rs.getObject(x);
    	float f = n.floatValue();
    	double d = n.doubleValue();
    	// And so on for all other numeric primitives

    Open in new window


    Expert Comment

    The solution is fake for me. The promisse is clariry way to determine the type of a column and this job is ot complete.

    really disappointed

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    Title # Comments Views Activity
    matchUp  challenge 9 48
    Increment alphanumeric sequence 6 52
    bunnyEars2 challenge 6 46
    ArrayLists in ArrayLists/HashMaps? 4 27
    An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
    I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
    This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now