Solved

date problem...again..

Posted on 2003-12-12
15
1,540 Views
Last Modified: 2012-05-04
i am still confused. Hope my code and situation can be sovled.

****

//it's a string consist of dd/MM/yyyy date format
String ida= request.getParameter("InsDateA");

//Declare and initiate a date
java.util.Date eda = new java.util.Date();
..
..
Calendar f_a  = Calendar.getInstance();
..
..
..
    if (rs.next()) {
        a_term=rs.getString("FilterA");
..
..

if (ida!=null || ida!="") {
    System.out.println("Inside the null");
    f_a.setTime(df.parse(ida));
    f_a.add(Calendar.MONTH, Integer.parseInt(a_term));
    eda=f_a.getTime();
}

...
....
    if ((eda == null) || (eda.equals(""))){
            pstmt.setNull( 7, java.sql.Types.DATE );
        } else {
              pstmt.setDate(7, new java.sql.Date(eda));
        }

********
basically i accept user enter date (ida), from there i retrieve a terms (in month) from db. Then i add the ida with this terms, and get a date. Then with this date, i pump it into a Date field in my db.

the question is,
1. based on the above code, anything wrong with the ttype and the declaration?
2. I keep on getting this error;
javax.servlet.ServletException: Unparseable date: " "
      at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:471)

when i did enter the ida date. But i thought i have already check the date before i do anything.

  if (ida!=null || ida!="") {

  }

have can i sovle this error?? unparseable date ?
0
Comment
Question by:fks2
  • 5
  • 4
  • 3
  • +2
15 Comments
 
LVL 6

Expert Comment

by:jarasa
ID: 9927406
Hi could you please post you SQL STATEMENT??
I think there is could be the problem.

Javier
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 9927509
try this
String ida= request.getParameter("InsDateA");
if ((eda == null) || (eda.equals(""))){
            pstmt.setNull( 7, java.sql.Types.DATE );
        } else {
              SimpleDateFormat sdf = new SimpleDateFormat( "dd/MM/yyyy" ) ;
              pstmt.setDate(7, sdf.parse( ida ).getTime());
        }

let me know if it works
0
 

Author Comment

by:fks2
ID: 9927790
no no no...

kuldeep, nice to see you again. Hope you are not late this time for helping me. :-)

I have this senario. User enter a date, called it as ida, and i need to calculate the ExpiredDate based on the terms i retrieve from the db. I manage to print out this term and proved that can retrieve data.

**key word**: I have execute the sql statement YET!! I remark these code,

 if ((eda == null) || (eda.equals(""))){
           pstmt.setNull( 7, java.sql.Types.DATE );
       } else {
             pstmt.setDate(7, new java.sql.Date(eda));
       }

and run the same problem and have the javax.servlet.ServletException: Unparseable date: " "
    at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:471)
error....

meaning to say it's before the code, and i suspect (since it's the only code) is this part

if (ida!=null || ida!="") {
    System.out.println("Inside the null");
   f_a.setTime(df.parse(ida));
    f_a.add(Calendar.MONTH, Integer.parseInt(a_term));
   eda=f_a.getTime();
}

any problem? from the code? the ida is a String, i need to set the ida into f_a as a Calendar. then i add the months into f_a. Then i retrieve the eda date after added the terms.

But when i test it, with a blank ida, it'll prompt me the error... VERY sure that i remark off the SQL part of code.

TQ.


0
 
LVL 6

Expert Comment

by:jarasa
ID: 9927833
Well anyway you can always parse the data as String and make the transformation on the SQL Statement that's why I asked:
pstmt.setString(7, "12/12/2003");
slq= "....... TO_DATE(?,"MM/DD/YYYY") ......"

That's what I mean. most of the time is better becose depending on the database you use Date formats are totally unuseful

Javier
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 9927858
Hmm.......
just one question before we proceed.... have you tried printing the ida or eda at the console??
Imean just do a

 (ida!=null || ida!="") {
    System.out.println("Inside the null");
    System.out.println(ida);
 f_a.setTime(df.parse(ida));
    f_a.add(Calendar.MONTH, Integer.parseInt(a_term));
   eda=f_a.getTime();
System.out.println(eda);
}
 and let us see what we get..... I guess your calculation and juggeling around the date is getting u in trouble.....
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 9932806
>>Well anyway you can always parse the data as String and make the transformation on the SQL Statement.
That might work, but use prepare statement with sql.Date object is better approach then this.
>>  slq= "....... TO_DATE(?,"MM/DD/YYYY") ......"
a lot of database doens't support to_date function.

>>  Unparseable date: " "
notice that there is a space between the quote, and it also should be AND, not OR try:
(ida!=null && ida.trim().length() > 0) {
  ...
0
 

Author Comment

by:fks2
ID: 9939400
yeah, you are right ken,
the space it between.
:-)


if (ida!=null && ida.trim().length() > 0) {
    System.out.println("Inside the null");
    System.out.println("IDA = "+ ida);
    System.out.println("EDA (b4 addtime) = "+ eda);
    f_a.setTime(df.parse(ida));
    f_a.add(Calendar.MONTH, Integer.parseInt(a_term));
    eda=f_a.getTime();
    System.out.println("EDA (after addtime) = "+ eda);
}

I wouldl ike to ask how can initialise a eda ?
if i declare like this,
java.util.Date eda = new java.util.Date();
it'll declare the eda as today date.

if i declare like

java.util.Date eda;

it's prompt me error saying i didn't initialise...

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.

 

Author Comment

by:fks2
ID: 9939417
customer_0005fconfirm$jsp.java [157:1] variable eda might not have been initialized
                    System.out.println("EDA (b4 addtime) = "+ eda);
 
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 9939842
do a eda=null;
that should also take care of compilation atleast...
0
 

Author Comment

by:fks2
ID: 9939866
:-)
ok.
0
 

Author Comment

by:fks2
ID: 9940231
java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Number of query values and destination fields are not the same.
        at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6958)

.
.
.
        PreparedStatement pstmt=null;
        pstmt=conn1.prepareStatement("insert into purchase values(?,?,?)");
        pstmt.setString(1, customer );
        pstmt.setString(2, warranty );

        if ((eda == null) || (eda.equals(""))){
            pstmt.setNull( 3, java.sql.Types.DATE );
        } else {
              pstmt.setDate(3, new java.sql.Date(eda.getTime()) );
        }
.
.
  purchase_number = pstmt.executeUpdate();

CONFIRMED it's the same value.. but how come it's say doesn't same?
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 9940280
thats a strange msg you are getting! the code above will set three parameters one way or the another..!! you sure that your table has got three columns only??
Also ( this is without a reason.... but try putting a semicolon in your query statement)....



0
 
LVL 6

Expert Comment

by:jarasa
ID: 9940654
>>a lot of database doens't support to_date function.

As far as I know MS Access does support it, anyway if they dunnot should have an equivalent, I should give it a try.

Javier
0
 
LVL 14

Accepted Solution

by:
kennethxu earned 125 total points
ID: 9946885
>> insert into purchase values(?,?,?)
does your table has more then 3 columns? try:
insert into purchase( customer, warranty, eda ) values(?,?,?)
0
 

Expert Comment

by:ajtiwari
ID: 10127014
i advice u to write stored procedure for this same purpose and call it from your java programe
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

Title # Comments Views Activity
simple jsp servlet and webservice web example 5 113
trouble with PDF encryption using itext-1.3.1 17 81
session bean client machine 9 57
SimpleDateFormat.parse examples 19 87
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 third 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).   We’ve been talki…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

911 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

22 Experts available now in Live!

Get 1:1 Help Now