JDBC - Statement,PreparedStatement,CallableStatement和缓存 [英] JDBC - Statement, PreparedStatement, CallableStatement and caching
问题描述
我想知道有什么区别以及何时使用 Statement
, PreparedStatement
和 CallableStatement
。
I am wondering what are the differences and when to use Statement
, PreparedStatement
, and CallableStatement
.
使用每种方法的最佳做法和典型方案是什么?
What is the best practice and typical scenario of using each of these?
推荐答案
声明与PreparedStatement
Statement vs PreparedStatement
-
使用PreparedStatement可以提高性能,但是依赖于数据库。
Performance can be better with PreparedStatement but is database dependent.
使用PreparedStatement可以避免SQL注入。 PreparedStatement如何避免或阻止SQL注入?
With PreparedStatement you avoid SQL injection. How does a PreparedStatement avoid or prevent SQL injection?
使用setInt更好地检查prepareStatement,setString where where语句,你只需要附加到主SQL。
Better type check with preparedStatement by setInt, setString where as statement you just keep appending to the main SQL.
类似帖子:
Statement和PreparedStatement之间的区别
CallableStatement - 在所有数据库中访问StoredProcedures的Java答案。
CallableStatement - Java answer to access StoredProcedures across all databases.
类似的帖子
CallableStatement vs Statement
使用PreparedStatement和Callable你已经有了缓存,缓存也是一个很大的话题,你不想做所有这些而不是看 ehcache
With PreparedStatement and Callable you already have caching, also caching is a big topic in its own, you wouldn't like to do all of that instead look at ehcache
你几乎总是喜欢PreparedStatement而不是Statement
You should almost always prefer PreparedStatement over Statement
如果你必须在StoredProcedure上操作,你只有一个选项CallableStatement。
If you have to operate over StoredProcedure you have just one option CallableStatement.
这篇关于JDBC - Statement,PreparedStatement,CallableStatement和缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!