如何在java.sql.Connection中检索主机,端口,SID,用户和密码信息 [英] How to retrieve host, port, sid, user and password informations in java.sql.Connection

查看:62
本文介绍了如何在java.sql.Connection中检索主机,端口,SID,用户和密码信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Oracle连接(在java.sql.Connection对象中),该连接是从属性文件(ApplicationResources.properties)安装的.

I have an Oracle connection (in a java.sql.Connection object) that is mounted from a properties file (ApplicationResources.properties).

如何捕获有关主机,端口,sid,用户和密码的信息? (我需要此信息来通过我的Java应用程序调用特定的数据库函数)

How do I capture information about host, port, sid, user and password? (I need this information to call a specific database function through my java application)

在我的代码下面:

private void conectaBanco() {
    ServiceLocator sl = new ServiceLocator();
    InitialContext ic = null;
    DataSource dataSource = null;
    try {
        if (conn == null) {
            ic = new InitialContext();
            dataSource = (DataSource) ic.lookup(sl.getInfo().getString("JNDI"));                
            conn = dataSource.getConnection();
            conn.setAutoCommit( true );
                            
            // I need host, port, sid and password information
            System.out.println("USER " + conn.getMetaData().getUserName());                                                
        }
    } catch (SQLException e) {
        logger.error("Erro ao tentar abrir conexao com o banco de dados.", e);
    } catch (NamingException e) {
        logger.error("Erro ao tentar abrir conexao com o banco de dados.", e);
    }
    
    if(conn != null)
        rastrear.definePredicate(conn);
}

推荐答案

我通过在Weblogic配置JDBC上添加其他参数(主机,用户,端口,sid-自动返回密码)来解决此问题. 而我得到的这些值将下面的代码抛出:

I solve this problem adding on my Weblogic configuration JDBC the additional parameters (host, user, port, sid - the password is returned automaticaly). And I got this values throw this code below:

import weblogic.jdbc.wrapper.PoolConnection;
import java.util.Properties;
(...)
public Properties retornaPropriedadesConexao(){
    PoolConnection pc = (PoolConnection)conn;
    Properties p = pc.getConnectionEnv().getDriverProperties();                            
    return p;
}

这篇关于如何在java.sql.Connection中检索主机,端口,SID,用户和密码信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