我今天用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=https://bbs.csdn.net/topics/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 + "]";
}
}
uj5u.com熱心網友回復:
把你的屬性名字改成小寫字母開頭的,BeanUtils是根據setter確定屬性名字的uj5u.com熱心網友回復:
確實是這樣的 這些小問題平時不注意能讓人瘋掉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=鄭州]
uj5u.com熱心網友回復:
謝謝樓上!!!!!uj5u.com熱心網友回復:
物體類 bean 要有對應的取值get() 和set()方法才能賦值轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/141043.html
標籤:Java EE
上一篇:Java背景圖片
下一篇:java寫的懸浮窗
