• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why we can set String to date ?

 
Nakata kokuyo
Ranch Hand
Posts: 472
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, when we have a query like



where mydate have set as datetime in msSQL,

but how come we can put string as follow ?


rather than ps.setDate() ?

thank you for guidance
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JDBC driver has no idea what types the attrbutes are that you are setting. It relies on the programmer to do it correctly.
 
stu derby
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because most databases also have an internal concept of datatype AND also have some implicit conversion rules.

In other words, when you use setDate, the database receives a date object and will use that unconverted in your query.

When you use setString, the database receives a string object and might attempt to convert it to a date object, in order to use the result. The conversion rules are very different for different databases, and I'm not sure that all of them will support string to date conversion; also in some databases such as Oracle, the conversion rules can be altered, either by changing the database configuration or by altering the current session.

Relying on implicit conversion in professional-level code is generally not a good idea. Many databases have an explicit string-to-date conversion function; if for some reason doing the conversion on the database is the right thing to do, then using the explicit conversion function is often the better approach. However, the functions aren't standardized, every database is different.
[ December 22, 2005: Message edited by: stu derby ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic