Java executes the Stored Procedure

Source: Internet
Author: User

The callablestatement object provides a standard method for calling stored procedures for all DBMS systems. Stored procedures are stored in the database. The call to a stored procedure is the content contained in the callablestatement object. This call is written using a code-changing syntax. There are two forms: one with result parameters and the other without result parameters. The result parameter is an output (out) parameter, which is the return value of the stored process. The two types can contain variable input (in parameter), output (out parameter), or input and output (inout parameter) parameters. The question mark is used as a placeholder for the parameter.

The syntax for calling stored procedures in JDBC is as follows. Note that square brackets indicate that the content is optional; square brackets are not part of the syntax.
{Call process name [(?, ?, ...)]}
The syntax of the returned result parameter is as follows:
{? = Call process name [(?, ?, ...)]}
The syntax for stored procedures without parameters is similar:
{Call process name}
Generally, the person who creates the callablestatement object should know that the DBMS used supports stored procedures and what these processes are. However, multiple databasemetadata methods can provide such information if you need to check. For example, if the DBMS supports calling stored procedures, the supportsstoredprocedures method returns true, and the getprocedures method returns a description of the stored procedure. Callablestatement inherits the statement method (they are used to process General SQL statements) and the preparedstatement method (they are used to process in parameters ).
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 );

Unlike resultset, callablestatement does not provide a special mechanism for retrieving large out values in incremental mode.

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 );

4. First retrieve the result and then retrieve the out Parameter

Due to the limitations of some DBMS, in order to achieve maximum portability, we recommend that you first retrieve the results produced by executing the callablestatement object, and then use the callablestatement. getxxx method to retrieve out parameters. If the callablestatement object returns multiple resultset objects (by calling the execute method), all results should be retrieved before the out parameter is retrieved. In this case, to ensure that all results are accessed, the statement method getresultset, getupdatecount, and getmoreresults must be called until no results are available.

After retrieving all the results, you can use the callablestatement. getxxx method to retrieve the value in the out parameter.

5. Retrieve the null value as the out Parameter

The value returned to the out parameter may be JDBC null. In this case, the JDBC null value is converted so that the value returned by the getxxx method is null, 0, or false, depending on the type of the getxxx method. For a resultset object, you need to know whether 0 or false is the only method that originates from jdbcnull. The method wasnull is used for detection. If the last value read by the getxxx method is JDBC null, this method returns true; otherwise, flase is returned.

 

Content Source: http://topic.csdn.net/t/20041124/11/3583311.html

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.