Basic concepts of Java, JDBC, SQL, PL/SQL,
Oracle Database.
1.2 Technical Overview
This sample application illustrates the new JDBC 3.0 feature:
Named Parameters, which is bundled with Oracle Database 10g
JDBC drivers. From JDBC 3.0, named parameters can be passed
to the CallableStatement object.
The current sample highlights the use of Named parameters with
CallableStatement where as, the 'How-To' document: 'Named Parameters
- Using Oracle 10g JDBC APIs' discusses usage of Oracle
specific APIs with the PreparedStatement.
From JDBC applications, until JDBC 3.0, positional or the ordinal
binding was the only way to bind the parameters. From JDBC 3.0
onwards, it is possible to use named binding to the parameters
in the CallableStatement object. Like the Oracle PL/SQL, the
JDBC applications can now use both named and ordinal binding.
The advantage of named binding is, the self-documented code
available through the implicit assignment of the parameter and
its value. You can also use the parameters in your own preferred
order. Given a choice, one could use named binding to avoid
confusion and whenever it’s not obvious what the argument corresponds
to and when there are more number of parameters to be bound.
Benefits of the feature:
CallableStatement interface is enhanced
by adding new setter methods by name.
For example,
public void setInt(String parameterName, int x) throws SQLException; public int getInt(String parameterName) throws SQLException;
When named parameters are used, you
do not need to bind all the IN or INOUT parameters, if those
unbound parameters have a default value.
CallableStatement interface
is also enhanced to support retrieving and registering output
parameters by name.
For example,
public void registerOutParameter(String parameterName, int sqlType) throws SQLException;
Using the JDBC 3.0 feature: Named Parameters,
the sample application will call a database procedure by passing
values for the IN and OUT parameters. The sample code makes use
of a table: product_information in OE schema. The called database
procedure will insert a record each in product_information and
product_descriptions tables in the database with the same product_id,
and returns the product_id of the newly created record.
2.1
Code Support
Following
is the code snippet from UsingNamedParameters.java file which
demonstrates the usage of named parameters while calling a
database procedure from a JDBC application.
/** * This method will call a database procedure 'createproduct()' by passing * values for the IN parameters and registering the OUT parameter to retrieve * value from the database procedure. Using the JDBC 3.0 feature: * Named Parameters, IN and OUT variables are bound using names instead of * Integers. * * The database procedure will first create a record in the table: * product_information and then another corresponding record for the same * product_id in the table: product_descriptions. The procedure returns the * created product_id as the OUT parameter when the record is succesfully * created. */ private static void namedParametersInCS(String name, int minprice, String desc, int listprice) throws SQLException, ClassNotFoundException { ...........
// Create SQL query statement to call the database procedure: createproduct // which creates product records in product_information and // product_descriptions tables in the database. String sqlCall = "BEGIN createProduct(?,?,?,?,?); END;"; cstmt = (CallableStatement)conn.prepareCall(sqlCall);// CallableStatement interface is enhanced by adding new setter // methods by name like below: cstmt.setString("productname",name); // Set Product Name. cstmt.setString("productdesc",desc); // Set Product Description. cstmt.setInt("listprice",listprice); // Set Product ListPrice. cstmt.setInt("minprice",minprice); // Set Product MinPrice.// Register out parameter which should return the product is created. // CallableStatement interface is enhanced to support retrieving and // registering output parameters by name. cstmt.registerOutParameter("prod_id",Types.INTEGER);
// Execute the call.
cstmt.execute();
// Get the value of the id from the OUT parameter: prod_id
int id = cstmt.getInt("prod_id");
// Print the created new product id to the standard output.
System.out.println("The new Product id is: "+id);
..................
..................
}
Install document to test and run the
sample application.
3.2 Sample Application files
File
Description
UsingNamedParameters.java
Runnable Java class that demonstrates
the Named Parameters usage by calling the database procedure.
createSP.sql
This SQL file has the SQL script to create
the database procedure createProduct. This database procedure
is called by the UsingNamedParameters.java class.
Connection.properties
File where database details like hostname,
port, sid, username, passwords are stored. This file is read
by UsingNamedParameters.java during execution.
run.bat
Batch
file for executing the sample application automatically on
the Windows platform. Make sure to set environmental variables
mentioned in Install.html before this is run.
run.sh
Batch file for executing the sample application
automatically on the Linux platform. The script when executed
will prompt the user to set environmental variables mentioned
in Install.html.