• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to convert gregoriancalendar obj to sqldate?

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am having a table with "date type" as one column.

table name is "TEST"

Index dates
1 01/01/2011
2 02/01/2011
3 03/01/2011
-----------------------
4 10/01/2011

select Index from TEST t where t.dates IN(------);

Here the values i am passing the dates as "Gregorian Calendar", but how can i convert Gregorian to sql date to retrieve?...
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can create a sql Date with java.sql.Date(long millisecs) and you can get the time in milisecs from your calendar object.
 
hariharan jayaprakash
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried like ..

From gregorian calendar object, gc.getTime().getTime()..

so my query returns like.

SELECT INDEX FROM TEST t WHERE t.dates IN(2011-01-06,2011-01-08,2011-01-09,2011-01-10)

but got the error,

java.sql.SQLSyntaxErrorException: ORA-00932: inconsistent datatypes: expected DATE got NUMBER
 
Sudheer Bhat
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are passing a Number where DB is expecting a date. Looks like you are not passing java.sql.Date to the sql IN list.
 
Ranch Hand
Posts: 174
Java ME Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
pass
 
hariharan jayaprakash
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did the same,

StringBuffer selectStmt = new StringBuffer();
selectStmt.append("SELECT INDEX quantity FROM TEST t where t.dates IN(");
GregorianCalendar gc = new GregorianCalendar(2003,04,06);
selectStmt.append(new Date(gc.getTime().getTime()));
selectStmt.append(")");
ps = con.prepareStatement(selectStmt.toString());
rs = ps.executeQuery();

Actually Here i ll get the Gregorian calendar objects dynamically... just for reference i created the object.
 
Sudheer Bhat
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have a major, major problem in your code.

In the above line you are appending the string representation of the date object to the SQL query.

Here is what I would suggest you to do.
Change the code to make use of PreparedStatement. Then bind the java.sql.Date values to the query and execute the query.

Please make use of code tags when you post code snippets. It makes reading your code a lot easier.

 
hariharan jayaprakash
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have so many columns in my table, i have used perparedstatement for the remaining colums and for date columns, i used like this
I am not sure. what went wrong here in my code..
 
hariharan jayaprakash
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Here GregorianCalendar objects ll get in a Vector called "dates" dynamically.... just for reference i tried creatin GregorianCalendar manually and tried, even though problem is still rising..
reply
    Bookmark Topic Watch Topic
  • New Topic