日期:2014-05-17  浏览次数:20801 次

求各位大侠的帮忙,。比较急。
BookDao:
public List<Book> Select(String value) {
Connection conn=DataBaseUtil.getConnection();
String sql="select * from book where bname like bname='%"+value+"%' or bauthor='%"+value+"%'";
PreparedStatement ps=null;
ResultSet rs=null;
List<Book> book=new ArrayList<Book>();

try{
ps=conn.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()){
Book oneBook=new Book();
oneBook.setBid(rs.getString("bid"));
oneBook.setBname(rs.getString("bname"));
oneBook.setBpress(rs.getString("bpress"));
oneBook.setBauthor(rs.getString("author"));
oneBook.setBnumber(rs.getInt("bnumber"));
book.add(oneBook);
}
rs.close();
ps.close();
conn.close();
}catch(Exception e){
e.printStackTrace();
}
return book;
}
SelectServlet:

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html,charset=gbk");
String name=request.getParameter("name");
BookDao book=new BookDao();
List<Book> books= book.Select(name);
if(books!=null){
request.setAttribute("info", books);
System.out.println("123");
}
else{
request.setAttribute("info", "不存在该图书");
request.getRequestDispatcher("message.jsp").forward(request,response);
}
}
建立一个jsp页面,根据输入的值可以查询到相关的图书信息,并且可以显示出来,我该如何编写SelectServlet或BookDao。

------解决方案--------------------
你的代码应该是没问题的。你运行后什么结果?

java高手群 群号:224651178,欢迎楼主