当前位置: 代码迷 >> Java Web开发 >> myeclipse中实现购物车后台的商品修改功能 使用jsp和servlet完成
  详细解决方案

myeclipse中实现购物车后台的商品修改功能 使用jsp和servlet完成

热度:386   发布时间:2011-06-22 14:20:48.0
myeclipse中实现购物车后台的商品修改功能 使用jsp和servlet完成
GoodsBean:
package shop;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import bean.DBBean2;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.*;
import java.util.ArrayList;
public class GoodsBean {
    private String cid;
    private String cname;
    private String cimage;
    private int cnumber;
    private float cprice;
    private String cintro;
   
   
    public void setCid(String cid){
        this.cid=cid;
    }
    public String getCid(){
        return this.cid;
    }
   
    public void setCname(String cname){
        this.cname=cname;
    }
    public String getCname(){
        return this.cname;
    }
   
    public void setCimage(String cimage){
        this.cimage=cimage;
        
    }
    public String getCimage(){
        return this.cimage;
    }
   

   
    public void setCnumber(int cnumber){
        this.cnumber=cnumber;
    }
    public int getCnumber(){
        return this.cnumber;
    }
   
    public void setCprice(float cprice){
        this.cprice=cprice;
    }
    public float getCprice(){
        return this.cprice;
    }
   
    public void setCintro(String cintro){
        this.cintro=cintro;
    }
    public String getCintro(){
        return this.cintro;
    }

