This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes JDBC and Relational Databases and the fly likes DB2 query cannot work using preparedstatement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "DB2 query cannot work using preparedstatement" Watch "DB2 query cannot work using preparedstatement" New topic

DB2 query cannot work using preparedstatement

vivien siu
Ranch Hand

Joined: Nov 10, 2005
Posts: 143
hi, I'm trying to pass in a string object into the PreparedStatement below:

This is the exception I got: The value of input host variable or parameter number "1" cannot be used because of its data type.

The query will smoothly if I use Statement object instead. Please let me know what is wrong with using PreparedStatement?

I'm not available, my BF's name is WORK.
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 32481

One of the big advantages of using Prepared Statements is not to have to worry about date formats and casting. If you really want to use it, see what type the cast function is expecting.

This query could be written as:
where date between ? and ?. This may be more efficient as the database doesn't need to run functions for every single row.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
vivien siu
Ranch Hand

Joined: Nov 10, 2005
Posts: 143
I want to select the values where condition will be "drundate"(value from db2) = a certain date.

however the "drundate" inside the database is a timestamp. I only need to compare the date value and not the whole timestamp.

how can I pass in a string then cast it to a date object correctly?
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
There are a few ways.

- Use database specific function to format your timestamp into date
- split your condition into two like less than <late-day-timestamp> and
greater than <early-day-timestamp>
[ April 26, 2006: Message edited by: Adeel Ansari ]
I agree. Here's the link:
subject: DB2 query cannot work using preparedstatement
It's not a secret anymore!