logo资料库

myEclipse购物车后台的商品修改.docx

第1页 / 共23页
第2页 / 共23页
第3页 / 共23页
第4页 / 共23页
第5页 / 共23页
第6页 / 共23页
第7页 / 共23页
第8页 / 共23页
资料共23页,剩余部分请下载后查看
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 try {fis.close();}catch(Exception try {conn.close();}catch(Exception } return mark; e){} e){} e){} }
//修改商品信息 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()); goods.getCname()); goods.getCprice()); goods.getCimage()); goods.getCid()); preparedStatement.setString(1, preparedStatement.setFloat(2, preparedStatement.setString(3, preparedStatement.setString(4,
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(indexend) break;
tmpCid=rs.getString(1); tmpCname=rs.getString(2); tmpCimage=rs.getString(3); tmpCprice=rs.getFloat(5); in=rs.getBinaryStream(6); String String String float //InputStream //写入到 JavaBean 中 GoodsBean g=new GoodsBean(); g.setCid(tmpCid); g.setCname(tmpCname); g.setCimage(tmpCimage); g.setCprice(tmpCprice); goodslist.add(g); } }catch(Exception e){
分享到:
收藏