public List<Book> getBooks(String name) {
  List<Book> books = ArrayList<Book>();
  String sql = "select * from Book where name?";

  try {
    conn = getConnection();    // 获取连接
    stmt = conn.preapareStatement(sql);    // 命名对象
    stmt.setString(1, '%' + name + '%'); // 参数设置
    rs = stmt.executeQuery();
      while (rs.next()) {
        Book book = new Book();
        book.setId(rs.getInt(1));
        book.setName(rs.getString(2));
        list.add(book);
      }
    } catch (SQLException e) {
      e.printStackTrace();
    } finally {
      close(conn, stmt, rs);
    }

    return books;
}

 

posted on 2016-03-09 16:52  Mtkqy  阅读(307)  评论(0编辑  收藏  举报