rt以下是代码,代码中state为int类型,name为string类型,想实现分页查询,可是一点下一页或者选到某一页就会报错,sql语句也没有写错,在数据库中都能查到,可是就是报错,请大神帮忙看看到底怎么回事
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
XmBillService service=new XmBillServiceImpl();
int page=Integer.parseInt(request.getParameter("page"));
String name=request.getParameter("goodsname");
String state=request.getParameter("billstate");
if(name==null){
name ="";
}
if(state==null){
try {
int sum=service.findPageByGoodsName(name);
List<XmBill>list=service.findByGoodsName(name, page, 5);
int totalPage=0;
if(sum%5==0){
totalPage=sum/5;
}else{
totalPage=sum/5+1;
}
request.setAttribute("list", list);
request.setAttribute("name", name);
request.setAttribute("state", state);
request.setAttribute("page", page);
request.setAttribute("totalPage", totalPage);
request.getRequestDispatcher("admin_bill_list.jsp").forward(request, response);
System.out.println(page+";"+name+";"+state);
} catch (SQLException e) {
e.printStackTrace();
}
}else{
try {
int sum=service.findPageByGoodsNameAndBillState(name,Integer.parseInt(state));
List<XmBill>list=service.findByGoodsNameAndBillState(name, Integer.parseInt(state), page, 5);
int totalPage=0;
if(sum%5==0){
totalPage=sum/5;
}else{
totalPage=sum/5+1;
}
request.setAttribute("list", list);
request.setAttribute("name", name);
request.setAttribute("page", page);
request.setAttribute("totalPage", totalPage);
request.getRequestDispatcher("admin_bill_list.jsp").forward(request, response);
System.out.println(page+";"+name+";"+state);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
------解决思路----------------------
原因:String state=request.getParameter("billstate");
这行肯定是取到""值了,所以走到else里面了,Integer.parseInt(state)这行就报错了。
解决:if(state==null) --->if(state==null
------解决思路----------------------
"".equals(state))
加个判断就行了。