public boolean duofa(String ad[]){
           boolean resave=false;
           try{
              stat=conn.createStatement();
               for(int i=0;i<ad.length;i++){
                    sql = "update Supply_data set time='" + sqltime +
                      "' where id='"+ad[i]+"'";
                   if(stat.executeUpdate(sql)>0){
                       resave=false;
                   }
               }
           }catch(Exception e){
               System.out.println(e.getMessage());
           }finally{
               try{
                  stat.close();
                   if (conn != null && !conn.isClosed()) { conn.close(); }
               }catch(Exception e){}
           }
           return resave;
   }
public boolean duofa(String ad[]){
           boolean resave=false;
           try{
              
               for(int i=0;i<ad.length;i++){
                   stat=conn.createStatement();
                    sql = "update Supply_data set time='" + sqltime +
                      "' where id='"+ad[i]+"'";
                   if(stat.executeUpdate(sql)>0){
                       resave=false;
                   }
                stat.close();
               }
           }catch(Exception e){
               System.out.println(e.getMessage());
           }finally{
               try{
                  stat.close();
                   if (conn != null && !conn.isClosed()) { conn.close(); }
               }catch(Exception e){}
           }
           return resave;
   }
上边哪个共好,从内存和速度上哪个更好?

解决方案 »

  1.   

    当然是第一个
    第一个stat只建立了一个
    第二个建立了ad.length - 1个
      

  2.   

    你一个一个update不累啊public boolean duofa(String ad[]){ 
              boolean resave=false; 
              try{ 
                  stat=conn.createStatement(); 
                  String sql = "update Supply_data set time='" + sqltime + "' where id in (";
                  String whereid="";
                  for(int i=0;i <ad.length;i++){ 
                        String id="'"+ad[i]+"'";
                        whereid+=id;
                  }
                  whereid+=")"
                  sql+=whereid;
                  if(stat.executeUpdate(sql)>0){ 
                          resave=false; 
                  } 
              }catch(Exception e){ 
                  System.out.println(e.getMessage()); 
              }finally{ 
                  try{ 
                      stat.close(); 
                      if (conn != null && !conn.isClosed()) { conn.close(); } 
                  }catch(Exception e){} 
              } 
              return resave; 
      } 
    前提是方法的参数String ad[]长度不为0;
      

  3.   

    当然第一个····  简单通熟的说  你声明一次就再内存开辟空间···第二种占内存的,,而且你这样写也不行啊,安全性不好,  一般也不使用拼SQL 这样拼会有漏洞的