JDBC中的Statement和PreparedStatement [重复]

时间:2022-04-22 03:12:14

Possible Duplicate:
Difference between Statement and PreparedStatement

可能重复:Statement和PreparedStatement之间的差异

I have confused with Statement and PreparedStatement in JDBC.Is PreparedStatement is version of Statement? or any other difference in that ? Can any body clear that question. thanks.

我对JDBC中的Statement和PreparedStatement感到困惑。PreparedStatement是Statement的版本吗?或者其他任何不同之处?任何人都可以清楚这个问题。谢谢。

1 个解决方案

#1


2  

From Using Prepared Statements of the Java official tutorials

从使用Java官方教程的准备语句

Sometimes it is more convenient to use a PreparedStatement object for sending SQL statements to the database. This special type of statement is derived from the more general class, Statement, that you already know.

有时使用PreparedStatement对象将SQL语句发送到数据库会更方便。这种特殊类型的语句派生自您已经知道的更通用的类Statement。

If you want to execute a Statement object many times, it usually reduces execution time to use a PreparedStatement object instead.

如果要多次执行Statement对象,通常会减少使用PreparedStatement对象的执行时间。

The main feature of a PreparedStatement object is that, unlike a Statement object, it is given a SQL statement when it is created. The advantage to this is that in most cases, this SQL statement is sent to the DBMS right away, where it is compiled. As a result, the PreparedStatement object contains not just a SQL statement, but a SQL statement that has been precompiled. This means that when the PreparedStatement is executed, the DBMS can just run the PreparedStatement SQL statement without having to compile it first.

PreparedStatement对象的主要特性是,与Statement对象不同,它在创建时会被赋予一个SQL语句。这样做的好处是,在大多数情况下,此SQL语句会立即发送到DBMS,并在其中进行编译。因此,PreparedStatement对象不仅包含SQL语句,还包含已预编译的SQL语句。这意味着当执行PreparedStatement时,DBMS可以只运行PreparedStatement SQL语句而无需先编译它。

Although PreparedStatement objects can be used for SQL statements with no parameters, you probably use them most often for SQL statements that take parameters. The advantage of using SQL statements that take parameters is that you can use the same statement and supply it with different values each time you execute it. Examples of this are in the following sections.

尽管PreparedStatement对象可用于没有参数的SQL语句,但您最常使用它们来获取带参数的SQL语句。使用带参数的SQL语句的优点是,您可以使用相同的语句,并在每次执行时为其提供不同的值。这方面的例子在以下部分中。

#1


2  

From Using Prepared Statements of the Java official tutorials

从使用Java官方教程的准备语句

Sometimes it is more convenient to use a PreparedStatement object for sending SQL statements to the database. This special type of statement is derived from the more general class, Statement, that you already know.

有时使用PreparedStatement对象将SQL语句发送到数据库会更方便。这种特殊类型的语句派生自您已经知道的更通用的类Statement。

If you want to execute a Statement object many times, it usually reduces execution time to use a PreparedStatement object instead.

如果要多次执行Statement对象,通常会减少使用PreparedStatement对象的执行时间。

The main feature of a PreparedStatement object is that, unlike a Statement object, it is given a SQL statement when it is created. The advantage to this is that in most cases, this SQL statement is sent to the DBMS right away, where it is compiled. As a result, the PreparedStatement object contains not just a SQL statement, but a SQL statement that has been precompiled. This means that when the PreparedStatement is executed, the DBMS can just run the PreparedStatement SQL statement without having to compile it first.

PreparedStatement对象的主要特性是,与Statement对象不同,它在创建时会被赋予一个SQL语句。这样做的好处是,在大多数情况下,此SQL语句会立即发送到DBMS,并在其中进行编译。因此,PreparedStatement对象不仅包含SQL语句,还包含已预编译的SQL语句。这意味着当执行PreparedStatement时,DBMS可以只运行PreparedStatement SQL语句而无需先编译它。

Although PreparedStatement objects can be used for SQL statements with no parameters, you probably use them most often for SQL statements that take parameters. The advantage of using SQL statements that take parameters is that you can use the same statement and supply it with different values each time you execute it. Examples of this are in the following sections.

尽管PreparedStatement对象可用于没有参数的SQL语句,但您最常使用它们来获取带参数的SQL语句。使用带参数的SQL语句的优点是,您可以使用相同的语句,并在每次执行时为其提供不同的值。这方面的例子在以下部分中。