预编译 JDBCPreparedStatement 有什么作用?
“预编译”语句有什么作用,因为我已经看到了 如果我使用错误的 SQL 语法编写准备好的语句,编译不会报告 有任何问题!
那么,如果预编译准备好的语句不检查语法有效性,那么它到底做了什么?
What does "precompiling" a statement do, because I have seen that
if I write a prepared statement with a bad SQL syntax that compilation does not report
any problem!
So if precompiling a prepared statement doesn't check for syntax validity what really does it do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建
PreparedStatements
可能涉及也可能不涉及 SQL 语法验证,甚至数据库服务器往返,这完全取决于所使用的 JDBC 驱动程序。有些司机会进行往返或验证,有些则不会。因此,在某些 JDBC 驱动程序上,
PreparedStatement
并不比普通的Statement
更“准备”。 (换句话说:对于某些 JDBC 驱动程序,PreparedStatement
表示服务器端资源(类似于Connection
),而在其他驱动程序中,它是纯粹的客户端构造)。然而,一个重要的区别是,PreparedStatement 将帮助您以一种保证避免在尝试将值插入 SQL 时出现的任何转义或格式问题的方式处理动态参数值。手动语句字符串并使用普通的语句执行它。
该功能与是否提前“准备”语句的选择无关,因此每个 JDBC 驱动程序都提供该功能,即使它不执行任何其他准备步骤。
Creating a
PreparedStatements
may or may not involve SQL syntax validation or even DB server roundtrips, that depends entirely on the JDBC driver used. Some drivers will do a roundtrip or validate, others will not.So on some JDBC drivers a
PreparedStatement
is no more "prepared" than a normalStatement
. (In other words: with some JDBC drivers aPreparedStatement
represents a server-side resource (similar toConnection
), while on others it's a pure client-side construct).An important difference, however is that a
PreparedStatement
will help you handle dynamic parameter values in a way that is guaranteed to avoid any escaping or formatting issues that you would have if you try to insert the values into the SQL statement string manually and execute it using a normalStatement
.That feature is indepdendent from the choice of "preparing" the statement beforehand or not, so it's provided by every JDBC driver, even if it doesn't do any other preparation steps.
“语句编译”是发生在数据库上的事情,有助于它更快、更有效地返回结果。如果您使用PreparedStatement,那么它允许数据库重用已编译的语句,并且无需再次执行。错误的 SQL 可能会导致数据库语句编译错误,但并非总是如此。
'statement compilation' is something that happens on the database which helps it return results faster and more efficiently. If you use a PreparedStatement then it allows the database to reuse a statement which was already compiled and saves having to do it again. Bad SQL will likely result in a badly compiles database statement, but not always.