This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JDBC and the fly likes how to convert gregoriancalendar obj to sqldate? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "how to convert gregoriancalendar obj to sqldate?" Watch "how to convert gregoriancalendar obj to sqldate?" New topic
Author

how to convert gregoriancalendar obj to sqldate?

hariharan jayaprakash
Ranch Hand

Joined: Sep 09, 2010
Posts: 63
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

Joined: Feb 22, 2011
Posts: 75
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

Joined: Sep 09, 2010
Posts: 63
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

Joined: Feb 22, 2011
Posts: 75
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

Joined: Aug 18, 2009
Posts: 174

pass
hariharan jayaprakash
Ranch Hand

Joined: Sep 09, 2010
Posts: 63
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

Joined: Feb 22, 2011
Posts: 75
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

Joined: Sep 09, 2010
Posts: 63
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

Joined: Sep 09, 2010
Posts: 63


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..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to convert gregoriancalendar obj to sqldate?
 
Similar Threads
Removing Z/z and time from Calender date
java date format stored as varchar - need to convert it into date
dates between two dates
How do i print 1 as 01?
increasing the date in java as whole