Java DatabaseMetaData getProcedures() method with example


This method retrieves the description of the stored procedures of a database/catalog. It accepts 3 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.

This method returns a ResultSet object describing specified procedure. 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.
REMARKS
String
Comments on the column.
SPECIFIC_NAME
String
Name of the procedure (which identifies it uniquely).


To get the description of the 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, 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 above created procedure.

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DatabaseMetaData_getProcedures {
   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 procedures = metaData.getProcedures(null, null, "myprocedure");
      //Printing the column name and size
      while (procedures.next()) {
         System.out.println("Catalog name: "+procedures.getString("PROCEDURE_CAT"));
         System.out.println("Procedure name: "+procedures.getString("PROCEDURE_NAME"));
         System.out.println("Type of the procedure: "+procedures.getShort("PROCEDURE_TYPE"));
         System.out.println("Specific name of the procedure: "+procedures.getString("SPECIFIC_NAME"));
         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

Vikyath Ram
Vikyath Ram

A born rival

Updated on: 30-Jul-2019

405 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements