Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2008-11-17
4
Medium Priority
?
7,649 Views
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 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

0
Comment
Question by:makeshkumar_r
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 1000 total points
ID: 22978273
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
0
 

Author Comment

by:makeshkumar_r
ID: 22978365
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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 22978443
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

0
 

Expert Comment

by:jlkuka
ID: 37481281
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
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month13 days, 7 hours left to enroll

581 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