购物车功能:怎么在MyEclipse中修改商品信息,一下是我的GoodsBean。通过servlet和jsp实现改功能
GoodsBean,packageshop;importjava.sql.Connection;importjava.sql.PreparedStatement;impo...
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();
}
}
} 展开
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();
}
}
} 展开
2个回答
展开全部
你这样是不行的。("update goods set cname=?,cprice=?,Image=? where cid=?");
这里用?号就不像你前边插入那样能识别了。你可以写成("update goods set cname=‘"+goods.getGoodID()+"’cprice=’"+goods.getCname()+"‘Image=……where cid=’“+goods.getCid()+"‘“;
这样的格式。不然你这里的问号是不会把数据带进去的。你可以试下println出来你的那个update语句看看。好像是没有数据的还是数据库update需要’。反正你改成我说这样就可以了。‘?’这样的格式好像就不能把?的数据带进去了。我采用我上边说的方法的。没有问题。
下边是我程序里边的update
"update cunhuobiao set shengyu = "+"'"+cun.getShengyu()+"'"+" where id ="+"'"+cun.getID()+"'";
主要是把单引号加进去了。
这里用?号就不像你前边插入那样能识别了。你可以写成("update goods set cname=‘"+goods.getGoodID()+"’cprice=’"+goods.getCname()+"‘Image=……where cid=’“+goods.getCid()+"‘“;
这样的格式。不然你这里的问号是不会把数据带进去的。你可以试下println出来你的那个update语句看看。好像是没有数据的还是数据库update需要’。反正你改成我说这样就可以了。‘?’这样的格式好像就不能把?的数据带进去了。我采用我上边说的方法的。没有问题。
下边是我程序里边的update
"update cunhuobiao set shengyu = "+"'"+cun.getShengyu()+"'"+" where id ="+"'"+cun.getID()+"'";
主要是把单引号加进去了。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询