zoukankan      html  css  js  c++  java
  • 通过反射获取DataSource里数据源的账号密码

    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;
    
        }
  • 相关阅读:
    菜根谭#298
    菜根谭#297
    菜根谭#296
    菜根谭#295
    菜根谭#294
    菜根谭#293
    菜根谭#292
    菜根谭#291
    菜根谭#290
    菜根谭#289
  • 原文地址:https://www.cnblogs.com/penghq/p/9876052.html
Copyright © 2011-2022 走看看