您的位置:首页 > 其它

网上商城—管理员增加商品

2015-04-26 14:49 253 查看
管理员增加商品(图书、服装、电器、零食)

先上本人的项目相应图片:






功能描述:

当以管理员的身份登录时,页面跳转到相应的管理商铺(shopkeeper.jsp)的页面(有关管理员和普通用户身份的验证请参看本人的其他博客),点击“增加图书、增加服装、增加电器、增加零食”都会跳转到填写要增加的商品信息的addproduct.jsp页面,在该页面的form表单中填写要增加的商品的信息,然后点击“增加”按钮将相应的信息提交给form表单对应的action“addProduct.do(AddProductServlet)”,在AddProductServlet中获取表单提交的信息,调用*DaoImpl的add()方法进行插入数据库的操作,然后进行页面跳转,如若添加成功跳转到ok.jsp,否则跳转到fail.jap。

实现类:

shopkeeper.jsp

addproduct.jsp(添加商品信息页)

ok.jsp(提示操作成功)

failjsp(提示操作失败)

AddProductServlet.java(获得添加的商品信息,调用数据库操作并进行页面跳转)

BookDaoImpl.java(接口BookDao.java)(定义一个方法实现将要添加的图书插入数据库的操作)

ClothesDaoImpl.java(接口ClothesDao.java)(定义一个方法实现将要添加的服装插入数据库的操作)

ElectricDaoImpl.java(接口ElectricDao.java)(定义一个方法实现将要添加的电器插入数据库的操作)

SnacksDaoImpl.java(接口SnacksDao.java)(定义一个方法实现将要添加的零食插入数据库的操作)

注意:

1、要添加四种商品,而我只定义了一个AddProductServlet,那么怎么判断要添加的商品是哪种类别呢?

在我的添加商品信息(addproduct.jsp)的页面中有四个按钮“增加图书、增加服装、增加电器、增加零食”,当你要增加某种商品时只需点击相应的增加按钮即可,这里我用到了按钮的name=” submit “属性和value=” 增加* “属性;当点击按钮将表单提交给addProduct.do之后,servlet会通过String submit = requst.getParameter(“submit”);方法获取到按钮的value属性的值,然后进行判断submit.contains(“圖書),通过判断结果来调用相应的数据库插入方法。还有一种方法:当点击按钮将带有要添加的商品信息的form表单提交给addProduct.do时传回一个参数,例如ddd=i,然后在servlet中利用ddd的值来进行相应的调用。

2、在添加数据库时有关图片的上传

,修改form表单的enctype=”multipart/form-data”(post提交方式上传字节型的大对象),上传相应的内容到数据库,在数据库表中承载该image的应该是一个Blob类型的大对象,该操作应该是这样实现的,可是我之前在创建数据库表的时候有关image使用的是一个图片的地址imgURL,相应的图片存放在项目的WebRoot/images下,所以这里要实现以上操作该动太大,关于这个问题我的解决是(求不嘲笑啊-_-||):将要添加的图片手动放到WebRoot/images下,然后上传数据库时只给出相应图片的URL,同时也没有用到文件上传。以后开始一个项目创建数据库表什么之类的之前一定要考虑全面啊啊!

代码:

shopkeeper.jsp

[code]<div class="main">
        <d1>
        <h1>增加商品</h1>
            <dt><a href="addproduct.jsp">增加图书</a></dt>
            <dt><a href="addproduct.jsp">增加服装</a></dt>
            <dt><a href="addproduct.jsp">增加零食</a></dt>
            <dt><a href="addproduct.jsp">增加电器</a></dt>
        </d1>
</div>


addproduct.jsp(添加商品信息页)

[code]<form method="post" action="addProduct.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>


AddProductServlet.java(获得添加的商品信息,调用数据库操作并进行页面跳转)

[code]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 AddProductServlet 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");
        System.out.println(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");

        System.out.println(id + name + title + manufacture + count + 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.addBook(product);
            request.getRequestDispatcher("addproduct.jsp").forward(request, response);
        }else if (submit.contains("服裝")) {//增加服裝
            //将用户输入的信息添加到数据库並進行頁面跳轉
            ClothesDao clothesDao=new ClothesDaoImpl();
            clothesDao.addClothes(product);
            request.getRequestDispatcher("addproduct.jsp").forward(request, response);
        }else if (submit.contains("電器")) {//增加電器
            //将用户输入的信息添加到数据库並進行頁面跳轉
            ElectricDao productDao=new ElectricDaoImpl();
            productDao.addElectric(product);
            request.getRequestDispatcher("addproduct.jsp").forward(request, response);
        }else if (submit.contains("零食")){//增加零食m 
            //将用户输入的信息添加到数据库並進行頁面跳轉
            SnacksDao productDao=new SnacksDaoImpl();
            productDao.addSnack(product);
            request.getRequestDispatcher("addproduct.jsp").forward(request, response);
        }

    }

}


BookDaoImpl.java(接口BookDao.java)(定义一个方法实现将要添加的图书插入数据库的操作)

(本文只给出addBook()方法,addClothes()、addElectric()、addSnack()只有sql语句中执行的表不一样,其余都一样)

[code]public class BooksDaoImpl implements BooksDao {

    @Override
    public String addBook(Product product) {

        Connection connection = DBUtil.getConnection();
        PreparedStatement preparedStatement = null; 
        try {
            String sql = "insert into books(id,imgURL,title,price,name,manufacture,counts) values(?,?,?,?,?,?,?);";

            preparedStatement = connection.prepareStatement(sql);

            //綁定參數
            preparedStatement.setInt(1,product.getId());
            preparedStatement.setString(2, "'"+product.getImgURL()+"'");
            preparedStatement.setString(3,"'"+ product.getTitle()+"'");
            preparedStatement.setDouble(4, product.getPrice());
            preparedStatement.setString(5,"'"+ product.getName()+"'");
            preparedStatement.setString(6,"'"+ product.getManufacture()+"'");
            preparedStatement.setInt(7,product.getCounts());
            //執行sql語句
            preparedStatement.executeUpdate();      
        } catch (SQLException e) {
            throw new DataBaseException();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
        return "ok";
    }
}


写博客会上瘾……
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: