javaweb工程的一个功能无法实现请求帮忙


在写一个类似于校园图书馆的网页工程,在还书功能那里遇到麻烦了
用的是mysql5.6+myeclipse10 其他功能都运行良好
在DAO层
/ 还书 /


 public void backBook(int bkid, int stid) throws Exception{
    // TODO Auto-generated method stub
    /*删除记录*/
    String sql = "delete record where bookid = " + bkid + " and studentid = " + stid;
    this.openconnection();
    this.conn.setAutoCommit(false);
    Statement st = conn.createStatement();

    st.executeUpdate(sql);
    /*把书的数量+1*/
    int oldnumber = 0;
    String sql2 = "select booknumber from book where bookid = " + bkid;
    PreparedStatement st2 = conn.prepareStatement(sql2);
    ResultSet rs = st2.executeQuery();
    if (rs.next())
        oldnumber = rs.getInt("booknumber");
    int newnumber = oldnumber + 1;
    String sql3= "update book set booknumber = " + newnumber + " where bookid = " + bkid;
    Statement st3 = conn.createStatement();
    st3.executeUpdate(sql3);
    this.closeResource();


}

在BIZ层

public void backBook(int bkid, int stid) throws Exception{


 // TODO Auto-generated method stub
    BookDao bkd = new BookDao();
    bkd.backBook(bkid, stid);
}

SEVLET里


 public void service(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String bookid = request.getParameter("bookid");
    String studentid = request.getParameter("studentid");

    int bkid = Integer.parseInt(bookid);
    int stid = Integer.parseInt(studentid);
    BookBiz bkb = new BookBiz();
    try{
        bkb.backBook(bkid, stid);
        request.setAttribute("errMsg", "还书成功!");
        request.getRequestDispatcher("error.jsp").forward(request, response);
    }catch(Exception e){
        Log.logger.error(e.getMessage());
        request.setAttribute("errMsg", "还书失败,请稍后再试!");
        request.getRequestDispatcher("error.jsp").forward(request, response);
    }

}

运行的结果是,还书失败请稍后再试

但是没有报错,已经不知道如何改,求帮助

myeclipse java mysql tomcat sql

3DM06 9 years, 3 months ago

一步一步debug 首先 int bkid = Integer.parseInt(bookid);int stid = Integer.parseInt(studentid);这两个地方有存在空指针或者是format exception 然后Log.logger.error(e.getMessage());这个地方 e.getMessage()这个不一定有值的,你如果想看全部的异常 你就直接把异常的堆栈打出来 应该就可以看到了 e.printStackTrace();

Ticshot answered 9 years, 3 months ago


 String sql = "delete from record where bookid = " + bkid + " and studentid = " + stid;

ziseed answered 9 years, 3 months ago

这种问题没有好的解决办法。

只能自己加断点, debug .先看看从页面上获取到的这两个参数是不是正常的。


 int bkid = Integer.parseInt(bookid);
int stid = Integer.parseInt(studentid);

如果不是正常的,请在页面上查找。

如果是正常的,请跟踪这个函数 bkb.backBook(bkid, stid);

然后在这个函数里面 打印你的具体的 sql 是什么?

用这个 sql 语句和参数直接去数据库执行,看是否有正常的结果。

凤凰院伦太郎 answered 9 years, 3 months ago

Your Answer