?
Solved

Date Formation

Posted on 2003-03-03
18
Medium Priority
?
290 Views
Last Modified: 2010-04-01
Hi friend, see this...

...
...
java.util.Data order_date;
...
...
...
...
   order_date = rs.getDate("OrderDate");
  catch {SQLException e) { e.printStackTrace(); }
...
...
...
 public String getOrder_date() {
    SimpleDateFormat df = new SimpleDateFormat("dd-MMMM-yyyy");
    return df.format(order_date);
}

Any problem with the code??

I think so, the SQLException there throwns me,

com.ibm.db2.jdbc.app.DB2JDBCEception: Data type mismatch.

I have tried to use this coding in other DAtabase such as Mysql and SQL Server. There is no such problems.

Anyone can correct my problems?
0
Comment
Question by:wjh7554
[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
  • 5
  • +1
18 Comments
 
LVL 3

Expert Comment

by:allahabad
ID: 8062077
order_date = rs.getDate("OrderDate");
This will return java.sql.Date type.

So
----> java.util.Data order_date;
Declare this to:
java.sql.date order_date;

0
 
LVL 3

Expert Comment

by:allahabad
ID: 8062161
You have typo.
----> java.util.Data order_date;
change it to:
java.util.Date order_date;
 
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8064730
>> com.ibm.db2.jdbc.app.DB2JDBCEception: Data type mismatch.

it means that you have used getInt, getDate or other getter on a column that is not a int, date or matching data type.

you code looks fine except the typo I think that's not the original code, otherwise it won't compile.

it could be the "OrderDate" column in your database is not in Date datatype. or it could be other getting method in your code.

Please get the full stacktrace and get the line number of you java source file to locate exactly which getter method is giving you the problem.
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!

 

Author Comment

by:wjh7554
ID: 8069551
allahadbad,
I have tried to change the code from java.util.Date to java.sql.Date, yet same error exception come out.

kennethxu, yes, typo is just not the problem that I am facing.
I have changed the code to java.sql.Date.  Manage to complied and when I call that time, it's thrwn me the same exception again.

Datatype not match.
0
 
LVL 3

Expert Comment

by:allahabad
ID: 8069709
You could know what return type is coming from DB.

Comment out the code part which is giving problem,
and get the return type using
System.out.println(rs.getDate("OrderDate").getClass());

0
 

Author Comment

by:wjh7554
ID: 8069839
allahabad,

if I put this,

System.out.println(rs.getDate("OrderDate").getClass());

I couldn't see anything.

If i put

System.out.println(rs.getString("OrderDate").getClass());

it'll come out, java.lang.String().

So.... doesn't help/
0
 
LVL 3

Expert Comment

by:allahabad
ID: 8069872
It means , 'order_date' column is either 'char' or 'varchar2' type in DB.
What value do you get ?
try this:

String order_date;
...
...
...
...
  order_date = rs.getString("OrderDate");

System.out.println("value of order_date : " + order_date));
0
 
LVL 3

Expert Comment

by:allahabad
ID: 8069874
It means , 'OrderDate' column is either 'char' or 'varchar2' type in DB.
What value do you get ?
try this:

String order_date;
...
...
...
...
  order_date = rs.getString("OrderDate");

System.out.println("value of order_date : " + order_date));
0
 

Author Comment

by:wjh7554
ID: 8070121
Ha.....
Friend, you get me wrong.

The data type is in String or Char, yes, correct.

I manage to get the data. but when I want to translate it into Date with this format,.....

Please read my initial question....

>>Hi friend, see this...

...
...
java.util.Data order_date;
...
...
...
...
  order_date = rs.getDate("OrderDate");
 catch {SQLException e) { e.printStackTrace(); }
...
...
...
public String getOrder_date() {
   SimpleDateFormat df = new SimpleDateFormat("dd-MMMM-yyyy");
   return df.format(order_date);
}

Any problem with the code??

I think so, the SQLException there throwns me,

com.ibm.db2.jdbc.app.DB2JDBCEception: Data type mismatch.

I have tried to use this coding in other DAtabase such as Mysql and SQL Server. There is no such problems.

Anyone can correct my problems?
0
 
LVL 3

Accepted Solution

