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

For this Connection an example is how to find out the URL that Connection uses to connect the database? Is it somewhere in the Properties returned by Connection.getClientInfo() ?

If you need me to give a clearer description, all comments are welcome. Thanks you

+47
java jdbc connection-string
Apr 19 '11 at 15:36
source share
4 answers

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

+77
Apr 19 '11 at 3:40 a.m.
source share

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

 DatabaseMetaData dmd = connection.getMetaData(); String url = dmd.getURL(); 
+23
Apr 19 '11 at 15:49
source share

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 code example.

And here is the object documentation: DatabaseMetaData Interface

+2
04 Oct '13 at 18:21
source share

connection.getClientInfo () has all the information related to the connection. It returns a property object. You can get the value of the password property to get the password that was used for the connection object.

Please let me know if this solves your problem.

0
Jan 25 '16 at 15:40
source share



All Articles