Solved

date problem...again..

Posted on 2003-12-12
15
1,530 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
Comment Utility
Hi could you please post you SQL STATEMENT??
I think there is could be the problem.

Javier
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>>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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:fks2
Comment Utility
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
Comment Utility
do a eda=null;
that should also take care of compilation atleast...
0
 

Author Comment

by:fks2
Comment Utility
:-)
ok.
0
 

Author Comment

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

Featured Post

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!

Join & Write a Comment

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
A procedure for exporting installed hotfix details of remote computers using powershell
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

13 Experts available now in Live!

Get 1:1 Help Now