我今天用Beanutils.setProperty方法为Student类赋值,并用SQL语句查询,结果出来
Student [FlowID=0, Type=0,IDCard=22222222222222, ExamCard=null, StudentName=null, Location=null, Grade=0] 奇怪的是只有IDCard赋上值了,Student类也很正常无错误,想破脑袋液想不明白,求帮助 ,下面是代码。
//测试类
@Test
public void testGet() {
String sql = "select Flow_ID as FlowID,Type,ID_Card IDCard,"
+ "Exam_Card ExamCard,Student_Name StudentName,Location,"
+ "Grade from stud_infor Where Grade=?;";
Student student = dao.get(Student.class, sql, 60);
System.out.println(student);
}//查询方法
public <T> T get(Class<T> clazz,String sql,Object...args){
T entity = null;Connection connection =null;
PreparedStatement ps =null;
ResultSet rs= null;
try {
//
connection=JDBCTools.getConnection();
ps=connection.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
ps.setObject(i+1, args[i]);
}
rs=ps.executeQuery();
//
ResultSetMetaData rsmd = rs.getMetaData();
//
Map<String,Object> map = new HashMap<String,Object>();
if(rs.next()){
for (int i = 0; i <rsmd.getColumnCount(); i++) {
String columnLable=rsmd.getColumnLabel(i+1);
Object columnValue = rs.getObject(columnLable);
map.put(columnLable, columnValue);
}
}
//
if(map.size()>0){
entity = clazz.newInstance();
for (Map.Entry<String,Object> entry: map.entrySet()) {
String fieldName=entry.getKey();
Object fieldValue=entry.getValue();
//ReflectionUtils.setFieldValue(entity, fieldName, fieldValue);
BeanUtils.setProperty(entity, fieldName, fieldValue);
}
}
} catch (Exception e) {
e.printStackTrace();
}finally{
JDBCTools.release(rs, ps, connection);//关闭资源
}
return entity;
}//Student类
public class Student {
private int FlowID;//流水号
private int Type;//四六级
private String IDCard;//身份证号
private String ExamCard;//准考证号
private String StudentName;//学生姓名
private String Location;//区域
private int Grade;//学生成绩
public String getLocation() {
return Location;
}
public void setLocation(String location) {
Location = location;
}
public int getFlowID() {
return FlowID;
}
public void setFlowID(int flowID) {
FlowID = flowID;
}
public int getType() {
return Type;
}
public void setType(int type) {
Type = type;
}
public String getIDCard1() {
return IDCard;
}
public void setIDCard(String iDCard) {
IDCard = iDCard;
}
public String getExamCard() {
return ExamCard;
}
public void setExamCard(String examCard) {
ExamCard = examCard;
}
public String getStudentName() {
return StudentName;
}
public void setStudentName(String studentName) {
StudentName = studentName;
}
public int getGrade() {
return Grade;
}
public void setGrade(int grade) {
Grade = grade;
}
public Student() {
super();
}
public Student(int flowID, int type, String iDCard, String examCard, String studentName, String location,
int grade) {
super();
FlowID = flowID;
Type = type;
IDCard = iDCard;
ExamCard = examCard;
StudentName = studentName;
Location = location;
Grade = grade;
}
@Override
public String toString() {
return "Student [FlowID=" + FlowID + ", Type=" + Type + ", IDCard=" + IDCard + ", ExamCard=" + ExamCard
+ ", StudentName=" + StudentName + ", Location=" + Location + ", Grade=" + Grade + "]";
}
}
Student [FlowID=0, Type=0,IDCard=22222222222222, ExamCard=null, StudentName=null, Location=null, Grade=0] 奇怪的是只有IDCard赋上值了,Student类也很正常无错误,想破脑袋液想不明白,求帮助 ,下面是代码。
//测试类
@Test
public void testGet() {
String sql = "select Flow_ID as FlowID,Type,ID_Card IDCard,"
+ "Exam_Card ExamCard,Student_Name StudentName,Location,"
+ "Grade from stud_infor Where Grade=?;";
Student student = dao.get(Student.class, sql, 60);
System.out.println(student);
}//查询方法
public <T> T get(Class<T> clazz,String sql,Object...args){
T entity = null;Connection connection =null;
PreparedStatement ps =null;
ResultSet rs= null;
try {
//
connection=JDBCTools.getConnection();
ps=connection.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
ps.setObject(i+1, args[i]);
}
rs=ps.executeQuery();
//
ResultSetMetaData rsmd = rs.getMetaData();
//
Map<String,Object> map = new HashMap<String,Object>();
if(rs.next()){
for (int i = 0; i <rsmd.getColumnCount(); i++) {
String columnLable=rsmd.getColumnLabel(i+1);
Object columnValue = rs.getObject(columnLable);
map.put(columnLable, columnValue);
}
}
//
if(map.size()>0){
entity = clazz.newInstance();
for (Map.Entry<String,Object> entry: map.entrySet()) {
String fieldName=entry.getKey();
Object fieldValue=entry.getValue();
//ReflectionUtils.setFieldValue(entity, fieldName, fieldValue);
BeanUtils.setProperty(entity, fieldName, fieldValue);
}
}
} catch (Exception e) {
e.printStackTrace();
}finally{
JDBCTools.release(rs, ps, connection);//关闭资源
}
return entity;
}//Student类
public class Student {
private int FlowID;//流水号
private int Type;//四六级
private String IDCard;//身份证号
private String ExamCard;//准考证号
private String StudentName;//学生姓名
private String Location;//区域
private int Grade;//学生成绩
public String getLocation() {
return Location;
}
public void setLocation(String location) {
Location = location;
}
public int getFlowID() {
return FlowID;
}
public void setFlowID(int flowID) {
FlowID = flowID;
}
public int getType() {
return Type;
}
public void setType(int type) {
Type = type;
}
public String getIDCard1() {
return IDCard;
}
public void setIDCard(String iDCard) {
IDCard = iDCard;
}
public String getExamCard() {
return ExamCard;
}
public void setExamCard(String examCard) {
ExamCard = examCard;
}
public String getStudentName() {
return StudentName;
}
public void setStudentName(String studentName) {
StudentName = studentName;
}
public int getGrade() {
return Grade;
}
public void setGrade(int grade) {
Grade = grade;
}
public Student() {
super();
}
public Student(int flowID, int type, String iDCard, String examCard, String studentName, String location,
int grade) {
super();
FlowID = flowID;
Type = type;
IDCard = iDCard;
ExamCard = examCard;
StudentName = studentName;
Location = location;
Grade = grade;
}
@Override
public String toString() {
return "Student [FlowID=" + FlowID + ", Type=" + Type + ", IDCard=" + IDCard + ", ExamCard=" + ExamCard
+ ", StudentName=" + StudentName + ", Location=" + Location + ", Grade=" + Grade + "]";
}
}
解决方案 »
- netbeans 无法运行 操作mysql程序
- tomcat发布出错 不懂
- spring基于session的国际化配置!
- 不小心移动了下tomcat的文件夹位置,结果就报404错误了
- jspsmartupload求救啊……Files' name is invalid or does not exist (1205)
- 怎样把bean:write 的值赋给一个变量
- 来帮我看看jacorb corba 为什么会出错
- webwork2+spring+hibernate架构
- 已经解决的问题,但不知道为什么,高手过来看看啊
- ejb-ql只能 like '?%' 么?我想实现模糊查寻,想实现 like'%?%' 怎么办??
- Syntax error on token "package", byte expected Syntax error on token "import",
- 求解决
public class Student {
private int flowID;//流水号
private int type;//四六级
private String iDCard;//身份证号
private String examCard;//准考证号
private String studentName;//学生姓名
private String location;//区域
public int getFlowID() {
return flowID;
}
String sql = "select Flow_ID as flowID,type,ID_Card iDCard,"
+ "Exam_Card examCard,Student_Name studentName,location,"
+ "Grade from stud_infor Where Flow_ID=?";
返回结果 Student [flowID=1, type=4, iDCard=412824195263214584, examCard=200523164754000, studentName=张锋, location=郑州]