6-PreparedStatement
This overview is taken from JDBCTM Database Access from JavaTM: A Tutorial and Annotated Reference. JavaSoft is currently preparing this book. This is a tutorial and an important reference manual for JDBC, which will be published by Addison-Wesley in the spring of 1997 as part of the Java series.
6.1 Overview
This PreparedStatement interface inherits Statement, which is different from the following two aspects:
The PreparedStatement instance contains compiled SQL statements. This is to make the statement "ready ".
The SQL statement contained IN the PreparedStatement object can have one or more IN parameters. The value of the IN parameter is not specified when the SQL statement is created. On the contrary, this statement reserves a question mark ("?") for each IN parameter. As a placeholder. The value of each question mark must be provided through the appropriate setXXX method before the statement is executed.
Because the PreparedStatement object has been pre-compiled, its execution speed is faster than the Statement object. Therefore, SQL statements executed multiple times are often created as PreparedStatement objects to improve efficiency.
As a subclass of Statement, PreparedStatement inherits all functions of Statement. IN addition, it also adds a complete set of methods to set the values sent to the database to replace the IN parameter placeholder. At the same time, the three methods execute, executeQuery, and executeUpdate have been changed so that they no longer need parameters. The Statement form of these methods (the form that accepts SQL Statement parameters) should not be used for the PreparedStatement object.
6.1.1 create a PreparedStatement object
The following code snippet (where con is the Connection object) creates a PreparedStatement object that contains an SQL statement with two IN parameter placeholders:
PreparedStatement pstmt = con. prepareStatement (
"UPDATE table4 SET m =? WHERE x =? ");
The pstmt object contains the statement "UPDATE table4 SET m =? WHERE x =? ", It has been sent to the DBMS and is ready for execution.
6.1.2 pass IN parameters
Before executing the PreparedStatement object, you must set each? Parameter value. This can be done by calling the setXXX method, where XXX is the type corresponding to this parameter. For example, if the parameter has the Java type long, the method used is setLong. The first parameter of the setXXX method is the ordinal position of the parameter to be set, and the second parameter is the value set to this parameter. For example, the following code sets the first parameter to 123456789 and the second parameter to 100000000:
Pstmt. setLong (1, 123456789 );
Pstmt. setLong (2, 100000000 );
Once the parameter value of a given statement is set, it can be used to execute the statement multiple times until the clearParameters method is called to clear it.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service