EasyManuals Logo

IBM DB2 User Manual

IBM DB2
585 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Page #47 background imageLoading...
Page #47 background image
Learning about parameters in a PreparedStatement using
ParameterMetaData methods
The IBM Data Server Driver for JDBC and SQLJ includes support for the
ParameterMetaData interface. The ParameterMetaData interface contains methods
that retrieve information about the parameter markers in a PreparedStatement
object.
ParameterMetaData methods provide the following types of information:
v The data types of parameters, including the precision and scale of decimal
parameters.
v The parameters’ database-specific type names. For parameters that correspond to
table columns that are defined with distinct types, these names are the distinct
type names.
v Whether parameters are nullable.
v Whether parameters are input or output parameters.
v Whether the values of a numeric parameter can be signed.
v The fully-qualified Java class name that PreparedStatement.setObject uses
when it sets a parameter value.
To invoke ParameterMetaData methods, you need to perform these basic steps:
1. Invoke the Connection.prepareStatement method to create a PreparedStatement
object.
2. Invoke the PreparedStatement.getParameterMetaData method to retrieve a
ParameterMetaData object.
3. Invoke ParameterMetaData.getParameterCount to determine the number of
parameters in the PreparedStatement.
4. Invoke ParameterMetaData methods on individual parameters.
The following code demonstrates how to use ParameterMetaData methods to
determine the number and data types of parameters in an SQL UPDATE statement.
The numbers to the right of selected statements correspond to the
previously-described steps.
Related reference
“Driver support for JDBC APIs” on page 252
Connection con;
ParameterMetaData pmtadta;
int mtadtacnt;
String sqlType;
...
pstmt = con.prepareStatement(
"UPDATE EMPLOYEE SET PHONENO=? WHERE EMPNO=?");
// Create a PreparedStatement object 1
pmtadta = pstmt.getParameterMetaData(); 2
// Create a ParameterMetaData object
mtadtacnt = pmtadta.getParameterCount(); 3
// Determine the number of parameters
System.out.println("Number of statement parameters: " + mtadtacnt);
for(inti=1;i<=mtadtacnt; i++) {
sqlType = pmtadta.getParameterTypeName(i); 4
// Get SQL type for each parameter
System.out.println("SQL type of parameter"+i"is"+sqlType);
}
...
pstmt.close(); // Close the PreparedStatement
Figure 11. Using ParameterMetaData methods to get information about a PreparedStatement
Chapter 3. JDBC application programming 31

Table of Contents

Questions and Answers:

Question and Answer IconNeed help?

Do you have a question about the IBM DB2 and is the answer not in the manual?

IBM DB2 Specifications

General IconGeneral
DeveloperIBM
Initial Release1983
LicenseProprietary
Written inC, C++
CategoryDatabase Management System (RDBMS)
Operating SystemLinux, Windows
Programming LanguagesC, C++
Data ModelsRelational
EditionsCommunity, Standard, Advanced
Key Featureshigh availability, scalability