dimanche 24 novembre 2019

use for PropertyUtils in conjunction with Class.forName

i am trying to use PropertyUtils utility to dynamically get and set the values of properties of different classes i tried following code:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Database.Connection;

import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
import java.sql.Connection;
import javax.sql.DataSource;
import java.util.Calendar;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
/**
 *
 * @author Administrator
 */
@SessionScoped
public class dbConnectionSQLServer {

    public static String dbname;
    public static String server;
    public static int portno;
    public int var01;
    public String var02;

    public static String getDbname() {
        return dbname;
    }

    public static void setDbname(String dbname) {
        this.dbname = dbname;
    }

    public static String getServer() {
        return server;
    }

    public static void setServer(String server) {
        this.server = server;
    }

    public static int getPortno() {
        return portno;
    }

    public static void setPortno(int portno) {
        this.portno = portno;
    }

    public int getVar01() {
        return var01;
    }

    public void setVar01(int var01) {
        this.var01 = var01;
    }

    public String getVar02() {
        return var02;
    }

    public void setVar02(String var02) {
        this.var02 = var02;
    }

}
String SClass = "Database.Connection.dbConnectionSQLServer";
Class<?> cls       =   Class.forName(SClass);
System.out.println("Class: " + cls.getSimpleName() + " var01: " + cls.getField("var01") );

the result of the above code is

Class: dbConnectionSQLServer var01: public int Database.Connection.dbConnectionSQLServer.var01

but when i use PropertyUtils as

System.out.println("dbName: " + PropertyUtils.getProperty(cls, "var01") );

the resultant is an error saying

Severe: java.lang.NoSuchMethodException: Unknown property 'var01' on class 'class java.lang.Class'

i am unable to figure out what is the problem, pls suggest a solution





Aucun commentaire:

Enregistrer un commentaire