简体   繁体   中英

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

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

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

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();

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM