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.

Sign up to request clarification or add additional context in comments.

3 Comments

Is there way to get it without metadata because if connections fails this code isnt work
do you think it makes sense that if you cannot connect to the server but can also get the information from the server ? .....
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
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();

2 Comments

Will this leak?
@RaglandAsir, can you specify what you mean by leak?
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 Comment

Welcome to SO. Please consider add a link to DatabaseMetaData javadoc.
-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 Comment

Welcome. The OP is asking for the database URL, not the password

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.