publicListfindStudentList(DatebeginDate,DateendDate){//TODOAuto-generatedmethodstubStringsql="select*fromstudentwherebirthdaybetween?and?";Listlist=newA...
public List findStudentList(Date beginDate, Date endDate) {
// TODO Auto-generated method stub
String sql = "select * from student where birthday between ? and ?";
List list = new ArrayList();
Student student = null;
Result result = executeQuery(sql, null);
int size = result.getRowCount();
for (int i = 0; i < size; i++) {
student = new Student();
Map map = result.getRows()[i];
student.setId((Integer) map.get("id"));
student.setName((String) map.get("name"));
student.setBirthday((String) map.get("birthday"));
list.add(student);
}
return list;
}
如何拿到查询条件beginDate,我就是想问如何拿到查询条件,或者说public List findStudentList(string sql),如何拿到这个sql, 最好写一个完整的过程 谢谢了 BaseDao是这么写的 省略了连接数据库步骤
* 为预编译sql设置参数
* 预编译的 SQL 语句中的‘?’参数的字符串数组
*/
public void setValue(PreparedStatement ps, List args) {
try {
if (ps != null && args != null) {
int size = args.size();
for (int i = 0; i < size; i++) {
ps.setObject(i + 1, args.get(i));
}
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
/**
* 通用增加,删除,修改
*/
public int executeUpdate(String sql, List args) {
try {
con = this.getConnection();
ps = con.prepareStatement(sql);
setValue(ps, args);
return ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
/**
* 通用查询方法
*/
public Result executeQuery(String sql, List args) {
try {
con = this.getConnection();
ps = con.prepareStatement(sql);
setValue(ps, args);
rs = ps.executeQuery();
//return ResultSupport.toResult(rs);
return ResultSupport.toResult(rs);
} catch (Exception e) {
e.printStackTrace();
System.out.println("Sql 错误");
}
return null;
}
展开