    public boolean add() throws Exception{
        boolean mark=false;
        DBBean2 db=new DBBean2();
        
        File file=new File(cimage);
        float l1=file.length();
        int l2=(int)l1;
//        FileInputStream fis=new FileInputStream(file);     
        
        InputStream fis=new FileInputStream(file);
        
        String sql="insert into goods values (?,?,?,?,?,?)";
        PreparedStatement pstmt=null;
        Connection conn=null;
        
        try{
            conn=db.createConn();
            pstmt=conn.prepareStatement(sql);            
            pstmt.setString(1,cid);
            pstmt.setString(2,cname);
            pstmt.setInt(3,cnumber);
            pstmt.setFloat(4,cprice);
            pstmt.setString(5,cintro);
            pstmt.setBinaryStream(6,fis,l2);
            mark=pstmt.execute();
        }catch(Exception e){
            System.out.println(e.toString());
        }finally{
            try {pstmt.close();}catch(Exception e){}
            try {fis.close();}catch(Exception e){}
            try {conn.close();}catch(Exception e){}
        }
        return mark;
        
    }
   
   
    //修改商品信息
    public void modifyGoods(GoodsBean goods){
        DBBean2 db=new DBBean2();
        PreparedStatement pstmt=null;
        Connection conn=null;
        try{
            conn=db.createConn();
            PreparedStatement preparedStatement=conn.prepareStatement("update goods set cname=?,cprice=?,Image=? where cid=?");
            //preparedStatement.setString(1, goods.getGoodID());
            preparedStatement.setString(1, goods.getCname());
            preparedStatement.setFloat(2, goods.getCprice());
            preparedStatement.setString(3, goods.getCimage());
            preparedStatement.setString(4, goods.getCid());
        
            preparedStatement.executeUpdate();
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    //获取所有的商品信息
    public ArrayList getGoodsByPage(int pageNo){
        ArrayList goodslist=new ArrayList();
        DBBean2 db=new DBBean2();
        int begin=(pageNo-1)*5+1;
        int end=pageNo*5;
        int index=0;
        String sql="select * from goods";
        ResultSet rs=null;
        try{
            rs=db.executeQuery(sql);
            while(rs.next()){
                index++;
                if(index<begin) continue;
                if(index>end) break;
                String tmpCid=rs.getString(1);
                String tmpCname=rs.getString(2);
                String tmpCimage=rs.getString(3);
                float tmpCprice=rs.getFloat(5);
               
                //InputStream in=rs.getBinaryStream(6);
               
                //写入到JavaBean中
                GoodsBean g=new GoodsBean();
                g.setCid(tmpCid);
                g.setCname(tmpCname);
                g.setCimage(tmpCimage);
                g.setCprice(tmpCprice);
                goodslist.add(g);               
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            db.close();
        }
        return goodslist;
    }
    //获取页码总数
    public int getPageCount(){
        DBBean2 db=new DBBean2();
        String sql="select count(*) from goods";
        ResultSet rs=null;
        int pageCount=1;
        try{
            rs=db.executeQuery(sql);
            rs.next();
            int n=rs.getInt(1);
            pageCount=(int)((n-1)/5+1);
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            db.close();
        }
        return pageCount;
    }
    // 根据商品号获得商品信息
    public GoodsBean getGoodsByGoodsId(String s) {
        GoodsBean goods = new GoodsBean();
        DBBean2 db=new DBBean2();
        PreparedStatement pstmt=null;
        Connection conn=null;
        try {
            conn=db.createConn();
            Statement statement = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
            ResultSet resultSet = statement.executeQuery("select * from goods where cid ='" + s+ "'");
            while (resultSet.next()) {
                String i =resultSet.getString(1);
                goods.setCid(i);
                goods.setCname(resultSet.getString(2));
                goods.setCimage(resultSet.getString(3));
                goods.setCnumber(resultSet.getInt(4));
                goods.setCprice(resultSet.getFloat(5));
                }
            statement.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return goods;
    }
   
    //根据商品名称获得商品信息
    public Collection getGoodsByGoodName(String s){
        GoodsBean goods;
        ArrayList arrayList=new ArrayList();
        DBBean2 db=new DBBean2();
        PreparedStatement pstmt=null;
        Connection conn=null;
        try {
            Statement statement = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
            ResultSet resultSet = statement.executeQuery("select * from goods where cname ='" + s+ "'");
            while (resultSet.next()) {
                goods = new GoodsBean();
                goods.setCid(resultSet.getString(1));
                goods.setCname(resultSet.getString(2));
                goods.setCimage(resultSet.getString(3));
                goods.setCnumber(resultSet.getInt(4));
                goods.setCprice(resultSet.getFloat(5));
                arrayList.add(goods);
            }
            statement.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return arrayList;
    }
   
    public GoodsBean findGoodsById(String cid){        
        try{
            String sql="select * from goods where cid = '"+cid+"'";
            DBBean2 db=new DBBean2();
            ResultSet rs=null;
            rs=db.executeQuery(sql);
            if(rs.next()){
                String cname=rs.getString(2);
                float cprice=rs.getFloat(5);
                GoodsBean temp=new GoodsBean();
                temp.setCname(cname);
                temp.setCid(cid);
                temp.setCprice(cprice);
                db.close();
                return temp;
            }else{
                return null;
            }
        }catch(Exception e){
            System.out.print(e.toString());
            return null;
        }
        
        
    }
   
}



ModifyGoods.jsp


<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%@page import="shop.GoodsBean"%>
<%@ page import="bean.DBBean2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script language=javascript>
function checkinfo(){
    if(checkspace(document.modify.cname.value)){
        document.modify.cname.focus();
        alert("对不起,请填写商品名!");
        return false;
    }
    if(checkspace(document.modify.cprice.value)){
        document.modify.cprice.focus();
        alert("对不起,请填写价格!");
        return false;
    }
   
}
    function checkspace(checkstr){
    var str='';
    for(i=0;i<checkstr.length;i++){
        str=str+'';
    }
    return (str==checkstr);
}

</script>
</head>

<body >
<a href="index.jsp">返回首页</a>    &nbsp;&nbsp;
<a href="maneger.jsp">返回管理界面</a>
<div>
<%
    String cid=request.getParameter("cid").toString();
    GoodsBean goodB=new GoodsBean();
    goodB=goodB.getGoodsByGoodsId(cid);        
    session.setAttribute("cid",cid);
%>
<form id="modify" name="modify" action="ModifyGoods" action="post">
<table width="412"  border="1">
  <tr>
    <td >&nbsp;编号:</td>
    <td>&nbsp;<%=goodB.getCid()  %></td>
  </tr><tr>
    <td>&nbsp;名称:</td>
    <td>&nbsp;<input name="goodName" type="text" value="<%=goodB.getCname() %>"/></td>
  </tr><tr>
    <td>&nbsp;价格:</td>
    <td>&nbsp;<input name="goodPrice" type="text" value="<%=goodB.getCprice() %>"/></td>
  </tr><tr>
      <td>&nbsp;选择商品图片</td>
      <td>&nbsp;<input name="image" type="file" /></td>
  </tr><tr>
      <td><input type="submit" name="submit1" value="提交" onclick="return checkinfo()"/></td>
  </tr>
</table>
</form>
</div>


</body>
</html>



ModifGoodsServlet:

package shop;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.io.IOException;
import java.io.PrintWriter;

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 com.jspsmart.upload.SmartUpload;

import bean.DBBean2;
import bean.ZhuanHuan;
import shop.GoodsBean;
import bean.CopyFile;
public class ModifyGoods extends HttpServlet {

    /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     *
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out
                .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
        out.println("<HTML>");
        out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
        out.println("  <BODY>");
        out.print("    This is ");
        out.print(this.getClass());
        out.println(", using the GET method");
        out.println("  </BODY>");
        out.println("</HTML>");
        out.flush();
        out.close();
    }

    /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     *
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        
        Calendar calendar=Calendar.getInstance();
        response.setContentType("text/html");
        HttpSession session=request.getSession();//
        String cid=(String)session.getAttribute("cid");//
        GoodsBean goodB=new GoodsBean();
        String cname=ZhuanHuan.toChinese(request.getParameter("cname"));
        float cprice=Float.parseFloat(request.getParameter("cprice"));
        String image=request.getParameter("image");
        //把图片提取出来存放到以下路径
        String imageSort=image.substring(image.lastIndexOf(".")-1);
        String dest="H:/jspex/jspclass/WebRoot/image/"+String.valueOf(calendar.getTimeInMillis())+imageSort;
        goodB.setCid(cid);
        goodB.setCname(cname);
        goodB.setCprice(cprice);
        goodB.setCimage(dest);
        CopyFile cp=new CopyFile();
        cp.copyFile(image, dest);
        goodB.modifyGoods(goodB);
        response.sendRedirect("showgoods.jsp");
   
   
}

/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
    // Put your code here
}

}


搜索更多相关主题的帖子: servlet  购物车  private  package  public  

----------------解决方案--------------------------------------------------------
购物车功能,怎么在MyEclipse中修改商品信息,以上是我的GoodsBean,ModifyGoods.jsp,ModifyGoodsServlet  可是在运行时老报错,请高手们指正!!!
----------------解决方案--------------------------------------------------------
  相关解决方案