Solved

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

Posted on 2004-08-03
25
814 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
Comment Utility
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
Comment Utility
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
Comment Utility
>>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
Comment Utility
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
Comment Utility
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
Comment Utility
>>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
Comment Utility
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
Comment Utility
>> 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
Comment Utility
Yes, OK - looks like objects' last one was most apt
0
 

Author Comment

by:nikrulz
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 92

Expert Comment

by:objects
Comment Utility
whats the complete definition for the column?
0
 

Author Comment

by:nikrulz
Comment Utility
The column is of type NUMBER.
0
 
LVL 92

Expert Comment

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

Author Comment

by:nikrulz
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Store nulls actually as nulls in the database.
0
 

Author Comment

by:nikrulz
Comment Utility
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
Comment Utility
>> 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
Comment Utility
> 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
Comment Utility
Think my last comment was a clear answer
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

771 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

11 Experts available now in Live!

Get 1:1 Help Now