1. 程式人生 > >通過反射獲取DataSource裏數據源的賬號密碼

通過反射獲取DataSource裏數據源的賬號密碼

tcl object code int 獲得 bsp ket span pass

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裏數據源的賬號密碼