publicstatic Date convertFromStringToDate(String stringDate){// 补全代码:
Date date =null;try{
date = sdf.parse(stringDate);}catch(ParseException e){// TODO Auto-generated catch block
e.printStackTrace();}return date;}
public Book findByName(String bname){// 请补全sql语句
String sql ="select * from book where name = ?";
Book b = ou.getOne(sql, Book.class, bname);return b;}
步骤 6:继续完善 SBDaoImpl 类
/**
* 查询所有书籍中价钱最贵的书籍名称
* @return 返回书籍名称
*/
public String queryExpensiveBook(){// 请补全sql语句
String sql ="select name from book where price = (select max(price) from book)";
Book b = ou.getOne(sql, Book.class);return b.getName();}
public boolean deleteStudent(String name){// 请补全sql语句
String sql ="delete from student where name = ?";
int a = ou.delete(sql, name);if(a>0){returntrue;}else{returnfalse;}}
public boolean borrow(SB sb){// 1.首先依据要借的书名来获取书籍对象
Book b =findByName(sb.getBname());// 2.有书则借书,并返回true;没有书则不借书,并返回falseif(b!=null&&b.getNum()>0){// 往 sb 表中插入相关信息:学生姓名,书籍名称,借书时间。自增id和还书时间不用插入。// 请补全sql语句
String sql1 ="insert into sb (sname,bname,begintime) values (?,?,?)";
ou.add(sql1,sb.getSname(),sb.getBname(),DateUtil.convertFromDateToString(sb.getBeginTime()));// 更新 book 表中对应书籍的数量减1// 请补全sql语句
String sql2 ="update book set num = num - 1 where name = ?";
ou.update(sql2, sb.getBname());returntrue;}else{returnfalse;}}
public boolean giveBack(SB sb){//1.首先查询某人是否在某个时间借阅了某书,但是还没有归还// 请补全sql语句
String sql ="select endtime from sb where sname = ? and bname = ? and begintime = ?";
String btime = DateUtil.convertFromDateToString(sb.getBeginTime());SB f = ou.getOne(sql,SB.class, sb.getSname(),sb.getBname(), btime);//2.借了则归还,并返回true;没有借则不用归还,并返回falseif(f!=null){// 根据借书人、借书名称、借书时间来更新 sb 表中的还书时间为当前时间// 请补全sql语句
String sql1 ="update sb set endtime = ? where sname = ? and bname = ? and begintime = ?";
ou.update(sql1, DateUtil.convertFromDateToString(newDate()),sb.getSname(),sb.getBname(),DateUtil.convertFromDateToString(sb.getBeginTime()));// 更新 book 表中对应书籍的数量加1// 请补全sql语句
String sql2 ="update book set num = num + 1 where name = ?";
ou.update(sql2, sb.getBname());returntrue;}else{returnfalse;}}