Solved

parse something where its blank...

Posted on 2003-11-28
17
241 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Large Outlook files lead to various unwanted errors and corruption issues. Furthermore, large outlook files can also make Outlook take longer to start-up, search, navigate, and shut-down. So, In this article, i will discuss a method to make your Out…
Note: This is the second blog post in a series on email clearinghouses (https://www.xmatters.com/alert-management/blog-email-has-failed-us?utm_campaign=70138000000ydLoAAI&utm_source=exex&utm_medium=article&utm_content=blog-post).   Every month t…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

863 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

25 Experts available now in Live!

Get 1:1 Help Now