Java DatabaseMetaData getProcedureColumns() method with example



In this article, we will learn the DatabaseMetaData getProcedureColumns() method in Java. In Java getProcedureColumns(), allows us to retrieve information about the input and output parameters of stored procedures in a database.

What is getProcedureColumns()?

The getProcedureColumns() method of DatabaseMetaData retrieves a ResultSet containing details about the parameters (input, output, and return values) of a stored procedure.

Syntax

ResultSet rs = metaData.getProcedureColumns(null, null, "myprocedure", null);

This method retrieves the description of the procedure parameter and results in columns of a database/catalog. It accepts 4 parameters ?

  • catalog: The catalog name; pass null to get all procedures.
  • schemaPattern: The schema name; pass null to get all schemas.
  • procedureNamePattern:  The procedure name pattern.
  • columnNamePattern: The column name pattern; null retrieves all columns.

This method returns a ResultSet object describing specified procedure columns. This object holds values for the following details (as column names) ?

Column name
Data type
Description
PROCEDURE_CAT
String
Catalog of the procedure.
PROCEDURE _SCHEM
String
Catalog of the schema.
PROCEDURE _NAME
String
Name of the procedure.
COLUMN_NAME
String
Name of the column/parameter name of the procedure.
COLUMN_TYPE
Short
Kind of the parameter (IN, OUT, INOUT, ResultSet, etc?)
DATA_TYPE
Int
Data type of the column as integer.
TYPE_NAME
String
Name of the datatype of the column.
PRECISION
int
precision of the column.
LENGTH
int
Length in bytes.
SCALE
short
scale.
RADIX
Short
radix.
NULLABLE
Short
Determines whether it contains null values.
REMARKS
String
Comments on the column.
COLUMN_DEF
String
Default value of the column.
ORDINAL_POSITION
int
Index of the column in the table.
IS_NULLABLE
String
Returns yes if the column contains null values, returns false if the column does not contain null values, and returns an empty String (" ") if null ability of the column cannot be determined.
SPECIFIC_NAME
String
Name of the procedure (which identifies it uniquely).

Retrieves the Description of the Columns

The following program demonstrates how to use the getProcedureColumns() method. It connects to a MySQL database and retrieves a description of the columns of a stored procedure in the database.

To get the description of the columns of a stored procedure in the database ?

  • Make sure your database is up and running.
  • Register the driver using the registerDriver() method of the DriverManager class. Pass an object of the driver class corresponding to the underlying database.
  • Get the connection object using the getConnection() method of the DriverManager class. Pass the URL of the database and, the user name, and password of a user in the database, as String variables.
  • Get the DatabaseMetaData object to the current connection using the getMetaData() method of the Connection interface.
  • Finally, the get ResultSet object holds the description of the required procedure columns, by invoking the getProcedureColumns() method of the DatabaseMetaData interface.

Registering the Driver: Select the required database and register the Driver class of the particular database using the registerDriver() method of the DriverManager class ?

DriverManager.registerDriver(new com.mysql.jdbc.Driver());

Get connection: Create a connection object by passing the URL of the database, username, and password of a user in the database (in string format) as parameters to the getConnection() method of the DriverManager class ?

DriverManager.getConnection(url, user, password);

Retrieving Metadata: retrieves the DatabaseMetaData object ?

DatabaseMetaData metaData = con.getMetaData();

Fetching Type Information: The ResultSet object holds the description of the required procedure columns, by invoking the getProcedureColumns() method  ?

ResultSet rs = metaData.getProcedureColumns(null, null, "myprocedure", null);

Procedure

Let us create a procedure with the name myprocedure in the MySQL database using the CREATE statement as shown below ?

CREATE PROCEDURE myprocedure(
   IN Product_Name VARCHAR(255),
   IN Name_Of_Customer VARCHAR(255),
   IN Month_Of_Dispatch VARCHAR(255),
   IN Price INT, IN Location VARCHAR(255))
   BEGIN
insert into Dispatches values (
   Product_Name,
   Name_Of_Customer,
   Month_Of_Dispatch,
   Price,
   Location);

Example

Following the JDBC program establishes a connection with MySQL database, and retrieves the description of the procedure parameter and result columns of a catalog ?

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DatabaseMetaData_getProcedureColumns {
   public static void main(String args[]) throws SQLException {
      //Registering the Driver
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      //Getting the connection
      String url = "jdbc:mysql://localhost/mydatabase";
      Connection con = DriverManager.getConnection(url, "root", "password");
      System.out.println("Connection established......");
      //Retrieving the meta data object
      DatabaseMetaData metaData = con.getMetaData();
      //Retrieving the columns in the database
      ResultSet rs = metaData.getProcedureColumns(null, null, "myprocedure", null);
      //Printing the column name and size
      while (rs.next()) {
         System.out.println("Procedure name: "+rs.getString("PROCEDURE_NAME"));
         System.out.println("Column name: "+rs.getString("COLUMN_NAME"));
         System.out.println("Type name: "+rs.getString("TYPE_NAME"));
         System.out.println("Precision: "+rs.getString("PRECISION"));
         System.out.println("Radix: "+rs.getString("RADIX"));
         System.out.println(" ");
      }
   }
}

Output

Connection established......
Procedure name: myprocedure
Column name: Product_Name
Type name: VARCHAR
Precision: 255
Radix: 10
Procedure name: myprocedure
Column name: Name_Of_Customer
Type name: VARCHAR
Precision: 255
Radix: 10
Procedure name: myprocedure
Column name: Month_Of_Dispatch
Type name: VARCHAR
Precision: 255
Radix: 10
Procedure name: myprocedure
Column name: Price
Type name: INT
Precision: 10
Radix: 10
Procedure name: myprocedure
Column name: Location
Type name: VARCHAR
Precision: 255
Radix: 10

Conclusion

The DatabaseMetaData.getProcedureColumns() method is a powerful tool for fetching details about stored procedure parameters. This is useful for database schema analysis and dynamic query generation. 

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-02-25T19:20:21+05:30

611 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements