Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-08-03
25
Medium Priority
?
840 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn different types of Android Layout and some basics of an Android App.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

609 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