?
Solved

Storing NULL values in DB

Posted on 2003-10-23
7
Medium Priority
?
313 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
[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
  • 4
  • 3
7 Comments
 
LVL 9

Expert Comment

by:yongsing
ID: 9604870
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
ID: 9604947
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 100 total points
ID: 9604955
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
Independent Software Vendors: 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 3

Author Comment

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

Author Comment

by:applekanna
ID: 9605012
I should have type casted it.

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

Expert Comment

by:yongsing
ID: 9605029
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
ID: 9605034
Thank you
0

Featured Post

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.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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 …
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:
Suggested Courses

649 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