Link to home
Start Free TrialLog in
Avatar of makeshkumar_r
makeshkumar_r

asked on

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

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 8.1.7.1 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();
			ds.setDriverType("thin");
			ds.setServerName("serverName");
			ds.setPortNumber(1561);
			ds.setDatabaseName("dbName");
			ds.setUser("userName");
			ds.setPassword("password");
          
         
			Connection conn = ds.getConnection();
 
			PreparedStatement stmt = conn.prepareStatement(selectQuery);
           
          
			ResultSet rset = stmt.executeQuery();
			metaData = rset.getMetaData();
			if(rset.next()) {
			   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);
					   if(!rset.wasNull())
					   {
						   columnValue  = Long.toString(templong);
					   }
				   }
				   else if(columnType == Types.DECIMAL || columnType == Types.DOUBLE || columnType == Types.FLOAT || columnType == Types.REAL)
				   {
					   double tempDouble1 = rset.getDouble(columnName);
					   if(!rset.wasNull())
					   {
						   columnValue  = Double.toString(tempDouble1);
					   }
				   }
				   else if(columnType == Types.TIME || columnType == Types.TIMESTAMP || columnType == Types.DATE)
				   {
					   Timestamp sqlTimeStamp = rset.getTimestamp(columnName);
					   if(!rset.wasNull())
					   {
						   columnValue = sqlTimeStamp.toString();
					   }
				   }
				   System.out.println("Column Name: "+columnName+ " Column Type: "+columnType + "  Column Value: "+columnValue + " "+metaData.getColumnClassName(columnCount));
			   }
			   
			   }
			rset.close();
			stmt.close();
			conn.close();
  }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of makeshkumar_r
makeshkumar_r

ASKER

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

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