Automatic conversion problem with JDBC

Hello All,

I'm returning a resultset from a db call using:

<code>String value = rs.getString(columnName);<code>

What I'm seeing are values like:

.00031565 and .0003629975 coming back in scientific notation. as in 3.1565E-4 for example. Is this a jdbc thing to automatically convert values being returned like this? What is the best way to resolve this? Keep in mind also that I won't know what the datatype is I'm returning until runtime, so I'm thinking I have to use ResultSetMetaData to query what the column datatype is and then use the correct rs.get(dataType method), but I'm guessing. Also, we are using Sybase 5.2 and jdbc 2.

Any help would be appreciated!

Thanks,

James

[746 byte] By [jamesEstona] at [2007-11-27 9:27:33]
# 1
If you retrieve a numeric type as a String you can't be too surprised if it converts between the two types.(edit). Yes, if you don't want automatic conversion you'll have to identify the type and convert it yourself. But why don't you know the datatype?
dcmintera at 2007-7-12 22:29:41 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2
The program is an adhoc query and they can make multiple selections for data tables that we can't predict ahead of time, but yes it appears to be as simple as determining the correct datatype at runtime and then using the appropriate get method. I'll try it again today.
jamesEstona at 2007-7-12 22:29:41 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3
You can also use ResultSet#getObject() and eventually determine the instantiated class from Object#getClass() and then cast it back. Generics can be very useful.
BalusCa at 2007-7-12 22:29:41 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4
I wouldn't recommend that. And what has it got to do with generics?
dcmintera at 2007-7-12 22:29:41 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5
Why not? Would you rather retrieve the ColumnClassName (or even the ColumnTypeName) from the ResultSetMetaData and then get the result based on that?
BalusCa at 2007-7-12 22:29:41 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 6
Yep. Because null doesn't have an object type, and because you may not want the default type conversion (JDBC types are not equivalent to Java types, so while the conversion will be sufficient it may not be desirable).
dcmintera at 2007-7-12 22:29:41 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 7

I got to the root of the problem. I hadn't ever worked with numbers of precision of 4 or more to the right of the decimal. Even if I use <code> rs.getDouble();</code> it still converts them by default to scientific notation. The solution is use the DecimalFormat api and I got that to work.

Thanks,

James

jamesEstona at 2007-7-12 22:29:41 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...