Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Date comparison in PreparedStatement gives Invalid column index Exception

Dean SchulzeMar 4 2008 — edited Mar 5 2008
When I set a date parameter in a PreparedStatement I get this Exception:

java.sql.SQLException: Invalid column index

Here's the code:

GregorianCalendar gc = new GregorianCalendar();
gc.set(Calendar.HOUR_OF_DAY, 6);
gc.set(Calendar.MINUTE,0);
gc.set(Calendar.SECOND,0);

String sql =
"SELECT notes FROM JOBQUEUE" +
" WHERE userkey='?' AND" +
" EXECUTE='buildGraph' AND" +
" request_stamp > ?";

try {

Connection c = getConnection();
PreparedStatement ps = c.prepareStatement(sql);
String userName = user.getUserName();
ps.setString(1, userName);
ps.setDate(2, new java.sql.Date(gc.getTimeInMillis()));
....

The setDate() call throws the SQLException above. Is there something wrong with doing a date comparison with the '>' operator in a PreparedStatement?

I'd like to avoid the ugliness of converting my Date back into a String and using to_date(). I thought that PreparedStatement was supposed to avoid all of this conversion nonsense.

Thanks.

Dean
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 2 2008
Added on Mar 4 2008
2 comments
3,201 views