Why PreparedStatement is preferable over Statement

回眸只為那壹抹淺笑 提交于 2019-12-08 04:32:20

问题


Though , PreparedStatement can do everything i.e DDL ,DML related queries with better performance than Statement.Than , why still Statement interface exist in Java API?


回答1:


Because some queries don't have any parameter, and preparing the statement and then executing it can actually be less efficient than executing the statement directly. It's also more readable and straightforward in this case to use (and potentially reuse) a single statement.

Here's what the javadoc says, BTW:

SQL statements without parameters are normally executed using Statement objects. If the same SQL statement is executed many times, it may be more efficient to use a PreparedStatement object.



来源:https://stackoverflow.com/questions/10717355/why-preparedstatement-is-preferable-over-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!