- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- 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 Connection getMetaData() method with example
Generally, Data about data is known as metadata. The DatabaseMetaData interface provides methods to get information about the database you have connected with like, database name, database driver version, maximum column length etc...
The getMetaData() method of the Connection interface retrieves and returns the DatabaseMetaData object. This contains information about the database you have connected to. You can get information about the database such as name of the database, version, driver name, user name and, url etc… by invoking the methods of the DatabaseMetaData interface using the obtained object.
This method returns the DatabaseMetaData object which holds information about the underlying database.
To get the DatabaseMetaData object for the underlying database.
Register the driver using the registerDriver() method of the DriverManager class as −
//Registering the Driver DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Get the connection using the getConnection() method of the DriverManager class as −
//Getting the connection String url = "jdbc:mysql://localhost/mydatabase"; Connection con = DriverManager.getConnection(url, "root", "password");
Get the metadata object using the getMetaData() method as −
DatabaseMetaData dbMetadata = con.getMetaData();
Following JDBC program establishes a connection with the database and retrieves information about the underlying database such as name of the database, driver name, URL etc…
Example
import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.DriverManager; import java.sql.SQLException; public class Connection_getMetaData { 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......"); //Creating the DatabaseMetaData object DatabaseMetaData dbMetadata = con.getMetaData(); //invoke the supportsBatchUpdates() method. boolean bool = dbMetadata.supportsBatchUpdates(); if(bool) { System.out.println("Underlying database supports batch updates"); } else { System.out.println("Underlying database doesn’t support batch updates"); } //Retrieving the driver name System.out.println("Driver name: "+dbMetadata.getDriverName()); //Retrieving the driver version System.out.println("Database version: "+dbMetadata.getDriverVersion()); //Retrieving the user name System.out.println("User name: "+dbMetadata.getUserName()); //Retrieving the URL System.out.println("URL for this database: "+dbMetadata.getURL()); } }
Output
Connection established...... Underlying database supports batch updates Driver name: MySQL-AB JDBC Driver Database version: mysql-connector-java-5.1.12 ( Revision: ${bzr.revision-id} ) User name: root@localhost URL for this database: jdbc:mysql://localhost/mydatabase