搜索功能的实现
程序员文章站
2022-05-07 15:20:45
...
public List<Student> search(Student student) throws SQLException{
StringBuffer sb=new StringBuffer();
sb.append("select * from student where 1=1");
if(student!=null){
if(!student.getStuNumber().equals("")){
sb.append(" and stuNumber='"+student.getStuNumber()+"'");
}
if(student.getStuSex()==1 || student.getStuSex()==0){
sb.append(" and stuSex="+student.getStuSex()+"");
}
if(!student.getStuClass().equals("请选择")){
sb.append(" and stuClass='"+student.getStuClass()+"'");
}
if(!student.getStuAddress().equals("")){
sb.append(" and stuAddress like '%"+student.getStuAddress()+"%'");
}
}
sb.append(" order by createDate desc");
System.out.println(sb.toString());
ResultSet rs = this.db.executeQuery(sb.toString());
List<Student> list=null;
try {
while(rs.next()){
Integer id=rs.getInt("id");
String stuNumber=rs.getString("stuNumber");
String stuPhoto=rs.getString("stuPhoto");
String stuName=rs.getString("stuName");
Integer stuAge=rs.getInt("stuAge");
Integer stuSex=rs.getInt("stuSex");
String stuAddress=rs.getString("stuAddress");
String stuClass=rs.getString("stuClass");
Timestamp time=rs.getTimestamp("createDate");
Date createDate=new Date(time.getTime());
Student student2=new Student();
student2.setId(id);
student2.setStuNumber(stuNumber);
student2.setStuPhoto(stuPhoto);
student2.setStuName(stuName);
student2.setStuAge(stuAge);
student2.setStuSex(stuSex);
student2.setStuAddress(stuAddress);
student2.setStuClass(stuClass);
student2.setCreateDate(createDate);
if(list==null){
list=new ArrayList<Student>();
}
list.add(student2);
}
return list;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw (e);
}finally{
try {
if(rs!=null)
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
db.closeStmt();
db.closeConnetion();
}
}