在jsp页面上怎样实现多条件查询
1个回答
展开全部
public List<Book> findBookCondition(QueryBookCondition condition) {
List<Book> books = new ArrayList<Book>();
StringBuilder sql = new StringBuilder(
"select fid,fname,fprice,fdesc,fimg,fauthor,fnumber " + "from t_books where 1=1 ");
if (condition.getBookName() != null) {
sql.append(" and fname like '%");
sql.append(condition.getBookName());
sql.append("%'");
}
if (condition.getAuthor() != null) {
sql.append(" and fauthor like '%");
sql.append(condition.getAuthor());
sql.append("%'");
}
if (condition.getDesc() != null) {
sql.append(" and fdesc like '%");
sql.append(condition.getDesc());
sql.append("%'");
}
if (condition.getMaxPrice() > 0) {
sql.append(" and fprice <");
sql.append(condition.getMaxPrice());
}
if (condition.getMinPrice() > 0) {
sql.append(" and fprice >");
sql.append(condition.getMinPrice());
}
System.out.println(sql.toString());
conn = super.getConn();
Book book = null;
try {
smt = conn.createStatement();
rs = smt.executeQuery(sql.toString());
while (rs.next()) {
book = new Book();
book.setId(rs.getInt(1));
book.setName(rs.getString(2));
book.setPrice(rs.getDouble(3));
book.setDesc(rs.getString(4));
book.setImg(rs.getString(5));
book.setAuthor(rs.getString(6));
book.setNumber(rs.getInt(7));
books.add(book);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(books);
return books;
}
给你参考下,查书的条件
List<Book> books = new ArrayList<Book>();
StringBuilder sql = new StringBuilder(
"select fid,fname,fprice,fdesc,fimg,fauthor,fnumber " + "from t_books where 1=1 ");
if (condition.getBookName() != null) {
sql.append(" and fname like '%");
sql.append(condition.getBookName());
sql.append("%'");
}
if (condition.getAuthor() != null) {
sql.append(" and fauthor like '%");
sql.append(condition.getAuthor());
sql.append("%'");
}
if (condition.getDesc() != null) {
sql.append(" and fdesc like '%");
sql.append(condition.getDesc());
sql.append("%'");
}
if (condition.getMaxPrice() > 0) {
sql.append(" and fprice <");
sql.append(condition.getMaxPrice());
}
if (condition.getMinPrice() > 0) {
sql.append(" and fprice >");
sql.append(condition.getMinPrice());
}
System.out.println(sql.toString());
conn = super.getConn();
Book book = null;
try {
smt = conn.createStatement();
rs = smt.executeQuery(sql.toString());
while (rs.next()) {
book = new Book();
book.setId(rs.getInt(1));
book.setName(rs.getString(2));
book.setPrice(rs.getDouble(3));
book.setDesc(rs.getString(4));
book.setImg(rs.getString(5));
book.setAuthor(rs.getString(6));
book.setNumber(rs.getInt(7));
books.add(book);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(books);
return books;
}
给你参考下,查书的条件
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |