Java DatabaseMetaData getProcedureColumns() method with example


This method retrieves the description of the procedure parameter and result columns of a database/catalog. It accepts 4 parameters −

  • catalog - A string parameter representing the name of the catalog (database in general) in which the procedure exists, pass "" to get the description of the primary key columns in tables with no catalog and, pass null if you don't want to use catalog and thus narrow the search.

  • schemaPattern - A String parameter representing the name (or, name pattern) of the schema, pass "" in case of no schema and, pass null if you don't want to use schema.

  • procedureNamePattern - A String parameter representing the name of the procedure for which you need the description.

  • columnNamePattern - A String parameter representing the name (or, name pattern) of the column.

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, returns an empty String (" ") if null ability of the column cannot be determined.
SPECIFIC_NAME
String
Name of the procedure (which identifies it uniquely).


To get the description of the columns of a stored procedures 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 the database and, user name, password of a user in the database, as String variables.

  • Get the DatabaseMetaData object with respect to the current connection using the getMetaData() method of the Connection interface.

  • Finally, get ResultSet object holding the description of the required procedure columns, by invoking the getProcedureColumns() method of the DatabaseMetaData interface .

Example

Let us create a procedure with 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);

Following JDBC program establishes connection with MySQL database, 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

Rishi Raj
Rishi Raj

I am a coder

Updated on: 30-Jul-2019

310 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements