954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

RecordSets, RecordSetMetaData, the Float Data Type and MySQL

Hello,

I have this piece of code.

private Vector getColumn(ResultSet rs)
	        throws SQLException
	        {
	            Vector row = null; 
	            ResultSetMetaData rsmd = null;
	            boolean moreResults = rs.next(); 
	            if (moreResults)
	            {
	            	row = new Vector();
	            	rsmd = rs.getMetaData();
	                do
	                {
	                	switch(rsmd.getColumnType(1))
	                	{
	                	case Types.VARCHAR:
	                		row.addElement(rs.getString(1));
	                		System.out.println(rs.getString(1));
	                		break;

	                	case Types.INTEGER:
	                		row.addElement(new Integer(rs.getInt(1)));
	                		System.out.println(rs.getString(1));
	                		break;

	                	
	                	case Types.FLOAT:
	                	row.addElement(new Float(rs.getFloat(1)));
	                		System.out.println(rs.getString(1));
	                		break;
	                		
	                	default:
	                		System.out.println("Column Type: " + rsmd.getColumnTypeName(1));
                	}
                }while(rs.next());
            }
            return row;
	        }


However, it will not catch the Float data type. I tried it with double, etc, but it will not work. The output that I get is the following:

select iID from items
101
102
103
104
select description from items
Watch
T-Shirt Collection
Heavy Duty Tea Cup
Shirts
select highestBid from items
Column Type: FLOAT
Column Type: FLOAT
Column Type: FLOAT
Column Type: FLOAT
Size of descArray: 4

Any suggestions on how to get the code to read data type Float?

curtissumpter
Light Poster
25 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

Well I do not exactly whats wrong since you have not given us the table schema, but have you tried just printing the value returned by rsmd.getColumnType(1) .

Although just for the record I do not see why it should be different from one given by rsmd.getColumnTypeName(1)

stephen84s
Nearly a Posting Virtuoso
1,443 posts since Jul 2007
Reputation Points: 668
Solved Threads: 154
 

Debug the code to find the actual type number returned and what it equates to. If you don't know how to use the debugger then print out the varying types at the top of the method as follows

System.out.println("Integer TYPE:  " + Types.INTEGER);
  System.out.println("Float     TYPE:  " + Types.FLOAT);
  ...


Then, just before the switch do

System.out.println(rsmd.getColumnType(1));


And see what that equates to.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You