
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 getURL() method with example
In this article, we will learn about the DatabaseMetaData getURL() method in Java. The DatabaseMetaData interface provides useful methods to obtain details about the database and the JDBC driver. One such method is getURL(), which returns the URL of the JDBC driver being used.
What is the getURL() Method?
The getURL() method in Java's DatabaseMetaData interface is used to retrieve the URL of the database to which the current connection is established.
Syntax
String dbUrl = metaData.getURL();
This method retrieves the URL of the underlying Database Management System and returns it in the form of a String variable.
-
Returns: A String representing the database URL.
- Throws: SQLException if a database access error occurs.
Using getURL() in a MySQL Database
The following program demonstrates how to use the getURL() method. It connects to a MySQL database and retrieves the URL of the underlying DBMS.
To get the list URL of the underlying DBMS ?
- 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 to the current connection using the getMetaData() method of the Connection interface.
- Finally retrieve the URL for the underlying database, by invoking the getURL() method of the DatabaseMetaData interface.
Registering the Driver: Select the required database and register the Driver class of the particular database using the registerDriver() method of the DriverManager class ?
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Get connection: Create a connection object by passing the URL of the database, username, and password of a user in the database (in string format) as parameters to the getConnection() method of the DriverManager class ?
DriverManager.getConnection(url, user, password);
Retrieving Metadata: Retrieves the DatabaseMetaData object ?
DatabaseMetaData metaData = con.getMetaData();
Fetching URL: Fetches metadata about the URL of DBMS and stores it in the string dbUrl ?
String driver_name = metaData.getDriverName();
Example
Following the JDBC program establishes a connection with the MySQL database, and retrieves the URL of the underlying database ?
import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.DriverManager; import java.sql.SQLException; public class DatabaseMetaData_getURL { 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/example_database"; Connection con = DriverManager.getConnection(url, "root", "password"); System.out.println("Connection established......"); //Retrieving the meta data object DatabaseMetaData metaData = con.getMetaData(); //Retrieving the URL of the underlying in the database String dbUrl = metaData.getURL(); System.out.println("URL for the underlying DBMS: "+dbUrl); } }
Output
Connection established...... jdbc:mysql://localhost/example_database
Conclusion
The getURL() method of DatabaseMetaData is a simple yet powerful tool for retrieving the database URL associated with a connection. It helps developers gain insights into their database connections and aids in debugging and monitoring database configurations.