This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes JDBC and Relational Databases and the fly likes null value for int Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "null value for int" Watch "null value for int" New topic

null value for int

Pavan Kumar

Joined: Jan 13, 2002
Posts: 8
if to send a null value while binding the variables to callble statement.
suppose in following statement contractseq is int. Then how to send this value as null.We can use setNull method to set it as null. But I need to initialse contractseq as null, how to do this? INTEGER can set to null,but how to set int as null.

pavan kuamr
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
Since a Java "int" can't ever be null you have a couple of choices. Either you make a convention that a null will be represented by an unlikely value (such as Integer.MIN_VALUE), or you use an "Integer" instead.
In the first case, if you want to set an "int" to null, you set contractseq = Integer.MIN_VALUE. Inside the receiving method you check for a value of "Integer.MIN_VALUE" and if found do a "setNull()", otherwise do a "setInt()". This works, but it is sorta' ugly, and would prevent you from ever storing Integer.MIN_VALUE in the database.
The second option means you have to wrap/unwrap the "int" value, but allows you to pass a null around.
I agree. Here's the link:
subject: null value for int
It's not a secret anymore!