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

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

WBOY
发布: 2016-06-06 09:38:06
原创
1488 人浏览过

mysqlservlet

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

<code>//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;</code>
登录后复制

}

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 {

<code> 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");        }         } } </code>
登录后复制

}

JSP页面:



Delete Product





the name of the product you want to update:

<code>    <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></code>
登录后复制
相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板