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
Solved

Storing NULL values in DB

Posted on 2003-10-23
7
293 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
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 25 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Weekend adv creator 3 52
difference of if loops 23 62
Is there a simpler dropbox system? 10 34
collection output issue 9 37
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)
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.

828 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