How to Retrieve Database Information using Database Meta Data in JDBC?
Last Updated :
26 Apr, 2024
JDBC (Java Database Connectivity) is the standard Java API for connecting and interacting with relational databases. It allowed to the Java applications to execute the SQL queries, retrieve the results, and perform the operations of the database in programmatically.
In this article, we will learn to retrieve database Information Using Database MetaData in JDBC.
Retrieve Database Information Using Database MetaData in JDBC
This metadata provides valuable insights into the structure of the database which is used for various advantages like generation of dynamic queries and validations of the data and schema introspection. This access to the database metadata enhances the flexibility, efficiency, and maintainability of the Java applications that rely on the JDBC for database connectivity and management.
Prerequisites:
The following are the prerequisites to retrieve database information using DatabaseMetaData in JDBC
- Knowledge of Java programming
- Basic understanding of SQL
- Corresponding JDBC driver
- Eclipse IDE (Optional)
Note: Ensure that your database server is running and accessible from the Java applications.
Step-by-Step instructions to retrieve database information using DatabaseMetaData in JDBC
Step 1: Set up Eclipse Project
- Open Eclipse IDE and create one new Java project.
- After that create a new class in a Java project and name it as “DatabaseMetadata“.
- If you are not using Maven/Gradle dependency management, you must add the JDBC driver for your database connection.
Here is the path for the “Java Class” and “JDBC driver” file
Path for class file and jar file
Step 2: Implement the code
Java
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DatabaseMetadataExample {
public static void main(String[] args) {
Connection connection = null;
try {
// Establish connection to the database
connection = DriverManager.getConnection("jdbc:mysql://localhost:3307/work", "root", "tiger");
// Get DatabaseMetaData
DatabaseMetaData metaData = connection.getMetaData();
// Retrieve table names
ResultSet resultSet = metaData.getTables(null, null, "%", new String[]{"TABLE"});
System.out.println("Tables in the database:");
while (resultSet.next()) {
String tableName = resultSet.getString("TABLE_NAME");
System.out.println(tableName);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// Close connection
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
Explanation of the above Program:
- In the above example, we are establish the connection to the MySQL database with the database URL, username and password. The connection is create with the “DriverManager.getConnection()” method.
- After establish the connection, it will obtain the “DatabaseMetaData” object which is named as “metaData“. This object is allowed access to the metadata about database.
- The above code retrieves the information about the tables in the MySQL database with the help of getTables() method of “DatabaseMetadata“.
- try-catch block is used to caught and handled the SQL Exceptions.
- If an exception occurred in the code, the stack trace is prints to aid in the debugging.
- In finally block the connection is closed with the “connection.close()” method.
Note: Make sure that you must be replace the database URL, username and password respectively.
Step 3: Run the Application
- After implementation of the code, you need to run the Java project.
- For run the class file, right click on Java project and then select Run As > Java Application.
The output will be shown in your console window of your Eclipse IDE as shown below:
Output:
Output
The above output shows the list of tables which are present in the database.
Share your thoughts in the comments
Please Login to comment...