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.
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.
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.
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) −
To get the description of the stored procedures in the database −
Make sure your database is up and running.
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.
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 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.
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 .
Finally, get ResultSet object holding the description of the required procedure, by invoking the getProcedureColumns() method of the DatabaseMetaData interface .
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(" ");
      }
   }
}
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