62

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

4 Answers 4

105

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

3
  • Is there way to get it without metadata because if connections fails this code isnt work
    – Bilgehan
    Commented Aug 20, 2020 at 13:48
  • do you think it makes sense that if you cannot connect to the server but can also get the information from the server ? .....
    – Ken Chan
    Commented Aug 20, 2020 at 15:41
  • It dependes on your needs.I need to access only datasource url info in runtime.DirverManagedDatasource has getUrl method and give this info without any server request
    – Bilgehan
    Commented Aug 21, 2020 at 6:53
30

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();
1
  • Will this leak? Commented Jun 15 at 16:06
3

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

1
  • Welcome to SO. Please consider add a link to DatabaseMetaData javadoc.
    – dic19
    Commented Oct 4, 2013 at 18:42
-1

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.

1
  • 2
    Welcome. The OP is asking for the database URL, not the password Commented Jan 25, 2016 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.