How Do You Create a ParameterMetaData Object? in JDBC

The method Prepared Statement.getParameter MetaData() returns a ParameterMetaData object describing the parameter markers that appear in the PreparedStatement object. See java.sql.ParameterMetaData for more information. The PreparedStatement method getParameterMetaData() creates and returns a ParameterMetaData object, as shown in the following code fragment. In this code, the SQL query has only one dynamic input parameter(marked with ?):

The method Prepared Statement. getParameter MetaData() definition is Parameter MetaData getParameter MetaData() throws SQLException

The method retrieves the number, types, and properties of this PreparedStatement object’s parameters. It returns a ParameterMetaData object that contains this information. The method throws a SQLException if a database access error occurs. To create a ParameterMetaData object, you need to perform these steps:

  1. Get a valid database Connection object.
  2. Prepare your SQL query with dynamic parameters marked as ?.
  3. Invoke the Connection. prepare Statement (“Your- SQL -Query”) method to create a PreparedStatement object.
  4. Invoke the Prepared Statement. getParameter MetaData() method to retrieve a Parameter MetaData object (once a Parameter MetaData is created, then you may invoke its methods to get information on the SQL query’s parameters).

Solutions for MySQL and Oracle

Here I will show that MySQL and Oracle drivers do not support Parameter MetaData (if in the future they support this feature, then you may use this code). This example first creates a Prepared Statement object (say pstmt) and then creates a Parameter MetaData object using pstmt.

Running the Solution for a MySQL Database

MySQL’s JDBC driver does not support Parameter MetaData implementation; therefore, it does not create Parameter MetaData at all.

Oracle Database Setup

Table created.

Running the Solution for an Oracle Database

The Solution for HSQLDB

HSQLDB Database Setup

In the following code, mp is an alias name for the database URL and has to be defined in the sqltool.rc file. The environment variable HSQLDB_HOME points to the installation directory of the HSQLDB database.

SPECIAL Commands begin with '' and execute when you hit ENTER. BUFFER Commands begin with ':' and execute when you hit ENTER. COMMENTS begin with '/*' and end with the very next '*/'. PROCEDURAL LANGUAGE commands begin with '* ' and end when you hit ENTER.

All other lines comprise SQL Statements.

SQL Statements are terminated by either a blank line (which moves the statement into the buffer without executing) or a line ending with ';' (which executes the statement).

Running the Solution for an HSQLDB Database

The Solution for a Derby Database

Running the Solution for a Derby Database

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

JDBC Topics