天天看點

JDBC 使用beanutils工具類操作JavaBean(8)

有兩種對象指派的方式:一種是使用反射為對象指派, 另一種通過beanutils方式指派

在JavaEE中,Java類的屬性的通過getter,setter來定義;get(或set)方法,去除get(或set)後,後字母小寫即為Java類的成員變量或字段。

操作Java類的屬性有一個工具包:beanutils

setProperty();// 給對象的成員變量指派
getProperty();// 擷取對象成員變量的值           

複制

public class BeanUtilsTest {
    public void testGetProperty() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException{
        Object object = new Student();
        System.out.println(object);         
        BeanUtils.setProperty(object, "idCard", "211121196509091876");
        System.out.println(object);         
        Object val = BeanUtils.getProperty(object, "idCard");
        System.out.println(val);
    }   
    public void testSetProperty() throws IllegalAccessException, InvocationTargetException {        
        Object object = new Student();
        System.out.println(object);         
        BeanUtils.setProperty(object, "idCard2", "211121196509091876");
        System.out.println(object); 
    }
}           

複制