PreparedStatement有什么作用

PreparedStatement有什么作用

本文介绍了预编译JDBC PreparedStatement有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

预编译一个语句是做什么的,因为我已经看到
如果我编写一个带有错误SQL语法的预准备语句,编译不会报告
任何问题!

What does "precompiling" a statement do, because I have seen thatif I write a prepared statement with a bad SQL syntax that compilation does not reportany problem!

因此,如果预编译预准备语句不检查语法有效性,那么它真正做了什么?

So if precompiling a prepared statement doesn't check for syntax validity what really does it do?

推荐答案

创建 PreparedStatements 可能会也可能不会涉及SQL语法验证甚至DB服务器往返,这完全取决于所使用的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.

因此在某些JDBC驱动程序上 PreparedStatement 为no比正常的声明更准备。 (换句话说:对于一些JDBC驱动程序, PreparedStatement 表示服务器端资源(类似于 Connection ),而在其他方面,它是一个纯粹的客户端构造。)

So on some JDBC drivers a PreparedStatement is no more "prepared" than a normal Statement. (In other words: with some JDBC drivers a PreparedStatement represents a server-side resource (similar to Connection), while on others it's a pure client-side construct).

然而,一个重要的区别是 PreparedStatement 将有助于您可以保证以一种方式处理动态参数值,以避免在尝试将值手动插入SQL语句字符串并使用普通语句执行它时所遇到的任何转义或格式化问题

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 normal Statement.

该功能是 indepdendent 选择预先准备语句,因此它是由每个人提供的JDBC驱动程序,即使它没有做任何其他准备步骤。

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.

这篇关于预编译JDBC PreparedStatement有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 12:33