请求大侠优化一段代码

07-08-28 jxvae

for (int i = 0; i<temp; i++) {
    if(values[i] instanceof String){
  ps.setString(i+1,values[i].toString());
    }else if(values[i] instanceof Integer){
  ps.setInt(i+1,new Integer(values[i].toString()));
    }else if(values[i] instanceof Short){
  ps.setShort(i+1,new Short(values[i].toString()));
    }else if(values[i] instanceof Long){
  ps.setLong(i+1,new Long(values[i].toString()));
    }else if(values[i] instanceof Date){
  ps.setDate(i+1,Date.valueOf(values[i].toString()));
    }else if(values[i] instanceof Double){
  ps.setDouble(i+1,new Double(values[i].toString()));
    }else if(values[i] instanceof Timestamp){
  ps.setTimestamp(i+1,Timestamp.valueOf(values[i].toString()));
    }else if(values[i] instanceof Time){
  ps.setTime(i+1,Time.valueOf(values[i].toString()));
    }else if(values[i] instanceof BigDecimal){
  ps.setBigDecimal(i+1,new BigDecimal(values[i].toString()));
    }else if(values[i] instanceof Blob){
  ps.setBlob(i+1,(Blob)values[i]);
    }else if(values[i] instanceof Clob){
  ps.setClob(i+1,(Clob)values[i]);
    }
}

这代码里的if else使用太多了,总觉得效率不高,有哪位大侠帮俺给优化一下啊

[该贴被jxvae于2007年08月28日 17:54修改过]

[该贴被jxvae于2007年08月28日 17:54修改过]

[该贴被jxvae于2007年08月28日 17:54修改过]

banq
2007-08-30 10:34

需要详细的业务场景,要重新设计整个实现思路。

否则只是代码,不会变魔术。

jxva
2007-09-22 11:02

是一个DAO的插入记录方法

具体的代码如下:

public void insert(Object obj) {

StringBuffer sql=new StringBuffer();

StringBuffer name_array=new StringBuffer();

StringBuffer value_array=new StringBuffer();

try {

Class<? extends Object> voClass = obj.getClass();

sql.append("insert into "+TableUtil.getTbname(voClass.getSimpleName()).toLowerCase()+" (");

Field[] fs = voClass.getDeclaredFields();

Object[] values=new Object[fs.length];

int temp=0;

for (int i = 0; i < fs.length; i++) {

String name = fs.getName();

String type = fs.getType().getName();

Method methInstance = voClass.getMethod("get"+name.substring(0, 1).toUpperCase()+ name.substring(1,name.length()),new Class[] {});

Object value=methInstance.invoke(obj,new Object[]{});

if(value==null||(value.equals("")&&!type.equals("java.lang.String"))){

continue;

}else{

name_array.append(name.toLowerCase()+",");

value_array.append("?,");

values[temp]=value;

temp++;

}

}

sql.append(name_array.substring(0,name_array.length()-1)+") values ("+value_array.substring(0,value_array.length()-1)+")");

PreparedStatement ps =conn.prepareStatement(sql.toString());

for (int i = 0; i<temp; i++) {

if(values instanceof String){

ps.setString(i+1,values.toString());

}else if(values instanceof Integer){

ps.setInt(i+1,Integer.valueOf(values.toString()));

}else if(values instanceof Short){

ps.setShort(i+1,new Short(values.toString()));

}else if(values instanceof Long){

ps.setLong(i+1,Long.valueOf(values.toString()));

}else if(values instanceof Date){

ps.setDate(i+1,Date.valueOf(values.toString()));

}else if(values instanceof Double){

ps.setDouble(i+1,new Double(values.toString()));

}else if(values instanceof Timestamp){

ps.setTimestamp(i+1,Timestamp.valueOf(values.toString()));

}else if(values instanceof Time){

ps.setTime(i+1,Time.valueOf(values.toString()));

}else if(values instanceof BigDecimal){

ps.setBigDecimal(i+1,new BigDecimal(values.toString()));

}else if(values instanceof Blob){

ps.setBlob(i+1,(Blob)values);

}else if(values instanceof Clob){

ps.setClob(i+1,(Clob)values);

}

}

ps.executeUpdate();

ps.close();

} catch (Exception e) {

e.printStackTrace();

}

System.out.println(sql);

}