qq_31045649 2015-09-11 15:07 采纳率: 50%
浏览 2132
已采纳

用servlet更新mysql数据不成功,没有报错,只是无法更新~求大神指点

这是更新的函数,在productdaoimpl类里:
public int updateProduct(Product product){
int flag = 0;
DBConnect conn = null;
PreparedStatement pstmt = null;

//update statement
    String sql = "update productinfo set productnumber=?,productvalue=?  where productname=?";

    try {

//connect DB
    conn = new DBConnect() ;
    pstmt = conn.getConnection().prepareStatement(sql) ;
    pstmt.setString(1, product.getProductnumber());
    pstmt.setString(2, product.getProductvalue());
    pstmt.setString(3, product.getProductname());
//update
    pstmt.executeUpdate();

    flag = 1;
    pstmt.close();

    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    conn.close();
    }
    return flag;

}

updateServlet的代码:
package jspservlet.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import jspservlet.dao.ProductDAO;
import jspservlet.dao.impl.ProductDAOImpl;
import jspservlet.vo.Product;

public class ProductUpdateServlet extends HttpServlet {

 public void doGet(HttpServletRequest req, HttpServletResponse res)
    throws IOException, ServletException{

     this.doPost(req, res);

 }

 public void doPost(HttpServletRequest req, HttpServletResponse res)
            throws IOException, ServletException{

        Product product = new Product();
        product.setProductname(req.getParameter("productname"));

        ProductDAO dao=new ProductDAOImpl();
        int flag = 0;
        int flag2 = 1;
        try {
            flag2 = dao.queryByProductName(product);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (flag2 == 1) {
            res.sendRedirect("./errorRegister.jsp");
        } else {

        try {
            flag = dao.updateProduct(product);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if( flag == 1) {
            //session是干嘛的?
            HttpSession session = req.getSession();
            session.setAttribute("productname", product.getProductname());
            res.sendRedirect("./success.jsp");
        } else {
            res.sendRedirect("./error.jsp");
        }

         }
 } 

}

JSP页面:

<%@ page language="java" pageEncoding="GB18030"%>



Delete Product





the name of the product you want to update:

    <form method="post" action="./update"> 
        productname : <input type="text" name="productname"/><br/>
        <p>the number and price of the product you want to change to:</p>
        productnumber : <input type="text" name="productnumber"/><br/>
        productvalue : <input type="text" name="productvalue"/><br/>        
        <input type="SUBMIT" name="submit" value="Submit"> 
    </form>

</body>

  • 写回答

7条回答

  • qq_31045649 2015-09-14 03:09
    关注

    我找到答案了,是没有获取页面输入导致的

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?