谁能给我看一下这个代码。。
public Book OrderBook(String bid,String rid,int num){
Connection conn=database.DataBaseUtil.getConnection();
String sql="select * from book where renumer>0 and renumer>num";
String osql="insert into borrow(bid,rid,borrownmb,btime,rtime) values(?,?,?,?,?)";
String nsql="update book set renumber=renumber-num";
PreparedStatement ps=null;
PreparedStatement pss=null;
PreparedStatement pstmt=null;
Book oneBook=new Book();
ResultSet rs=null;
Date MyDate=new Date();
try{
ps=conn.prepareStatement(sql);
pss=conn.prepareStatement(osql);
rs=ps.executeQuery();
if(rs.next()){
pss.setString(1,bid);
pss.setString(2,rid);
pss.setInt(3, num);
pss.setString(4,MyDate.toString() );
pss.setString(5, MyDate.toString()+30);
pss.executeUpdate();
oneBook.setBid(rs.getString("bid"));
oneBook.setBname(rs.getString("bname"));
oneBook.setBpress(rs.getString("bpress"));
oneBook.setBauthor(rs.getString("bauthor"));
oneBook.setBnumber(rs.getInt("bnumber"));
pstmt=conn.prepareStatement(nsql);
pstmt.executeUpdate();
return oneBook;
}
rs.close();
ps.close();
pss.close();
pstmt.close();
conn.close();
}catch(Exception e){
e.printStackTrace();
}
return null;
}
//servlet
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws
ServletException,
IOException {
response.setContentType("text/html;charset=gbk");
String name=request.getParameter("name");
String uname=request.getParameter("uname");
int num=Integer.parseInt((String)request.getParameter("number"));
Date MyDate=new Date();
BookDao book=new BookDao();
Book books= book.OrderBook(name,uname,num);
if(!("".equals(books.getBid()))||books.getBid()!=null){
request.setAttribute("number", MyDate.hashCode());
request.getSession().setAttribute("books", books.getBid());
request.getRequestDispatcher("ordersuccess.jsp").forward(request,response);
}
else{
request.setAttribute("info","数量不够");
request.getRequestDispatcher("orderfailure.jsp").forward(request, response);
}
}
------解决方案--------------------你最后有个return null;
那servlet中。。
Book books= book.OrderBook(name,uname,num);
要对books进行非空判断吧?
如果books为空,还怎么get值?
------解决方案--------------------把这块
if(!("".equals(books.getBid()))||books.getBid()!=null)
改一下,不然如果数据库没有查询出结果,就会报
空指针。你是学生吧。
改成:
if(books != null && books.getBid() != null && !"".equals(books.getBid()))