Solved

parse something where its blank...

Posted on 2003-11-28
17
240 Views
Last Modified: 2010-04-01
i have this code,

  long todayDate = System.currentTimeMillis();
            SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
            try {
            getConnection();
            PreparedStatement pstmt=null;
            pstmt=conn.prepareStatement("insert into mytable values (?,?,?)");
            pstmt.setString(1, customer );
            pstmt.setDate(2, new java.sql.Date(df.parse(dob).getTime()) );
            pstmt.setDate(3, new java.sql.Date(df.parse(dop).getTime()) );
           
..
..
..
        public void setDob(String dob) {
            if (!dob.equals("") || !dob.equals(null)) {
                this.dob=dob;
            } else { dob="00/00/0000"; }
        }
        public void setDop(String dop) {
            this.dop=dop;
        }

This is because i allow sometime use can enter a blan date of birth intio the table. but afte ri have add the logic in the setDob(), the "unparse Exception" still prompt out and refuse to insert...

any idea how to sovle this??
0
Comment
Question by:xman19
  • 7
  • 5
  • 3
  • +1
17 Comments
 
LVL 15

Expert Comment

by:jimmack
ID: 9837943
I'm not sure what you mean by "unparse Exception".  If you mean that you are getting a NullPointerException, this is probably because your test is the wrong way round and the logic isn't quite right:

>>  if (!dob.equals("") || !dob.equals(null)) {

should be:

 if (dob != null && !dob.equals("")) {

Alternatively (for readability), it is better not to use lots of negatives in the test, you could rearrange this as follows:

if ((dob == null) || (dob.equals(""))
{
   dob = "00/00/00";
}
else
{
   this.dob = dob;
}

The important part of this is that the test for "null" needs to be done first.
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9837965
Just for information (if you're interested ;-))

Java "short-circuits" complex logical expressions.  This means that each expression is evaluated in turn and:

For an OR (||), when an expression is evaluated as true, the whole complex expression is deemed to be true

For an AND (&&), when an expression is evaluated as false, the whole complex expression is deemed to be false.

When either of these occurs, it means that subsequent expressions are not evaluated.  This is why you should always test for null first.
0
 
LVL 14

Accepted Solution

by:
kennethxu earned 125 total points
ID: 9838057
jimmack is right that the if condition should be corrected.

in addition, I don't think it is a good idea of saving "00/00/0000" to database. I'm not sure what endup in the database if you save such a date, but I'm sure it won't be "00/00/0000" when you try to read it back. I guess the read back date could be database dependent.

So I would suggest to simple save the null value to database:
if ((dob == null) || (dob.equals(""))
{
   pstmt.setNull( 2, java.sql.Types.DATE );
}
else
{
   pstmt.setDate(2, new java.sql.Date(df.parse(dob).getTime()) );
}
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9838071
Thanx kennethxu :-)  I was wondering about the 00/00/00 bit.  I don't use dates in databases too often, but I thought it looked a bit suspect :-)

Also, I'm sure that (as you say) it will be database dependent.
0
 
LVL 92

Expert Comment

by:objects
ID: 9840908
You could change your member variables to Date instances, then you could just use:

pstmt.setDate(2, dob);

And handle your parsing in your set method, which would allow you to handle invalid dates in advance of making the db insert. Also better to test length of trimmed string so you pick up blank as well as empty strings:

public void setDob(String dob) throws ParseException
{
   if (dob==null || dob.trim().length()==0)
   {
      // blank dob
      this.dob = null;
   }
   else
   {
      this.dob = new java.sql.Date(df.parse(dob).getTime());
   }
}
0
 

Author Comment

by:xman19
ID: 9841450
thank you guys.
jimmack, object thank you for your information. :-)

Ken, somethings need to confirm. I have amend the following code.

          PreparedStatement pstmt=null;
           pstmt=conn.prepareStatement("insert into mytable values (?,?,?)");
           pstmt.setString(1, customer );
           if ((dob == null) || (dob.equals(""))
          {
            pstmt.setNull( 2, java.sql.Types.DATE );
           } else {
            pstmt.setDate(2, new java.sql.Date(df.parse(dob).getTime()) );
           }
         //  pstmt.setDate(2, new java.sql.Date(df.parse(dob).getTime()) );
           pstmt.setDate(3, new java.sql.Date(df.parse(dop).getTime()) );

and my data entered (after i display it out) is "null"

so, i wonder, this java.Types.DATE is going what function for me?
thank you.
0
 
LVL 92

Expert Comment

by:objects
ID: 9841478
If you make your dob member var a Date instance I don't think you need to worry about using setNull(). You also seperate the parsing of the input from the database code.

0
 
LVL 14

Expert Comment

by:kennethxu
ID: 9841496
           pstmt.setNull( 2, java.sql.Types.DATE );
it just to provides a null value for a database column with Date datatype. java.sql.Types.DATE is just a constant that telling jdbc driver that you are dealing with date type. see:
http://java.sun.com/j2se/1.3/docs/api/java/sql/Types.html#DATE

0
IT, Stop Being Called Into Every Meeting

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 92

Expert Comment

by:objects
ID: 9841516
Doesn't setDate(2, null) have the same effect?
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 9843881
object, I'm answering xman19's question. is there anything wrong in my answer?
<xman19>so, i wonder, this java.Types.DATE is going what function for me?</xman19>
0
 
LVL 92

Expert Comment

by:objects
ID: 9843893
> is there anything wrong in my answer?

did i say there was? it was just a question.
My apologies if i inadvertedly offend u.
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 9844506
>> did i say there was?
It was the negative question made me think that way. Acutally, did I say setDate has different effect?
My apologies too if inadvertently offend u.
0
 
LVL 92

Expert Comment

by:objects
ID: 9844517
> It was the negative question

It wasn't intended as one. It was merely curiosity as to whether you knew if there was a difference as I was not sure.

> did I say setDate has different effect?

Not at all, thats what I was asking you :)

No offence taken :-)
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 9851081
No offence taken here too.
As to your question, as far as stated in specification, they should be the same. But practically, I did experience some jdbc driver throws exception on setDate(index, null). I always suggest setNull simply because I know for sure it will work.
0
 
LVL 92

Expert Comment

by:objects
ID: 9852385
thx :)
0
 

Author Comment

by:xman19
ID: 9854401
Thank you  ken and object.

:-)
0
 
LVL 92

Expert Comment

by:objects
ID: 9854485
No worries :)
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

In this article, I show you step by step with screenshots to assist you - HOW TO: Deploy and Install the VMware vCenter Server Appliance 6.5 (VCSA 6.5), with some helpful tips along the way.
Find out what the Office 365 disclaimer function is, why you would use it and its limited ability to create Office 365 signatures.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 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

12 Experts available now in Live!

Get 1:1 Help Now