Solved

date problem...again..

Posted on 2003-12-12
15
1,556 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
The advancement in technology has been a great source of betterment and empowerment for the human race, Nevertheless, this is not to say that technology doesn’t have any problems. We are bombarded with constant distractions, whether as an overload o…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

830 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