Java ResultSet、SetObject 与 SetString/SetDate/等
我想知道是否有人认为在准备语句时对所有数据类型使用 PreparedStatement.setObject
是一种不好的做法。一个用例是具有通用的executeQuery() 方法的DAO,该方法可以重复用于所有查询,而不必担心其数据类型。
I'd like to know whether anyone considers using PreparedStatement.setObject
for all data types as bad practice when preparing a statement. A use case for this would be a DAO with a generic executeQuery() method which can be re-used for all queries without having to worry about its data types.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你可以这样做。
例如
,
JDBC 驱动程序将执行类型检查。唯一的缺点可能是(较小的)开销,但与最终得到的更好的可维护代码相比,这是可以忽略不计的。此外,大多数 ORM 框架(例如 Hibernate/JPA)也在底层使用了这一点。
You can do so.
E.g.
with
The JDBC driver will do the type checking. The only disadvantage is maybe the (minor) overhead, but this is negligible as compared to the better maintainable code you end up with. Also, most ORM frameworks like Hibernate/JPA also uses that deep under the covers.
ResultSet
接口没有setObject(..)
方法。它只有一个updateObject(..)
方法。您的意思是PreparedStatement.setObject(..)
吗?在这种情况下,我认为这不是一个坏的做法,但甚至不是一个好的解决方案。对于我来说,我并不真正理解“通用 DAO”的必要性。你能更详细地解释这个想法吗?
The
ResultSet
interface has nosetObject(..)
methods. It has only anupdateObject(..)
method. Did you meanPreparedStatement.setObject(..)
?In this case I think this is not a bad practice, but not even a nice solution. As for me I don't really understand the need for the "generic DAO". Could you explain this idea in more details..?