Bug #12244 ResultSet.getLong returns a incorrect value
Submitted: 28 Jul 2005 18:28 Modified: 29 Jul 2005 12:14
Reporter: Keita Kitamura Email Updates:
Status: Duplicate Impact on me:
None 
Category:Connector / J Severity:S2 (Serious)
Version:3.1.9, 3.1.10 OS:Any (any)
Assigned to: CPU Architecture:Any

[28 Jul 2005 18:28] Keita Kitamura
Description:
ResultSet.getLong() returns a wrong value with using INTEGER UNSIGNED column type.

How to repeat:
scheme for testcase;

create table test (
  value int(10) unsigned not null
);
insert into test values(100), (2147483648), (4294967295);

test code;

PreparedStatement p = conn.prepareStatement("select * from test");
ResultSet rs = p.executeQuery();

while (rs.next()) {
       System.out.println(rs.getRow() + "(getLong): " + rs.getLong(1));
       System.out.println(rs.getRow() + "(getInt): " + rs.getInt(1));
       System.out.println("---");
}

You will get following output;

1(getLong): 4294967396
1(getInt): 100
---
2(getLong): 2147483648
2(getInt): -2147483648
---
3(getLong): 4294967295
3(getInt): -1
---

As you see, 1(getLong) is wrong value. It is expected to be 100.
The other values row 2 and row 3 are no problem. works fine.

Suggested fix:
This problem only occurs when the value is less than or equals to max integer value(2147483647).
In com.mysql.jdbc.ResultSet at line 3780, there requires checking if the returned value from getNativeInt is less than or equals to max integer value or not.

3780: return getNativeInt(columnIndex + 1) + 4294967296L;

should be something like this,

int i = getNativeInt(columnIndex + 1);
return i <= 2147483647 ? i : i + 4294967296L
[29 Jul 2005 7:19] Vasily Kishkin
I was not able reproduce the bug:

1(getLong): 100
1(getInt): 100
---
2(getLong): -2147483648
2(getInt): -2147483648
---
3(getLong): -1
3(getInt): -1
---

I tested on 3.1.10. What version of mysqld did you use ?
[29 Jul 2005 9:17] Keita Kitamura
I'm using 4.1.10a installed by rpm on WBEL4.

By the way, your output was wrong too, right?
Since INTEGER UNSIGNED supports 0 to 4294967295, 2(getLong) is expected to be 2147483648 and 3(getLong) should be 4294967295, aren't they?
[29 Jul 2005 9:51] Vasily Kishkin
Ah...sorry...you are right.
[29 Jul 2005 12:14] Mark Matthews
Dupe of a bug already fixed for 3.1.11, see http://downloads.mysql.com/snapshots.php#connector-j for a nightly snapshot build with the fix.