Solved

Differentiating between BigDecimal(Types.NULL) and BigDecimal(0)

Posted on 2004-08-03
25
822 Views
Last Modified: 2008-03-17
Hi,
My requirements are such that before I insert some data into the DB I need to check if it is null or not. Depending on the type of null, I will need to insert a different value in the DB. Now if is there anyway by which I can declare a BigDecimal(Types.NULL) to some other value such that it can be distinguished easily from any other normal BigDecimal. Types.NULL is int 0 so essentially all I am doing is BigDecimal(0). Is there any other value that can be instantiated with a BigDecimal so that it canbe distinguised easily as a BigDecimal different from any other instantiated BigDecimal.

Thanks in Advance
Nik
0
Comment
Question by:nikrulz
  • 8
  • 5
  • 4
  • +2
25 Comments
 
LVL 92

Expert Comment

by:objects
ID: 11701661
why do you need to instantiate one at all to represent null, why not just leave the variable null.
0
 
LVL 35

Expert Comment

by:girionis
ID: 11701677
I am not sure I understand what you mean by saying "normal" BigDecimal.

Also, this one:

>  Depending on the type of null

you cannot distinguish between null, since a null is null, it does not have a class.

Can you also be a bit more specific about this:

>  Now if is there anyway by which I can declare a BigDecimal(Types.NULL)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11701909
>>Types.NULL is int 0 so essentially all I am doing is BigDecimal(0).

That's not actually the case. NULL is quite distinct from 0 or any other value that is not NULL. On a write, NULL is only equivalent to NULL and therefore cannot be represented by anything else
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11701921
Are you talking about this:

            BigDecimal yourVariable = new BigDecimal(0);

            ...
            if ( yourVariable == null )
                 preparedStatement.setNull(index++, Types.INTEGER);         // Sets the column to NULL
            else
                 preparedStatement.setBigDecimal(index++, yourVariable);  // Sets the column to 0
0
 
LVL 92

Expert Comment

by:objects
ID: 11701980
Specifically why is it you need to distingish between null value.
just use null, eg.

BigDEcimal nullvalue = null;
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11701996
>>preparedStatement.setNull(index++, Types.INTEGER);         // Sets the column to NULL

that should be

preparedStatement.setNull(index++, Types.NULL);         // Sets the column to NULL
0
 
LVL 92

Expert Comment

by:objects
ID: 11702055
actually think it should be:

ps.setNull(index++, Types.NUMERIC);  
or
ps.setNull(index++, Types.BIGINT);        

though thats probably not even necessary.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11702337
>> that should be
>> preparedStatement.setNull(index++, Types.NULL);
No it isn't. The 2nd parameter should correspond with the table's column type.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11702368
Yes, OK - looks like objects' last one was most apt
0
 

Author Comment

by:nikrulz
ID: 11852985
The actual problem here is that I have a method which does the job of preparing the statement.
Now due to some reasons if a null is present while preparing the statement, it is inserted into the DB as a 0.
And I felt the need to send in a BigDecimal instantiating it with such a value that can be recognized distinctly.
So I needed some method of declaring the BigDecimal in such a way that can be recognized apart from any other BigDecimal.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 92

Expert Comment

by:objects
ID: 11856907
whats the complete definition for the column?
0
 

Author Comment

by:nikrulz
ID: 11890704
The column is of type NUMBER.
0
 
LVL 92

Expert Comment

by:objects
ID: 11898211
then why can't you store null, does the column allow nulls?
0
 

Author Comment

by:nikrulz
ID: 11910316
Yeah the column allows nulls. And I want to store a NULL. However as I had said earlier because of a business need, initially all nulls were converted to 0s. And since I don't want a 0 to be inserted into the DB, I used to sent new BigDecimal(TYPES.NULL), which incidently also enters a 0 into the DB. Therefore I wanted to know if there was a way out of this predicament.
0
 
LVL 92

Expert Comment

by:objects
ID: 11910362
Well currently it appears that you are treating 0 as null, so you could simply do that.
If you don't want 0 interpreted as null then isn't the data in your database already corrupt as it's full of 0's that you don't know whether they are null's or not.
0
 

Author Comment

by:nikrulz
ID: 12081456
Yes. You are right...indeed I want to bypass this problem. Is there any way I can avoid it?
0
 
LVL 92

Expert Comment

by:objects
ID: 12081477
Store nulls actually as nulls in the database.
0
 

Author Comment

by:nikrulz
ID: 12188652
I can store nulls in the database. But as I said, while storing values in the database nulls are interpreted as zeros because of some requirements. So I was hoping there is some way of differentiating a BigDecimal object which holds a NULL from one that has a value.
0
 
LVL 37

Accepted Solution

by:
zzynx earned 125 total points
ID: 12188860
>> Now due to ***some reasons*** if a null is present while preparing the statement, it is inserted into the DB as a 0
>> while storing values in the database nulls are interpreted as zeros because of ***some requirements***
I wonder why you can't change that requirements/reasons...

>> So I needed some method of declaring the BigDecimal in such a way that can be recognized apart
>> from any other BigDecimal
Since any BigDecimal that differs from null can contain a valid number, you can't.

So that's really the answer to your original question:
>> Is there any other value that can be instantiated with a BigDecimal so that it can be distinguised easily
>> as a BigDecimal different from any other instantiated BigDecimal.
NO!
0
 
LVL 92

Expert Comment

by:objects
ID: 12194660
> while storing values in the database nulls are interpreted as zeros because of some requirements.

then you will be unable to distinguish between null or zero until you get that requirement changed.
even then you still won't be able to distinguish any existing data in the table.

so looks like you're stuck treating zero and null as the same.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12408412
Think my last comment was a clear answer
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now