by:
allahabad earned 400 total points
ID: 8072476
If i understood question correctly:
1. Since your return is date into String type , get the return type into String as:
      String   order_date = rs.getString("OrderDate");

2.  Now you want to format this date (order_date) which is of String type and in some other date format,  you want to convert it  into "dd-MMMM-yyyy". To do this , you will have to find out ont the date format
    of the returned order_date. for ex. if order_date "03/04/2003", format type in this case would be"mm/dd/yyyy".

     You can convert this date into your expected format . You can use this method:

public String  getOrder_date(String displayStartDate, String inputFormat,String outputFormat) {

   String retDate = null;
   try {
         java.util.Date displayDate = new SimpleDateFormat(inputFormat).parse(displayStartDate);

         SimpleDateFormat formatter=new SimpleDateFormat (outputFormat);
         retDate = formatter.format(displayDate);
   }
   catch (Exception e) {
      e.printStackTrace();
     
   }

   return retDate;
 }


If you still have problem,  it would be helpful, if you could post  stacktrace, and sql statement that builds prepare statement or statement object.
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8073490
>> System.out.println(rs.getString("OrderDate").getClass());
WRONG! this will always return String type regardless the actually datatype in database. to determine the database type, use your database tools or, in java use:
rs.getMetaData().getColumnTypeName(int column)
or
rs.getMetaData().getColumnClassName(int column)

>> The data type is in String or Char, yes, correct.
As I stated in my earlier comment. the data type defined in your database is IMPORTANT. if your data type in database is char, you CANNOT use getDate even if your data looks like date. you must use getString and then convert the String to Date in java:

SimpleDateFormat df = new SimpleDateFormat( "dd-MMMM-yyyy" ); // this is the format of you datebase data.
String strOrderDate = rs.getString("OrderDate");
java.util.Date orderDate = null
if( strOrderDate != null ) orderDate = df.parse( strOrderDate );

0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8073512
BTW, it is unrecommend to use char type to store Date in  database. I consider this as design flaw.

let us know if you have further enquires.
0
 

Author Comment

by:wjh7554
ID: 8076691
kennethxu, allahbad,

this is my final code.

public String order_date="";
..
..
order_date=rs.getString("OrderDate");
..
..
public String getOrder_date() {
 String retDate="":
try {
   java.util.Date displayDate = new SimpleDateFormat("yyyyMMdd").parse(order_date);
          SimpleDateFormat formatter=new SimpleDateFormat ("dd-MM-yyyy");
             retDate = formatter.format(displayDate);
} catcth (ParseException e) { e.printStackTrace(); }
 return retDate;
}

it's manage to return something that I want. Please see this,
DB type  : IBM DB2
Order Data (Data Type) : Character

*********************

and for my last programs:
java.util.Data order_date;
...
...
...
...
  order_date = rs.getDate("OrderDate");
 catch {SQLException e) { e.printStackTrace(); }
...
...
...
public String getOrder_date() {
   SimpleDateFormat df = new SimpleDateFormat("dd-MMMM-yyyy");
   return df.format(order_date);
}

It's work fine in this environment:

DB Type : SQL Server
Data Type : datetime

***********
So, guys.... meaning to say it's all depend on the data type that I specify.

And the conclusion that i get from kennthxu is depend on the type of DB.....

:-)
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8076835
right, if the datebase datatype is Char, use getString, if database datatype is Date, use getDate.
usually, getString will always work, if you use getString on Date type, it is same as:
getDate().toString()

HTH
0
 

Author Comment

by:wjh7554
ID: 8077523
Thanks for your time allahabad. I think you also learn along with me right?

Ken, go out and grap your points like how i did for you last time...

Thank you ghuys.... it's clear my doubt already...

:-)
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8085071
>> Ken, go out and grap your points like how i did for you last time...
thanks, but I cannot find it :)
0
 
LVL 3

Expert Comment

by:allahabad
ID: 8085124
It seems,  he has given points to me. I do'nt know how can it be moved to your account ?
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 8088757
I think he wanted to give pts to both of you.

I think he just forgot to post a question for kennethxu.

CJ
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Learn how to use the free Acronis True Image app to easily transfer data between iPhones and Android phones.
The Summer 2017 Scholarship Winners have been announced!
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

777 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