日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

网上商城—管理员修改商品

發(fā)布時(shí)間:2024/4/14 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 网上商城—管理员修改商品 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

管理員修改商品(圖書、服裝、電器、零食)

先上本人的項(xiàng)目相應(yīng)圖片:

(與網(wǎng)上商城—管理員增加商品http://blog.csdn.net/lmb55/article/details/45288321 類似)

功能描述:

當(dāng)以管理員的身份登錄時(shí),頁面跳轉(zhuǎn)到相應(yīng)的管理商鋪(shopkeeper.jsp)的頁面(有關(guān)管理員和普通用戶身份的驗(yàn)證請(qǐng)參看本人的其他博客),點(diǎn)擊“修改圖書、修改服裝、修改電器、修改零食”都會(huì)跳轉(zhuǎn)到填寫要增加的商品信息的updateproduct.jsp頁面,在該頁面的form表單中填寫要修改的商品的信息,然后點(diǎn)擊“修改X”按鈕將相應(yīng)的信息提交給form表單對(duì)應(yīng)的action“updateProduct.do(UpdateProductServlet)”,在UpdateProductServlet中獲取表單提交的信息,調(diào)用*DaoImpl的updateX()方法進(jìn)行修改數(shù)據(jù)庫數(shù)據(jù)的操作,然后進(jìn)行頁面跳轉(zhuǎn),如若修改成功跳轉(zhuǎn)到ok.jsp,否則跳轉(zhuǎn)到fail.jap。

實(shí)現(xiàn)類:

shopkeeper.jsp
updateproduct.jsp(修改商品信息頁)
ok.jsp(提示操作成功)
failjsp(提示操作失敗)
UpdateProductServlet.java(獲得要修改的商品信息,調(diào)用數(shù)據(jù)庫操作并進(jìn)行頁面跳轉(zhuǎn))
BookDaoImpl.java(接口BookDao.java)(定義一個(gè)方法實(shí)現(xiàn)將要修改的圖書記錄在數(shù)據(jù)庫中進(jìn)行修改)
ClothesDaoImpl.java(接口ClothesDao.java)(定義一個(gè)方法實(shí)現(xiàn)將要修改的服裝記錄在數(shù)據(jù)庫中進(jìn)行修改)
ElectricDaoImpl.java(接口ElectricDao.java)(定義一個(gè)方法實(shí)現(xiàn)將要修改的電器記錄在數(shù)據(jù)庫中進(jìn)行修改)
SnacksDaoImpl.java(接口SnacksDao.java)(定義一個(gè)方法實(shí)現(xiàn)將要修改的零食記錄在數(shù)據(jù)庫中進(jìn)行修改)

代碼:
shopkeeper.jsp略
updateproduct.jsp(修改商品信息頁)

<form method="post" action="updateProduct.do" ><table><tr><td class="field">商品ID:</td><td><input class="text" type="text" name="id" /></td></tr><tr><td class="field">商品名稱:</td><td><input class="text" type="text" name="name" /></td></tr><tr><td class="field">商品數(shù)量:</td><td><input class="text" type="text" name="count" /></td></tr><tr><td class="field">商品價(jià)格:</td><td><input class="text" type="text" name="price" /></td></tr><tr><td class="field">商品圖片:</td><td><input class="text" type="text" name="imgURL"/>格式:images/*.jpg</td></tr><tr><td class="field">商品標(biāo)題:</td><td><textarea name="title"></textarea></td></tr><tr><td class="field">商品制造商:</td><td><textarea name="manufacture"></textarea></td></tr><tr><td><label class="ui-blue"><input type="submit" name="submit" value="修改圖書" /></label></td><td><label class="ui-blue"><input type="submit" name="submit" value="修改服裝" /></label></td><td><label class="ui-blue"><input type="submit" name="submit" value="修改電器" /></label></td><td><label class="ui-blue"><input type="submit" name="submit" value="修改零食" /></label></td></tr> </table></form>

UpdateProductServlet.java(獲得要修改的商品信息,調(diào)用數(shù)據(jù)庫操作并進(jìn)行頁面跳轉(zhuǎn))

import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.ymw.dao.BooksDao; import com.ymw.dao.BooksDaoImpl; import com.ymw.dao.ClothesDao; import com.ymw.dao.ClothesDaoImpl; import com.ymw.dao.ElectricDao; import com.ymw.dao.ElectricDaoImpl; import com.ymw.dao.SnacksDao; import com.ymw.dao.SnacksDaoImpl; import com.ymw.domain.Product;public class UpdateProductServlet extends HttpServlet {private static final long serialVersionUID = 1L;public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doPost(request, response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {request.setCharacterEncoding("utf-8");Product product=new Product();//獲取addproduct.jsp表單中用戶輸入的信息String submit=request.getParameter("submit");String id=request.getParameter("id");String name=request.getParameter("name");String title=request.getParameter("title");String imgURL=request.getParameter("imgURL");String manufacture=request.getParameter("manufacture");String count=request.getParameter("count");String price=request.getParameter("price");//將從前臺(tái)表單中獲取得值放進(jìn)product對(duì)象中product.setId(Integer.parseInt(id));product.setName(name);product.setTitle(title);product.setImgURL(imgURL);product.setManufacture(manufacture);product.setCounts(Integer.parseInt(count));product.setPrice(Double.parseDouble(price));//判斷ddd的值並調(diào)用相應(yīng)的方法if (submit.contains("圖書")) {//增加圖書//將用戶輸入的信息添加到數(shù)據(jù)庫並進(jìn)行頁面跳轉(zhuǎn)System.out.println("進(jìn)入增加圖書的方法……");BooksDao booktDao=new BooksDaoImpl();booktDao.updateBook(product);request.getRequestDispatcher("updateproduct.jsp").forward(request, response);}else if (submit.contains("服裝")) {//增加服裝//將用戶輸入的信息添加到數(shù)據(jù)庫並進(jìn)行頁面跳轉(zhuǎn)ClothesDao clothesDao=new ClothesDaoImpl();clothesDao.updateClothes(product);request.getRequestDispatcher("updateproduct.jsp").forward(request, response);}else if (submit.contains("電器")) {//增加電器//將用戶輸入的信息添加到數(shù)據(jù)庫並進(jìn)行頁面跳轉(zhuǎn)ElectricDao productDao=new ElectricDaoImpl();productDao.updateElectric(product);request.getRequestDispatcher("updateproduct.jsp").forward(request, response);}else if (submit.contains("零食")){//增加零食m//將用戶輸入的信息添加到數(shù)據(jù)庫並進(jìn)行頁面跳轉(zhuǎn)SnacksDao productDao=new SnacksDaoImpl();productDao.updateSnack(product);request.getRequestDispatcher("updateproduct.jsp").forward(request, response);}}}

BookDaoImpl.java(接口BookDao.java)(定義一個(gè)方法實(shí)現(xiàn)將要修改的圖書記錄在數(shù)據(jù)庫中進(jìn)行修改)
(本文只給出updateBook()方法,updateClothes()、updateElectric()、updateSnack()只有sql語句中執(zhí)行的表不一樣,其余都一樣)
【在綁定參數(shù)時(shí)要注意各參數(shù)的順序】

@Overridepublic String updateBook(Product product) {Connection connection = DBUtil.getConnection();PreparedStatement preparedStatement = null;try {String sql = "update books set imgURL=?,title=?,price=?,name=?,manufacture=?,counts=? where id=?;";preparedStatement = connection.prepareStatement(sql);//綁定參數(shù)preparedStatement.setString(1, "'"+product.getImgURL()+"'");preparedStatement.setString(2,"'"+ product.getTitle()+"'");preparedStatement.setDouble(3, product.getPrice());preparedStatement.setString(4,"'"+ product.getName()+"'");preparedStatement.setString(5,"'"+ product.getManufacture()+"'");preparedStatement.setInt(6,product.getCounts());preparedStatement.setInt(7,product.getId());//執(zhí)行sql語句preparedStatement.executeUpdate();} catch (SQLException e) {throw new DataBaseException();} catch (Exception e) {e.printStackTrace();} finally {}return "ok";}

寫博客會(huì)上癮……

總結(jié)

以上是生活随笔為你收集整理的网上商城—管理员修改商品的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。