这里为什么这样用呢???

xingshen100 2013-10-15 12:01:11

package cn.itcast.utils;

import java.lang.reflect.Field;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;

public class BeanHandler implements ResultSetHandler {
private Class clazz;
public BeanHandler(Class clazz){
this.clazz = clazz;
}
public Object handler(ResultSet rs) {
try{
if(!rs.next()){
return null;
}
Object bean = clazz.newInstance();

ResultSetMetaData metadata = rs.getMetaData();
int columnCount = metadata.getColumnCount(); //得到结果集中有几列数据
for(int i=0;i<columnCount;i++){
String coulmnName = metadata.getColumnName(i+1); //得到每列的列名
Object coulmnData = rs.getObject(i+1);

Field f = clazz.getDeclaredField(coulmnName);//反射出类上列名对应的属性
f.setAccessible(true);
f.set(bean, coulmnData);
}
return bean;


}catch (Exception e) {
throw new RuntimeException(e);
}
}
}



这里为什么用getDeclaredField()方法,getDeclaredField()方法不是获取私有字段的吗?但是这里它是怎么确定clazz类中的字段都是私有的,或者是getDeclaredField()方法既可以获得私有字段,也可以获得公有字段吗?查jdk api文档就没发现介绍这个方法的详细使用情况,大家都查什么文档呢?
...全文
148 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
呼啸 2013-10-15
  • 打赏
  • 举报
回复
引用 楼主 xingshen100 的回复:

package cn.itcast.utils;

import java.lang.reflect.Field;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;

public class BeanHandler implements ResultSetHandler {
	private Class clazz;
	public BeanHandler(Class clazz){
		this.clazz = clazz;
	}
	public Object handler(ResultSet rs) {
		try{
			if(!rs.next()){
				return null;
			}
			Object bean = clazz.newInstance();
			
			ResultSetMetaData metadata = rs.getMetaData();
			int columnCount = metadata.getColumnCount();  //得到结果集中有几列数据
			for(int i=0;i<columnCount;i++){
				String coulmnName = metadata.getColumnName(i+1);  //得到每列的列名
				Object coulmnData = rs.getObject(i+1);
				
				Field f = clazz.getDeclaredField(coulmnName);//反射出类上列名对应的属性
				f.setAccessible(true);
				f.set(bean, coulmnData);
			}
			return bean;
		
		
		}catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}

这里为什么用getDeclaredField()方法,getDeclaredField()方法不是获取私有字段的吗?但是这里它是怎么确定clazz类中的字段都是私有的,或者是getDeclaredField()方法既可以获得私有字段,也可以获得公有字段吗?查jdk api文档就没发现介绍这个方法的详细使用情况,大家都查什么文档呢?
getDeclaredField是获取已声明的字段
无聊找乐 2013-10-15
  • 打赏
  • 举报
回复
Declared是已申明的意思,getDeclaredField()是获取申明的字段。 私有是private
teemai 2013-10-15
  • 打赏
  • 举报
回复
这个方法返回的是所有类型的,public protected default private 看这里: getDeclaredFields public Field[] getDeclaredFields() throws SecurityException Returns an array of Field objects reflecting all the fields declared by the class or interface represented by this Class object. This includes public, protected, default (package) access, and private fields, but excludes inherited fields. The elements in the array returned are not sorted and are not in any particular order. This method returns an array of length 0 if the class or interface declares no fields, or if this Class object represents a primitive type, an array class, or void. See The Java Language Specification, sections 8.2 and 8.3. Returns: the array of Field objects representing all the declared fields of this class Throws: SecurityException - If a security manager, s, is present and any of the following conditions is met: invocation of s.checkMemberAccess(this, Member.DECLARED) denies access to the declared fields within this class the caller's class loader is not the same as or an ancestor of the class loader for the current class and invocation of s.checkPackageAccess() denies access to the package of this class Since: JDK1.1 http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