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 fileStep 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:
OutputThe above output shows the list of tables which are present in the database.
Similar Reads
How to add Image to MySql database using Servlet and JDBC Structured Query Language or SQL is a standard Database language which is used to create, maintain and retrieve the data from relational databases like MySQL, Oracle, SQL Server, PostGre, etc. In this article, we will understand how to add an image to the MYSQL database using servlet. MYSQL is a rel
6 min read
How to Extract Database Metadata using JDBC? In JDBC, for extracting Metadata from the database we have one interface which is DatabaseMetaData. By using this interface, we can extract the metadata from the Database. We have a connection object It has getMetaData() method. After this, define the DatabaseMetaData and create an object for this.
8 min read
How to Handle SQL Injection in JDBC using PreparedStatement? In this article, we can learn how to prevent SQL injection in JDBC by using Java programming. Basically, SQL injection is a type of security vulnerability It occurs when hackers or attackers can change or manipulate the SQL query, and this can lead to unauthorized access to resources and also perfor
4 min read
How to Retrieve Data from a ResultSet in JDBC? In Java database connectivity, the ResultSet is an interface, and it has a lot of built-in methods for handling the results of SQL queries. In this article, we will learn to retrieve data from a ResultSet in JDBC. ResultSet in InterfaceResultSet interface represents the result set of a database quer
4 min read
How to Perform a Bulk Insertion using JDBC? Performing a bulk insert using JDBC involves using the PreparedStatement interface and addBatch() method. This addBatch() method increases the performance while data is inserted into a database. In this article, we will learn how to perform a bulk insert using JDBC. Approach:For this, we need to cre
4 min read
Java Program to Insert Details in a Table using JDBC Java Database Connectivity is basically a standard API(application interface) between the java programming language and various databases like Oracle, SQL, Postgres, SQL, etc. It connects the front end(for interacting with the users ) with the backend( for storing data). Algorithm: Search/ Insert/ D
4 min read