Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to convert gregoriancalendar obj to sqldate?

 
hariharan jayaprakash
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • 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?...
 
Sudheer Bhat
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • 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: 67
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Zandis Murāns
Ranch Hand
Posts: 174
Java Java ME Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pass
 
hariharan jayaprakash
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • 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
  • 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: 67
  • Mark post as helpful
  • send pies
  • 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: 67
  • Mark post as helpful
  • send pies
  • 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..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic