Solved

Storing NULL values in DB

Posted on 2003-10-23
7
265 Views
Last Modified: 2010-03-31
Hi
I have a date field that might or might not contain data. What do you suggest I store it as
I store it as NULL riht now, but it is causing some problems such as nullPointer Exceptions.

My questions is -> what do experts do?
                      1. store null and then do a try catch whenever we access it
                  or 2. use a dummy date as 1901-01-01 as date and then change it during display as "" <empty string>

Thank you!
                                                               
0
Comment
Question by:applekanna
  • 4
  • 3
7 Comments
 
LVL 9

Expert Comment

by:yongsing
Comment Utility
Use option 1.

When you access it, you test it for null:

ResultSet resultSet = statement.executeQuery("select datecol from yourtable");
while (resultSet.next()) {
    Date date = resultSet.getDate(1);

    // test to see if date is null
    if (date != null) {
        // do something with the date
    }

}
resultSet.close();
0
 
LVL 3

Author Comment

by:applekanna
Comment Utility
I got this problem becoz it was like this

ResultSet resultSet = statement.executeQuery("select datecol from yourtable");

    java.util.Date date = resultSet.getDate(1);//this returns java.sql.Data

resultSet.close();

I added a private method to later conver sql date to util date and added a try catch there.
Looking for your expert opinion
0
 
LVL 9

Accepted Solution

by:
yongsing earned 25 total points
Comment Utility
You don't have to convert java.sql.Date to java.util.Date.

java.sql.Date is a java.util.Date (it's a subclass of the latter).

Not sure what other problem you have.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 3

Author Comment

by:applekanna
Comment Utility
oops..
I tried this when it gave me an error
PreparedStatement pstmt
 
pstmt.setDate(utildate);
0
 
LVL 3

Author Comment

by:applekanna
Comment Utility
I should have type casted it.

pstmt.setDate(11, (java.sql.Date)utilDate);
0
 
LVL 9

Expert Comment

by:yongsing
Comment Utility
You need to convert java.util.Date to java.sql.Date as follows:

pstmt.setDate(11, new java.sql.Date(utilDate.getTime()));
0
 
LVL 3

Author Comment

by:applekanna
Comment Utility
Thank you
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

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.

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

15 Experts available now in Live!

Get 1:1 Help Now