parse something where its blank...

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??
xman19Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jimmackCommented:
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
jimmackCommented:
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
kennethxuCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

jimmackCommented:
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
objectsCommented:
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
xman19Author Commented:
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
objectsCommented:
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
kennethxuCommented:
           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
objectsCommented:
Doesn't setDate(2, null) have the same effect?
0
kennethxuCommented:
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
objectsCommented:
> 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
kennethxuCommented:
>> 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
objectsCommented:
> 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
kennethxuCommented:
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
objectsCommented:
thx :)
0
xman19Author Commented:
Thank you  ken and object.

:-)
0
objectsCommented:
No worries :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.