
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java DatabaseMetaData getPrimaryKeys() method with example
DatabaseMetaData
DatabaseMetaData is an interface that provides methods to access metadata about the database itself.
getPrimaryKeys() method
The getPrimaryKeys() method of the DatabaseMetaData interface in Java is used to retrieve information about the primary keys of a table in a database. A primary key is a unique identifier for each record in 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. |
Steps to access primary key metadata
To get the description of the 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 of the database and, the user name, and 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, the get ResultSet object holds the description of the required primary key columns, by invoking the getPrimaryKeys() method of the DatabaseMetaData interface.
Code implementation for retrieving primary key information
Let us create a table with the name cricketers_data in MySQL database using the CREATE statement as shown below ?
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
Code explanation
In the provided code, we first register the MySQL driver using the registerDriver() method from the DriverManager class. Then, a connection to the database is established by passing the URL of the database, along with the username and password. Once the connection is established, we retrieve a DatabaseMetaData object that holds metadata about the database. Using this DatabaseMetaData object, the getPrimaryKeys() method is called, which retrieves the primary key details for the specified table (cricketers_data). The result is a ResultSet object, from which the table name, column name, catalog, key sequence, and primary key name are extracted and printed to the console.