Solved

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

Posted on 2004-08-03
25
835 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

707 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