有没有办法将 JDBC 连接的主机和端口连接到 Oracle 数据库服务器?
我知道,我可以解析 URL。但是我们使用故障转移,我想知道我实际连接的是哪个服务器。由于我们使用的格式不同,URL 的解析是静态的并且容易出错。
无法在连接元数据中找到它。
使用“select * from global_name”我可以获得服务名称。但是我还没有找到获取主机和端口的方法,我们已连接到。
有什么想法吗?
最佳答案
以下是您可以从连接中获得的一些信息:
Connection dbConnection = null;
try {
dbConnection = dataSource.getConnection();
DatabaseMetaData dbMetaData = dbConnection.getMetaData();
getLogger().debug("DB Product Name = " + dbMetaData.getDatabaseProductName());
getLogger().debug("DB Product Version= " + dbMetaData.getDatabaseProductVersion());
getLogger().debug("DB Driver Name = " + dbMetaData.getDriverName());
getLogger().debug("DB Driver Version = " + dbMetaData.getDriverVersion());
getLogger().debug("DB Username = " + dbMetaData.getUserName());
getLogger().debug("DB URL = " + dbMetaData.getURL());
} catch (Exception e) {
getLogger().debug("Failed to recover DatabaseMetaData: "+e.getMessage(), e);
} finally {
if (dbConnection != null) {
try {
dbConnection.close();
} catch (Exception ex) {
getLogger().error("Failed to close the DB connection: "+ex.getMessage(), ex);
}
}
}
如果您需要更多/不同的信息,请检查 DatabaseMetaData。 输出示例:
DB Product Name = Oracle
DB Product Version= Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Data Mining
and Real Application Testing options
DB Driver Name = Oracle JDBC driver
DB Driver Version = 11.2.0.2.0
DB Schema = XXXXX
DB URL = jdbc:oracle:thin:@XXX.XXX.XXX.XXX:11010:MYSID
关于java - 从 jdbc 连接到 oracle 服务器获取 Activity 主机和端口(不要解析 url),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10849954/