Solved

parse something where its blank...

Posted on 2003-11-28
17
249 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
[X]
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
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The recent Petya-like ransomware attack served a big blow to hundreds of banks, corporations and government offices The Acronis blog takes a closer look at this damaging worm to see what’s behind it – and offers up tips on how you can safeguard your…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

615 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