管理员修改商品(图书、服装、电器、零食)
先上本人的项目相应图片:
(与网上商城—管理员增加商品http://blog.csdn.net/lmb55/article/details/45288321 类似)
功能描述:
当以管理员的身份登录时,页面跳转到相应的管理商铺(shopkeeper.jsp)的页面(有关管理员和普通用户身份的验证请参看本人的其他博客),点击“修改图书、修改服装、修改电器、修改零食”都会跳转到填写要增加的商品信息的updateproduct.jsp页面,在该页面的form表单中填写要修改的商品的信息,然后点击“修改X”按钮将相应的信息提交给form表单对应的action“updateProduct.do(UpdateProductServlet)”,在UpdateProductServlet中获取表单提交的信息,调用*DaoImpl的updateX()方法进行修改数据库数据的操作,然后进行页面跳转,如若修改成功跳转到ok.jsp,否则跳转到fail.jap。
实现类:
shopkeeper.jsp
updateproduct.jsp(修改商品信息页)
ok.jsp(提示操作成功)
failjsp(提示操作失败)
UpdateProductServlet.java(获得要修改的商品信息,调用数据库操作并进行页面跳转)
BookDaoImpl.java(接口BookDao.java)(定义一个方法实现将要修改的图书记录在数据库中进行修改)
ClothesDaoImpl.java(接口ClothesDao.java)(定义一个方法实现将要修改的服装记录在数据库中进行修改)
ElectricDaoImpl.java(接口ElectricDao.java)(定义一个方法实现将要修改的电器记录在数据库中进行修改)
SnacksDaoImpl.java(接口SnacksDao.java)(定义一个方法实现将要修改的零食记录在数据库中进行修改)
代码:
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">商品数量:</td>
<td><input class="text" type="text" name="count" /></td>
</tr>
<tr>
<td class="field">商品价格:</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">商品标题:</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(获得要修改的商品信息,调用数据库操作并进行页面跳转)
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");
//将从前台表单中获取得值放进product对象中
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的值並調用相應的方法
if (submit.contains("圖書")) {//增加圖書
//将用户输入的信息添加到数据库並進行頁面跳轉
System.out.println("進入增加圖書的方法……");
BooksDao booktDao=new BooksDaoImpl();
booktDao.updateBook(product);
request.getRequestDispatcher("updateproduct.jsp").forward(request, response);
}else if (submit.contains("服裝")) {//增加服裝
//将用户输入的信息添加到数据库並進行頁面跳轉
ClothesDao clothesDao=new ClothesDaoImpl();
clothesDao.updateClothes(product);
request.getRequestDispatcher("updateproduct.jsp").forward(request, response);
}else if (submit.contains("電器")) {//增加電器
//将用户输入的信息添加到数据库並進行頁面跳轉
ElectricDao productDao=new ElectricDaoImpl();
productDao.updateElectric(product);
request.getRequestDispatcher("updateproduct.jsp").forward(request, response);
}else if (submit.contains("零食")){//增加零食m
//将用户输入的信息添加到数据库並進行頁面跳轉
SnacksDao productDao=new SnacksDaoImpl();
productDao.updateSnack(product);
request.getRequestDispatcher("updateproduct.jsp").forward(request, response);
}
}
}
BookDaoImpl.java(接口BookDao.java)(定义一个方法实现将要修改的图书记录在数据库中进行修改)
(本文只给出updateBook()方法,updateClothes()、updateElectric()、updateSnack()只有sql语句中执行的表不一样,其余都一样)
【在绑定参数时要注意各参数的顺序】
@Override
public 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);
//綁定參數
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());
//執行sql語句
preparedStatement.executeUpdate();
} catch (SQLException e) {
throw new DataBaseException();
} catch (Exception e) {
e.printStackTrace();
} finally {
}
return "ok";
}
写博客会上瘾……