I would like to insert some unsigned int value using a prepared statement.
setInt() slaps me with a DataTruncation if the second parameter is above 2,147,483,647 (Max Signed Int) although my underlying table can chew up to 4,294,967,295 (Max Unsigned Int)
Is there such a thing as setUnsignedInt()? Should I use setLong() although the table is setup as (INT(10) UNSIGNED)?
Thanks
Asked
Active
Viewed 1,577 times
2

MonoThreaded
- 11,429
- 12
- 71
- 102
3 Answers
4
Yes, you should use setLong()
. You'll note that the javadocs tell you:
Sets the designated parameter to the given Java long value. The driver converts this to an SQL BIGINT value when it sends it to the database.
So be aware that it would allow you to set a value larger than 2^32-1 which would then be rejected by the DB (hopefully; I suppose it could wrap).

Brian Roach
- 76,169
- 12
- 136
- 161
3
To do this you use the next smallest data type that will hold the current max (unsigned) value. For Integer that would be Long. You can then discard anything above the max (unsigned) value for Integer.
2
Typically when you need an unsigned primitive's headroom in Java you just go to the next tier up in size. In your case, that's a long.

Raevik
- 1,945
- 9
- 32
- 53