How to get database url from java.sql.Connection?

JavaJdbcConnection String

Java Problem Overview


For given Connection instance how do I find out url that the Connection uses to connect the database ? Is it somewhere in Properties returned by Connection.getClientInfo() method?

If there you need me to provide clearer description all comments are welcome. Thank you

Java Solutions


Solution 1 - Java

Connection has the getMetaData() to return DatabaseMetaData . DatabaseMetaData has the getURL() to return the URL for this DBMS.

Solution 2 - Java

I believe you can use the DatabaseMetaData object from the Connection and then get the URL. Try:

DatabaseMetaData dmd = connection.getMetaData();
String url = dmd.getURL();

Solution 3 - Java

Inside the Connection object, you have an object of type DatabaseMetaData, it contains a lot of information about the database.

Lucas de Oliveira gave you a good example of code.

And here is the documentation of the object : Interface DatabaseMetaData

Solution 4 - Java

connection.getClientInfo() has all the details related to connection. It returns a properties object. You can retrieve the value of "password" property to fetch the password that was used for the connection object.

Please let me know if this solves your problem.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRastoView Question on Stackoverflow
Solution 1 - JavaKen ChanView Answer on Stackoverflow
Solution 2 - JavaLucas de OliveiraView Answer on Stackoverflow
Solution 3 - JavaMehdiView Answer on Stackoverflow
Solution 4 - JavaAmitView Answer on Stackoverflow