• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 861
  • Last Modified:

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

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
nikrulz
Asked:
nikrulz
  • 8
  • 5
  • 4
  • +2
1 Solution
 
objectsCommented:
why do you need to instantiate one at all to represent null, why not just leave the variable null.
0
 
girionisCommented:
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
 
CEHJCommented:
>>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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
zzynxSoftware engineerCommented:
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
 
objectsCommented:
Specifically why is it you need to distingish between null value.
just use null, eg.

BigDEcimal nullvalue = null;
0
 
CEHJCommented:
>>preparedStatement.setNull(index++, Types.INTEGER);         // Sets the column to NULL

that should be

preparedStatement.setNull(index++, Types.NULL);         // Sets the column to NULL
0
 
objectsCommented:
actually think it should be:

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

though thats probably not even necessary.
0
 
zzynxSoftware engineerCommented:
>> that should be
>> preparedStatement.setNull(index++, Types.NULL);
No it isn't. The 2nd parameter should correspond with the table's column type.
0
 
CEHJCommented:
Yes, OK - looks like objects' last one was most apt
0
 
nikrulzAuthor Commented:
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
 
objectsCommented:
whats the complete definition for the column?
0
 
nikrulzAuthor Commented:
The column is of type NUMBER.
0
 
objectsCommented:
then why can't you store null, does the column allow nulls?
0
 
nikrulzAuthor Commented:
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
 
objectsCommented:
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
 
nikrulzAuthor Commented:
Yes. You are right...indeed I want to bypass this problem. Is there any way I can avoid it?
0
 
objectsCommented:
Store nulls actually as nulls in the database.
0
 
nikrulzAuthor Commented:
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
 
zzynxSoftware engineerCommented:
>> 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
 
objectsCommented:
> 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
 
zzynxSoftware engineerCommented:
Think my last comment was a clear answer
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 8
  • 5
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now