Register the Output Parameters – Querying Data from Databases

6.3.7.5.4  Register the Output Parameters

As we discussed in Section 6.3.7.5, after a CallableStatement interface is executed, you need to use the associated getXXX() method to pick up the run result from the CallableStatement object, since it cannot return any result itself. However, before you can do that, you must first register any output parameter in the query statement to allow the CallableStatement to know that an output result is involved and stored in the related output parameters in the query statement.

Once an output parameter is registered, the parameter is considered an OUT parameter, and it can contain run results that can be picked up by using the associated getXXX() method.

To register an output parameter, the registerOutParameter() method that belongs to the CallableStatement interface should be used to declare what kind of Oracle type the OUT parameter will return. A point to be noted is that a parameter in a query statement can be defined as both an IN and an OUT at the same time, which means that you can set up this parameter as an IN by using the setXXX() method, and you can also register this parameter as an OUT using the reg-isterOutParameter() method at the same time. In this way, this parameter can be considered an IN/OUT parameter with both input and output functions.
The syntax to register an output parameter is:

registerOutParameter(int position, data _ type Oracle _ data _ type);

where position is still the relative position of the OUT parameter in the query statement, and Oracle _ data _ type is the Oracle data type of the OUT parameter, which can be found from the JDBC API class java.sql.TYPE.
An example of using this method is shown here:

String query = “{call FacultyCourse(?,?)}”;
cstmt = LogInFrame.con.prepareCall(query);
cstmt.setString(1, ComboName.getSelectedItem().toString());
cstmt.setString(2, “CSC-230A”);
cstmt.registerOutParameter(2, oracle.jdbc.OracleTypes.VARCHAR);

There are two parameters in the CallableStatement interface in this example. The first one is an IN parameter, which is set by using the setString() method. The second one is an IN/OUT parameter, which is first set up by using the setString() method and then registered by using the registerOutParameter() method with the data type of VARCHAR. The Oracle data type VARCHAR can be mapped to a data type of String in Java.

An interesting point to theregisterOutParameter() method is that all OUT parameters can be registered by using this syntax except those OUT parameters with NUMERIC and DECIMAL data types. The syntax to register those OUT parameters looks like:

registerOutParameter(int position, data _ type Oracle _ data _ type, int scale);

The only difference is that the third parameter, scale, is added, and it is used to indicate the number of digits to the right of the decimal point for the OUT parameter.

Leave a Reply

Your email address will not be published. Required fields are marked *


© 2024 vogafloat, LLC ,About, Careers, Contact us,Cookies, Terms, Privacy