Using Parameter Metadata

To query a SQLServerPreparedStatement or a SQLServerCallableStatement object about the parameters that they contain, the Microsoft SQL Server 2005 JDBC Driver implements the SQLServerParameterMetaData class. This class contains numerous fields and methods that return information in the form of a single value.

To create a SQLServerParameterMetaData object, you can use the getParameterMetaData methods of the SQLServerPreparedStatement and SQLServerCallableStatement classes.

In the following example, an open connection to the SQL Server 2005 AdventureWorks sample database is passed in to the function, the getParameterMetaData method of the SQLServerCallableStatement class is used to return a SQLServerParameterMetaData object, and then various methods of the SQLServerParameterMetaData object are used to display information about the type and mode of the parameters that are contained within the HumanResources.uspUpdateEmployeeHireInfo stored procedure.

public static void getParameterMetaData(Connection con) {
   try {
      CallableStatement cstmt = con.prepareCall("{call HumanResources.uspUpdateEmployeeHireInfo(?, ?, ?, ?, ?)}");
      ParameterMetaData pmd = cstmt.getParameterMetaData();
      int count = pmd.getParameterCount();
      for (int i = 1; i <= count; i++) {
         System.out.println("TYPE: " + pmd.getParameterTypeName(i) + " MODE: " + pmd.getParameterMode(i));
      }
      cstmt.close();
   }
   catch (Exception e) {
      e.printStackTrace();
   }
}

Note

There are some limitations when using the SQLServerParameterMetaData class with prepared statements. The JDBC driver supports SELECT, DELETE, INSERT, and UPDATE statements. However, these statements should not contain subqueries. In addition, the JDBC driver also supports using the IN, IS, and LIKE predicates. For search conditions, the JDBC driver supports using the IS, <, >, LIKE, >=, <=, NOT IN, <>, !>, !<, and != comparative operators. The FREETEXT operator is not supported.

See Also

Other Resources

Handling Metadata with the JDBC Driver