首页 > 其他 > 详细

通过反射获取DataSource里数据源的账号密码

时间:2018-10-30 12:47:14      阅读:528      评论:0      收藏:0      [点我收藏+]
public Connection getDbConnection()
    {
        Connection conn = null;
        // 获得连接
        DataSource ds = null;
        try
        {
            Context ctx = new InitialContext();
            ds = (DataSource) ctx.lookup("java:comp/env/PCMSDB");
           //ds = (DataSource) ctx.lookup("PCMSJN");// weblogic环境配置数据源
            Class c = ds.getClass();
            //Object invokeTest = c.newInstance();
            Method getPassword = c.getMethod("getPassword", null);
            Method getUsername = c.getMethod("getUsername", null);
            
            Object pwd = getPassword.invoke(ds, null);
            Object username = getUsername.invoke(ds, null);
            //测试输出
            System.out.println("getDbConnection pwd:"+pwd.toString());
            System.out.println("getDbConnection username:"+username.toString());
        }
        catch (Exception e)
        {
            System.out.println("getDbConnection Exception "+e.getMessage());
            e.printStackTrace();
        }
        try
        {
            conn = ds.getConnection();
        }
        catch (SQLException e)        
        {            
            System.out.println("getDbConnection SQLException "+e.getMessage());
        }
        return conn;

    }

 

通过反射获取DataSource里数据源的账号密码

原文:https://www.cnblogs.com/penghq/p/9876052.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!