简体   繁体   English

如何从java.sql.Connection获取数据库URL?

[英]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 ? 对于给定的Connection实例,如何找到Connection用于连接数据库的URL? Is it somewhere in Properties returned by Connection.getClientInfo() method? 它是在Connection.getClientInfo()方法返回的Properties某个位置吗?

If there you need me to provide clearer description all comments are welcome. 如果您需要我提供更清晰的描述,欢迎所有评论。 Thank you 谢谢

Connection has the getMetaData() to return DatabaseMetaData . Connection具有getMetaData()以返回DatabaseMetaData DatabaseMetaData has the getURL() to return the URL for this DBMS. DatabaseMetaData具有getURL()以返回此DBMS的URL。

I believe you can use the DatabaseMetaData object from the Connection and then get the URL. 我相信您可以使用Connection中的DatabaseMetaData对象,然后获取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. 在Connection对象中,您有一个DatabaseMetaData类型的对象,它包含有关数据库的大量信息。

Lucas de Oliveira gave you a good example of code. Lucas de Oliveira为您提供了一个很好的代码示例。

And here is the documentation of the object : Interface DatabaseMetaData 这里是对象的文档: Interface DatabaseMetaData

connection.getClientInfo() has all the details related to connection. connection.getClientInfo()具有与连接相关的所有详细信息。 It returns a properties object. 它返回一个属性对象。 You can retrieve the value of "password" property to fetch the password that was used for the connection object. 您可以检索“password”属性的值以获取用于连接对象的密码。

Please let me know if this solves your problem. 如果这可以解决您的问题,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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