public int doAdd(HeroBean hero) throws Exception {
int rs=0;
List list=new ArrayList();
StringBuilder sb1=new StringBuilder("insert into hero(name,");
StringBuilder sb2=new StringBuilder("values(?,");
if(hero.getName()!=null&&hero.getName().length()>0){
list.add(hero.getName());
}
if(hero.getSex()!=null){
sb1.append("sex,");
sb2.append("?,");
list.add(hero.getSex());
}
if(hero.getType1()!=null){
sb1.append("type1,");
sb2.append("?,");
list.add(hero.getType1());
}
sb1.deleteCharAt(sb1.length()-1);
sb2.deleteCharAt(sb2.length()-1);
sb2.append(")");
sb1.append(")");
String sql=sb1.append(sb2).toString();
try{
PreparedStatement psmt=null;
conn=db.getConn();
psmt=conn.prepareStatement(sql);
for(int i=0;i<list.size();i++){
psmt.setObject(i,list.get(i));
}
rs=psmt.executeUpdate();
psmt.close();
}catch(Exception e){
throw e;
}finally{
if(conn!=null){
conn.close();
}
}
return rs;
}
这是我的代码,检查不出哪里错了,是JDBC连接的问题。
绿洲仙人球
相关分类