0

I'm a beginner in Server-side programming in java, and I wrote a sample program that fetches the records from a database table and prints them on the console. So, I created a table in the database with 3 columns of types INT, VARCHAR and FLOAT, and entered some data in it. In the JDBC code, the first two types of data have been fetched successfully, which are getInt(col) and getString(col), but the getFloat(col) is not returning anything, even though there is data present in the table. I've double-checked the types in the database but it was of no help.

If I try to write getFloat(col) method individually (i.e. not in a loop) it works perfectly fine.

while(rs.next()) { //"rs" is the ResultSet object
  for(int i=1; i<=noc; i++) {  // "noc" is rs.getColumnCount()
    switch(rsmd.getColumnType(i)) { // "rsmd" is the ResultSetMetaData object
    case Types.VARCHAR :
        System.out.print(rs.getString(i));
        break;
    case Types.INTEGER :
        System.out.print(rs.getInt(i));
        break;
    case Types.FLOAT :
            System.out.print(rs.getFloat(i));
        break;
    }
    System.out.println();
    }
}

/*
If I write like below, it works fine

rs.next();
System.out.println(rs.getFloat(3));

but it doesn't work as in the above code
*/

I expect the output to be a full table of records, but what I get is a table with missing "FLOAT" type column data. I don't understand what wrong I was doing.

The output I was getting on the console

Community
  • 1
  • 1
  • and you are sure that the column is `FLOAT`, or more precisely, `getColumnType` is really returning `FLOAT`? and not maybe `NUMERIC` or so... anyway it is not bad to add a `default` to the switch with at least an error message, logging or throwing exception... – user85421 Jul 26 '19 at 16:58
  • Yes, I am sure because I created the table with INT, VARCHAR and FLOAT columns. – Harsha Bharadwaz Jul 26 '19 at 17:02
  • I did NOT ask how you created the table, I asked for the return value of `getColumnType` - the driver may be mapping databse FLOAT o some different Types ("* `getColumnType` is really returning FLOAT*" (there should be no reason for `getFloat(3)`to work and `getFloat(i)`, assuming `i==3` not to) – user85421 Jul 26 '19 at 17:02
  • 1
    @HarshaBharadwaz, prove it to yourself. Add a `println` to see what the column type is. For example, in Oracle (I know it's not your DB) a table created with a column of `INTEGER` is really just a special case of `NUMBER`, and will appear in the data dictionary as `NUMBER`. If it yours turns out to be some flavor of `NUMBER`, note that some numeric types end up being handled as `BigDecimal` in Java. – Tad Harrison Jul 26 '19 at 17:04
  • I'm sorry but how do I check that?. I'm a beginner, please let me know. – Harsha Bharadwaz Jul 26 '19 at 17:05
  • add a default to the switch that prints the value returned: `default: System.err.println("unexpected type: " + rsmd.getColumnType(i)); break;` or `default: throw new SQLException("unexpected type: " + rsmd.getColumnType(i)); ` – user85421 Jul 26 '19 at 17:06
  • Once you get the value of the column type, you can find which of the `Types` values it matches here: https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.sql.Types.BIT – Tad Harrison Jul 26 '19 at 17:08
  • 1
    Yeah, I got it now, It was actually a REAL type. Thank you. – Harsha Bharadwaz Jul 26 '19 at 17:21
  • @HarshaBharadwaz So exactly what I said in [my answer](https://stackoverflow.com/a/57224379/5221149). You should click the check mark next to my answer to *accept* it, so others can see that the question has been answered to your satisfaction. – Andreas Jul 26 '19 at 19:23

1 Answers1

4

but the getFloat(col) is not returning anything

It can't return nothing. Even if it returned a null value, you'd get that printed. If you get nothing, it's because the print statement isn't executing.

Most likely cause of that, is that getColumnType() returns REAL (or DOUBLE), not FLOAT. Try adding those to the switch statement to see.

Also add a default: to the switch, to at least get something printed, e.g. add this to the end of the switch statement:

default:
    System.out.print("Unexpected type: " + rsmd.getColumnType(i));

This would be a great time for you to learn how to debug your own code. With debugging, you'd have learned all this, a lot faster than it took you to write the question here. See:

Andreas
  • 154,647
  • 11
  • 152
  • 247
  • `getFloat` would return `0` for a `null` value. – Mark Rotteveel Jul 27 '19 at 06:53
  • @MarkRotteveel True. I was more trying to say in general that even if a method returned *nothing* (aka `null`), you'd get the text `null` printed, not *nothing*. Only way for *nothing* to be printed is if method returned an empty (or blank) string. – Andreas Jul 27 '19 at 17:52