JDBC -- Statement, PreparedStatement, and CallableStatement

Source: Internet
Author: User

JDBC:

Statement (Interface)
|
PreparedStatement (Interface)
|
CallableStatement (Interface)
The above three are inheritance relationships.
1). concept:
Statement: (used to execute simple SQL statements without parameters)
Each time you execute an SQL statement, the database needs to compile the SQL statement. It is better to use this statement to execute only one query and return results, which is more efficient than PreparedStatement.
Usage:
String SQL = "select seq_orderdetid.nextval as orderid from dual ";
Statement stat1 = conn. createStatement ();
ResultSet rs1 = stat1.executeQuery (SQL );
If (rs1.next ()){
Id = rs1.getLong (1 );
}
PreparedStatement: (used to execute pre-compiled SQL statements with or without the IN parameter)
The executed SQL statements can contain parameters and support batch execution of SQL statements. Because the Cache mechanism is used, pre-compiled statements are placed in the Cache. The next time you execute the same SQL statement, you can obtain the statements directly from the Cache.
Usage:
PreparedStatement pstmt = con. prepareStatement ("update employees set salary =? Where id =? ");
Pstmt. setBigDecimal (1, 153833.00 );
Pstmt. setInt (2, 110592 );
Pstmt. executeUpdate ();
Usage 2:
PreparedStatement pstmt = con. prepareStatement ("update employees set salary =? Where id =? ");
For (int I = 0; I <length; I ++ ){
Pstmt. setBigDecimal (1, param1 [I]);
Pstmt. setInt (2, param2 [I]);
Pstmt. addBatch ();
}
Pstmt. executeBatch ();
CallableStatement: (used to call stored procedures of the database)
The syntax for calling a stored procedure in JDBC is as follows:
{Call process name [(?, ?, ...)]}
The syntax of the returned result parameter is as follows:
{? = Call process name [(?, ?, ...)]}
The syntax of stored procedures without parameters is similar:
{Call process name}
All methods defined in CallableStatement are used to process the output part of the OUT or INOUT parameters: register the JDBC Type of the OUT parameter (General SQL type), and retrieve results from these parameters, or check whether the returned value is jdbc null.
1. Create a CallableStatement object
The CallableStatement object is created using the Connection method prepareCall. The following example creates a CallableStatement instance, which contains a call to the stored procedure getTestData. This process has two variables but does not include the result parameters:
CallableStatement cstmt = con. prepareCall ("{call getTestData (?, ?)} ");
Where? The placeholder is the IN, OUT, or INOUT parameter, depending on the stored process getTestData.
2. IN and OUT parameters
The IN parameter is passed to the CallableStatement object through the setXXX method. This method is inherited from PreparedStatement. The type of the input parameter determines the setXXX method used (for example, using setFloat to pass in the float value ).
If the OUT parameter is returned for a stored procedure, the JDBC Type of each OUT parameter must be registered before the CallableStatement object is executed (this is required because some DBMS require the JDBC Type ). You can use the registerOutParameter method to register the JDBC Type. After the statement is executed, the getXXX method of CallableStatement retrieves the parameter value. The correct getXXX method is the Java type corresponding to the JDBC Type registered by each parameter. In other words, registerOutParameter uses the JDBC Type (so it matches the JDBC Type returned by the database), and getXXX converts it to the Java type.
As an example, the following code first registers the OUT parameter, executes the stored procedure called by cstmt, and then retrieves the value returned in the OUT parameter. The getByte method extracts a Java byte from the first OUT parameter, while getBigDecimal extracts a BigDecimal object from the second OUT parameter (three digits after the decimal point ):
CallableStatement cstmt = con. prepareCall ("{call getTestData (?, ?)} ");
Cstmt. registerOutParameter (1, java. SQL. Types. TINYINT );
Cstmt. registerOutParameter (2, java. SQL. Types. DECIMAL, 3 );
Cstmt.exe cuteQuery ();
Byte x = cstmt. getByte (1 );
Java. math. BigDecimal n = cstmt. getBigDecimal (2, 3 );
3. INOUT Parameters
Besides calling the registerOutParameter method, INOUT parameters that support both input and output are also required to call the appropriate setXXX method (this method is inherited from PreparedStatement ). The setXXX method sets the parameter value as the input parameter, while the registerOutParameter method registers its JDBC Type as the output parameter. The setXXX method provides a Java value, which the driver first converts to a JDBC value and then sends it to the database. The JDBC Type of this IN value should be the same as the JDBC Type provided to the registerOutParameter method. Then, to retrieve the output value, use the corresponding getXXX method. For example, if the Java type is byte, you should use the setByte method to assign the input value. RegisterOutParameter should be provided with a JDBC Type of TINYINT type, and getByte should be used to retrieve the output value.
In the next example, there is a stored procedure reviseTotal. Its unique parameter is the INOUT parameter. The method setByte sets this parameter to 25, and the driver sends it to the database as the jdbc tinyint type. Then, registerOutParameter registers the parameter as jdbc tinyint. After the stored procedure is executed, a new jdbc tinyint value is returned. The getByte method retrieves the new value as a Java byte type.
CallableStatement cstmt = con. prepareCall ("{call reviseTotal (?)} ");
Cstmt. setByte (1, 25 );
Cstmt. registerOutParameter (1, java. SQL. Types. TINYINT );
Cstmt.exe cuteUpdate ();
Byte x = cstmt. getByte (1 );

2). Differences:
A). functions:
The Statement interface provides basic methods for executing statements and obtaining results;
The PreparedStatement interface adds a method to process IN parameters;
The CallableStatement interface adds a method to process the OUT parameter.
B) Usage
Statement:
Common SQL queries without parameters;
PreparedStatement:
Variable Parameter SQL, compiled once, executed multiple times, high efficiency;
High Security, effectively preventing SQL injection and other problems;
Batch update and batch deletion are supported;
CallableStatement:
Inherited from PreparedStatement, supporting SQL operations with parameters;
Supports calling stored procedures and supports output and input/output parameters;

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.