Solved

Storing NULL values in DB

Posted on 2003-10-23
7
286 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maven project error 5 56
ForLoop Example 3 49
How to convert String matching to regex in java 4 34
tomcat startup error 5 66
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

822 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