现在小弟遇到如下问题。现在我想逐行检查数据库记录。当某个记录值为0时,让当前值更新为上一行记录的非零值。如:数据库记录如下
1 2001 99
2 2002 70
3 2003  0

这个时侯逐行检查到第三行2003记录时第三个字段的值为0.这时需要让它更新为2002年的70。代码如下,使用access数据库。该怎么更新,才能更新当前行?
ResultSet rs = getUpdate.executeQuery("select dyhdl from nfdlb");
n1=0;
while(rs.next()){
   n=rs.getFloat("dyhdl");
   if(n==0){
        getUpdate.executeUpdate("update nfdlb set dyhdl="+n1);
   }
   n1=n;
}
就是这句查询语句不会写。请各位前辈指教!如果能写出直接调用上一行的语句将更加感激不尽!

解决方案 »

  1.   


    ResultSet rs = getUpdate.executeQuery("select dyhdl from nfdlb"); 
    n1=0; 
    int index =0 ;
    while(rs.next()){ 
      if(index==0) n1 = rs.getFloat("dyhdl");
      n=rs.getFloat("dyhdl"); 
      if(n1!=n && n==0){ 
            getUpdate.executeUpdate("update nfdlb set dyhdl="+n1); 
      } 
      n1=n; 
      index ++;
                    }
      

  2.   

    加个Else感觉更对。 ResultSet rs = getUpdate.executeQuery("select dyhdl from nfdlb"); 
    n1=0; 
    int index =0 ;
    while(rs.next()){ 
      if(index==0) n1 = rs.getFloat("dyhdl");
      n=rs.getFloat("dyhdl"); 
      if(n1!=n && n==0){ 
                getUpdate.executeUpdate("update nfdlb set dyhdl="+n1); 
      }else{ 
        n1=n; 
      }
      index ++;

      

  3.   

    现在关键是当我update时,整个数据库全变了。并不是变当前行。这句update nfdlb set dyhdl="+n1语句是不是应该来个限定?
      

  4.   

    你现在是在循环里面update 的,n1的一直是上个的值。所以不需要限定条件
      

  5.   

    说错了,呵呵,习惯性的以为是操作对象了。
    需要限定条件的,在查询中
    select id,dyhdl from nfdlb
    把ID也查询出来,
    getUpdate.executeUpdate("update nfdlb set dyhdl="+n1 where id = rs.getInt("id")); 
      

  6.   

    还有就是,getUpdate在运行时指针变了。
    怎么才能在循环里面再使用另一个getUpdate呢?
      

  7.   

    id是连续的吗?是用的sequence吗?如果是:ResultSet rs = getUpdate.executeQuery("select id,dyhdl from nfdlb"); while(rs.next()){ 
      n=rs.getFloat("dyhdl"); 
     int n1=rs.getInt("id");
      if(n==0){ 
            getUpdate.executeUpdate("update nfdlb set dyhdl=(select dyhdl from  nfdlb  where id="+n1-1+")"); 
      }