JDBCTM guide: Getting Started 6-PreparedStatement

Source: Internet
Author: User

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.

In the default connection mode (enable automatic submission), the statement is automatically submitted or restored when the statement is completed.

If the basic database and driver remain open after the statement is submitted, the same PreparedStatement can be executed multiple times. If this is not true, it is meaningless to try to use the PreparedStatement object instead of the Statement object to improve performance.

The following code uses pstmt (the PreparedStatement object created earlier) to demonstrate how to set the values of Two Parameter placeholders and execute pstmt for 10 times. To do this, the database cannot close pstmt. In this example, the first parameter is set to "Hi" and kept as a constant. In the for loop, the second parameter is set to a different value each time: from 0 to 9.

Pstmt. setString (1, "Hi ");
For (int I = 0; I <10; I ++ ){
Pstmt. setInt (2, I );
Int rowCount = pstmt.exe cuteUpdate ();
}

6.1.3 consistency of data types IN the IN Parameter
In the setXXX method, XXX is of the Java type. It is an implicit JDBC Type (generally an SQL type), because the driver maps the Java type to the corresponding JDBC Type (according to section 8 in the JDBC Guide. 6.2 ing specified in the "ing Java and JDBC types" table) and send the JDBC Type to the database. For example, the following code snippet sets the second parameter of the PreparedStatement object pstmt to 44 and the Java type to short:

Pstmt. setShort (2, 44 );

The driver sends 44 to the database as jdbc smallint, which is a standard ing of the Java short type.

The programmer's responsibility is to ensure that the Java type of each IN parameter is mapped to the JDBC Type compatible with the JDBC data type required by the database. Consider the situation where the database needs jdbc smallint. If setByte is used, the driver sends the jdbc tinyint to the database. This is feasible because many databases can be converted from one type to another, and TINYINT can usually be used wherever SMALLINT applies. However, for applications that apply to as many databases as possible, it is best to use the Java type corresponding to the exact JDBC Type required by the database. If the required JDBC Type is SMALLINT, using setShort instead of setByte will make the application more portable.


6.1.4 use setObject
Programmers can use the setObject method to explicitly convert input parameters to specific JDBC types. This method can accept the third parameter to specify the target JDBC Type. Before sending a Java Object to a database, the driver converts it to the specified JDBC Type.

If the JDBC Type is not specified, the driver maps the Java Object to its default JDBC Type (see the table in section 8.6.4) and sends it to the database. This is similar to the conventional setXXX method. In both cases, the driver maps the Java type of the value to the appropriate JDBC Type before sending it to the database. The difference between the two is that the setXXX method uses the standard ing from the Java type to the JDBC Type (see the table in section 8.6.2 ), the setObject method uses ing from the Java Object type to the JDBC Type (see the table in section 8.6.4 ).

The setObject method allows applications to be more generic by accepting all Java objects and can accept parameter input at runtime. In this case, the application is not clear about the input type during compilation. By using setObject, the application can accept all Java object types as input and convert them to the JDBC types required by the database. The table in section 8.6.5 shows all possible conversions that setObject can perform.


6.1.5 send jdbc null as the IN Parameter
The setNull method allows the programmer to send the jdbc null value to the database as the IN parameter. However, you must specify the JDBC Type of the parameter.

When the Java null value is passed to the setXXX method (if it accepts a Java object as a parameter), jdbc null is also sent to the database. However, the setObject method can accept null values only when the JDBC Type is specified.


6.1.6 send large IN Parameters
The setBytes and setString methods can send an unlimited amount of data. However, sometimes programmers prefer to use smaller blocks to transmit large data. This can be done by setting the IN parameter to a Java input stream. When a statement is executed, the JDBC driver repeatedly calls the input stream, reads its content, and transmits them as actual parameter data.

JDBC provides three methods to set the IN parameter to the input stream: setBinaryStream is used for streams containing unspecified bytes, setAsciiStream is used for streams containing ASCII characters, and setUnicodeStream is used for streams containing Unicode characters. Because the total length of the stream must be specified, the parameters used by these methods are one more parameter than other setXXX methods. This is necessary because some databases need to know the total transfer size before sending data.

The following code uses a stream as the IN parameter to send file content:

Java. io. File file = new java. io. File ("/tmp/data ");
Int fileLength = file. length ();
Java. io. InputStream fin = new java. io. FileInputStream (file );
Java. SQL. PreparedStatement pstmt = con. prepareStatement (
"UPDATE Table5 SET stuff =? WHERE index = 4 ");
Pstmt. setBinaryStream (1, fin, fileLength );
Pstmt.exe cuteUpdate ();

When a statement is executed, the input stream fin is repeatedly called to pass its data.


Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

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.