关于struts2按条件 模糊查询!!!!!!!!!!!!
我就不知道为什么我的keyword1 和keyword2 联合在一起做查询 查询不到结果,数据库里有的结果。用到一个select 下拉框。求各位帮看原因为!!!!!!!!!!!!!!
不多说,先上界面代码
<form name="fom" id="fom" method="post" action="Book-Search.action"><td width="538">按 <select name="keyword1"> <option>请选择</option> <option value="productID">图书产品号</option> <option value="categoryname">分类名</option> <option value="bookname">图书名</option> <option value="author">作者</option> <option value="publisher">出版社</option> </select> 搜索: <input type="text" name="keyword2" value="请输入关键字" /> <input name="Submit4" type="submit" class="right-button02" value="查 询" /></td> </form>
<s:iterator value="bms" id="b"> <tr bgcolor="#FFFFFF"> <td height="20"><input type="checkbox" name="delid"/></td> <td bgcolor="#FFFFFF"><s:property value="#b.productID"/></td> <td><s:property value="#b.categoryname"/></td> <td><s:property value="#b.bookname"/></td> <td><s:property value="#b.publisher"/></td> <td><s:property value="#b.publishdate"/></td> <td><s:property value="#b.author"/></td> <td><s:property value="¥#b.unitprice"/></td> <td bgcolor="#FFFFFF" align="center"> <a href="Book-UpdateInput.action?productID=<s:property value="#b.productID" />">编辑</a>| <!-- <a href="searchBook.jsp">查看该类别所有图书</a>| --> <a href="Book-Delete.action?productID=<s:property value="#b.productID"/>" onclick="return checkDel();">删除</a> </td> </tr></s:iterator>
private ABookService bs = new ABookService();//private ACategoryService cs = new ACategoryService();private List<BookModel> bms;private String keyword1;private String keyword2;//省略get set方法(keyword1 2 等等)//条件模糊查询public String findAll(){ try { bms = ABookService.findAll(keyword1, keyword2); System.out.println("!!!!!"+keyword1+keyword2); //bm = new BookModel(); } catch (Exception e) { e.printStackTrace(); } return SUCCESS; }
//模糊查询 public static List<BookModel> findAll(String keyword1, String keyword2) throws Exception{ Connection conn = ConnectionDB.getConn(); ArrayList<BookModel>all = new ArrayList<BookModel>(); String sql = "select * from bookInfo where ? = ?"; /*if(keyword1 == "productID"){ String sql="select * from bookInfo where productID like ? "; } else if(keyword1 == "bookname"){ String sql = "select * from bookInfo where bookname like ? "; }*/ PreparedStatement ps = ConnectionDB.prepare(conn, sql); //keyword = "100"; System.out.println("~~~~~~~~~~~~~~~~"+keyword1+keyword2); ps.setString(1,keyword1); ps.setString(2,keyword2); ResultSet rs = ps.executeQuery(); while(rs.next()){ BookModel bm= new BookModel(); bm.setProductID(rs.getString(1)); bm.setCategoryname(rs.getString(2)); bm.setBookname(rs.getString(3)); bm.setPublisher(rs.getString(4)); bm.setPublishdate(rs.getString(5)); bm.setAuthor(rs.getString(6)); bm.setUnitprice(rs.getString(7)); all.add(bm); System.out.println(bm.getAuthor()); } ConnectionDB.close(ps); ConnectionDB.close(conn); return all; }