This is an updated function. in the productdaoimpl class: publicintupdateProduct (Productproduct) {intflag0; DBConnectconnnull; PreparedStatementpstmtnull; updatestatementStringsql & quot; updateproductinfosetproductnumber ?, Productvalue? Whereproductname? Mysqlservlet
This is an updated function. in the productdaoimpl class:
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 code:
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 {fla G2 = 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) {// What Is session? HttpSession session = req. getSession (); session. setAttribute ("productname", product. getProductname (); res. sendRedirect (". /success. jsp ");} else {res. sendRedirect (". /error. jsp ");}}}
}
JSP page:
<% @ Page language = "java" pageEncoding = "GB18030" %>
Delete Product
The name of the product you want to update: