Java DatabaseMetaData getPrimaryKeys() method with example


This method retrieves the description of the primary key columns of a table. It accepts 3 parameters −

  • catalog - A string parameter representing the name of the catalog (database in general) in which the table 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.

  • schema - A String parameter representing the name of the schema of the table, pass "" to get the description of the columns in tables with no schema and, pass null if you don't want to use schema.

  • table - A String parameter representing the name of the table.

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

Column name
Data type
Description
TABLE_CAT
String
Catalog of the table.
TABLE_SCHEM
String
Catalog of the schema.
TABLE_NAME
String
Name of the table.
COLUMN_NAME
String
Name of the column.
KEY_SEQ
Short
Sequence number a primary key.
PK_NAME
String
Name of the primary key.


To get the description of required primary key columns of a table 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 primary key columns, by invoking the getPrimaryKeys() method of the DatabaseMetaData interface .

Example

Let us create a table with name cricketers_data in MySQL database using CREATE statement as shown below −

CREATE TABLE cricketers_data(
   ID INT,
   First_Name VARCHAR(255),
   Last_Name VARCHAR(255),
   Date_Of_Birth date,
   Place_Of_Birth VARCHAR(255),
   Country VARCHAR(255),
   PRIMARY KEY (ID));

As you observe the column named "ID" of the above table is declared as primary key column.

Following JDBC program establishes connection with MySQL database, retrieves the description of the primary key column of the above table.

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DatabaseMetaData_getPrimaryKeys {
   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.getPrimaryKeys("mydatabase", null, "cricketers_data");
      //Printing the column name and size
      while (rs.next()){
         System.out.println("Table name: "+rs.getString("TABLE_NAME"));
         System.out.println("Column name: "+rs.getString("COLUMN_NAME"));
         System.out.println("Catalog name: "+rs.getString("TABLE_CAT"));
         System.out.println("Primary key sequence: "+rs.getString("KEY_SEQ"));
         System.out.println("Primary key name: "+rs.getString("PK_NAME"));
         System.out.println(" ");
      }
   }
}

Output

Connection established......
Table name: cricketers_data
Column name: ID
Catalog name: mydatabase
Primary key sequence: 1
Primary key name: PRIMARY

Vikyath Ram
Vikyath Ram

A born rival

Updated on: 30-Jul-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements