Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java SQL prepared statement for unsigned int

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

like image 267
MonoThreaded Avatar asked Aug 25 '26 16:08

MonoThreaded


2 Answers

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).

like image 182
Brian Roach Avatar answered Aug 28 '26 07:08

Brian Roach


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.

This post will shed some light

like image 26
Woot4Moo Avatar answered Aug 28 '26 07:08

Woot4Moo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!