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?

Recommended Answers

All 2 Replies

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)

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.