Updating Rows Programmatically (JDBC 2.0)

Hi,

I am trying to run the Coffee Sample from the JDBC tutorial, with a SQL Server.

All the code supplied by sun is working perfectly, but just updating the COFFEE table in the programmatically way it doesnt work perfectly.

The code I am using is simple:

(...)

ResultSet uprs = stmt.executeQuery("SELECT * FROM COFFEES");

uprs.updateFloat("PRICE", 10.99f);

uprs.updateRow();

It doesn`t throws any Exception but into the database appear the float -1.540744E-33 instead of 10,99. !!!

On the other hand if I try to update a String with:

(...)

uprs.updateString("COF_NAME", "Colombiano");

it`s working fine!!! Why it doesn`t accept the float ?

Can anyone help me?

Regards,

Nieves.

[782 byte] By [npena] at [2007-9-26 3:14:28]
# 1

In the example you are using from the sun documentation, you forget to position the cursor to a row before updating:

ResultSet uprs = stmt.executeQuery("SELECT * FROM COFFEES");

uprs.last();

uprs.updateFloat("PRICE", 10.99f);

uprs.updateRow();

it might have been a cut and paste error by you as this should throw an exception?

Jamie

jlrober at 2007-6-29 11:24:49 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

[npena],

There is a possibility that on the SQL server end, the PRICE column is set to a default format other than float, which may result in the database automatically converting the data from float. Check the database end and let us know your findings.

HTH.

Allen Lai

SUN Developer Technical Support

allenlai at 2007-6-29 11:24:49 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

Hi both,

Unfortunately, I had tried both posibilities yesterday before sending the question.

As I explained the same code using updateString() works !! which means I am positioned in the correct row, the problem appears with Floats an also with Ints types.

updateFloat()

updateInt()

I have checked in the SQL Server the type of Int and is the same as in java in fact the range is exactly the same... These methods doesnt return an Exception in fact a number is saved into the Database but this number is always the same trying to store a Float and the same trying to store an Int....

I think that could be a wrong implementation of these methods in the SQL Driver ?

Any other idea?

Thanks very much for your answers,

Nieves.

npena at 2007-6-29 11:24:49 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4

[npena],

Judging from what you mentioned about no exceptions thrown and that the data is definitely stored into the SQL db but not in the float or int format as expected, it looks like there is a high possibility that the JDBC driver for the SQL server is not correctly written.

Try finding another revision of the driver (older or newer) for this SQL server that you are using.

Sorry for not being able to assist you further in this posting.

Allen Lai

Developer Technical Support

SUN Microsystems

http://www.sun.com/developer/support/

allenlai at 2007-6-29 11:24:49 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...