• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Prepared statement in DB2

 
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all
i am using db2 as my back end
when i issue the below lines its working fine in the command editor


select * from mytable where birth_date = '03/01/1997'

but when i tried to execute the above query using prepared statement i am getting error

in the prepared statement i am setting it as string.

stmtObj.setString(0,strBirthDate);

strBirthDate is string which holds '03/01/1997',
i donno why it is not executing when using the prepared statement, the same thing works out when i build the query and execute it as a simple stmt.

Any pointers will be greatly appreciated.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the error that you are getting? Just a thought but, maybe db2 is looking for a certain date format. I have seen the format YYYY-MM-DD HH:MM:SS.ms
 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I assume your birth_date column in DB2 is Date data type. In this case I believe you would need to use setDate() not setString().

The PreparedStatement javadoc has this to say

"Note: The setter methods (setShort, setString, and so on) for setting IN parameter values must specify types that are compatible with the defined SQL type of the input parameter. For instance, if the IN parameter has SQL type INTEGER, then the method setInt should be used.

If arbitrary parameter type conversions are required, the method setObject should be used with a target SQL type."

so possibly you could use setObject(0, strBirthDate, java.sql.Types.DATE)
 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jim,

Yes, DB2 might look for some own format for date, but
select * from mytable where birth_date = '03/01/1997'
is working fine, thats why i am passing the date of this format and execute it using the prepared statement.
reply
    Bookmark Topic Watch Topic
  • New Topic