aspose file tools*
The moose likes JDBC 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
Bookmark "DB2 query cannot work using preparedstatement" Watch "DB2 query cannot work using preparedstatement" New topic
Author

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:
com.ibm.db2.jcc.b.SQLException: 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
Marshal

Joined: May 26, 2003
Posts: 30929
    
158

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, 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 ]
 
Don't get me started about those stupid light bulbs.
 
subject: DB2 query cannot work using preparedstatement