java 从 DataSource 类中获取主机名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6340684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:28:28  来源:igfitidea点击:

Get the hostname from a DataSource class

javadatabase-connectiondatasourcehostname

提问by Ron

Is there a way to get the hostname from the DataSource class in java? I mean, I have a DataSource object (DS), which is annotated to get the Jboss datasource. Anyway, I want to get the hostname used in that DS.

有没有办法从 Java 中的 DataSource 类中获取主机名?我的意思是,我有一个数据源对象 (DS),它被注释以获取 Jboss 数据源。无论如何,我想获得该 DS 中使用的主机名。

Debugging, i can see it this way: I get the Connection from DS, then I get the DataSourceMetaData and inside of that the is something called Protocol Connection which have the hostname, but I don't know how to get it.

调试,我可以这样看:我从 DS 获得连接,然后我获得了 DataSourceMetaData,其中有一个叫做协议连接的东西,它有主机名,但我不知道如何获得它。

Anyone here knows how? or another way to get the hostname? Thanks in advance. Kind regards, RDAM

这里有人知道怎么做吗?或另一种获取主机名的方法?提前致谢。亲切的问候, RDAM

回答by Suraj Chandran

Once you have the DatabaseMetaData, just call the getURL()method which should contain the hostname like so:

获得 DatabaseMetaData 后,只需调用getURL()方法,该方法应包含主机名,如下所示:

dataSource.getConnection().getMetaData().getURL();