Java DatabaseMetaData getURL() method with example


This method retrieves the URL of the underlying Database Management System and returns in the form of a String variable.

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 the database and, user name, 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 retrieve the URL for the underlying database, by invoking the getURL() method of the DatabaseMetaData interface .

Example

Following JDBC program establishes connection with MySQL database, 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

Updated on: 30-Jul-2019

272 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements