二、Java编程与程序运行结果

  1.Java编程,打印昨天的当前时刻

public class YesterdayCurrent{
  public void main(String[] args){
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.DATE, -1);
    System.out.println(cal.getTime());
  }
}

  2.文件读写,实现一个计数器

  public int getNum(){
        int i = -1;
        try{
            String stri="";
            BufferedReader in = new BufferedReader(new FileReader(f));
            while((stri=in.readLine())!=null){
                i = Integer.parseInt(stri.trim());
            }
            in.close();
        }catch(Exception e){
            e.printStackTrace();
        }
        return i;
    }
    public void setNum(){
        int i = getNum();
        i++;       
        try{
            PrintWriter out=new PrintWriter(new BufferedWriter(new FileWriter(f,false))); 
            out.write(String.valueOf(i));            //可能是编码的原因,如果直接写入int的话,将出现java编码和windows编码的混乱,因此此处写入的是String
            out.close() ;
        }catch(Exception e){
            e.printStackTrace();
        }
    }

  3. 指出下面程序的运行结果:

class A{
    static{
        System.out.print("1");
    }
    public A(){
        System.out.print("2");
    }
}
class B extends A{
    static{
        System.out.print("a");
    }
    public B(){
        System.out.print("b");
    }  
}
public class Hello{
    public static void main(String[] ars){
        A ab = new B(); //执行到此处,结果: 1a2b
 ab = new B(); //执行到此处,结果: 1a2bab
    }
}

  注:类的static 代码段,可以看作是类首次加载(被虚拟机加载)执行的代码,而对于类的加载,首先要执行其基类的构造,再执行其本身的构造

  4.写一个Singleton模式的例子

public class Singleton{
 private static Singleton single = new Singleton();
 private Singleton(){}
 public static Singleton getInstance(){
  return single;
 }
}

三、数据库

  1.删除表的重复记录

  如果记录完全相同才算重复记录,那么:  (sql server2000下测试通过)

select distinct * into #tmpp from tid
delete from tid    
insert into tid select * from #tmpp
drop table #tmpp

  如果有id主键(数字,自增1的那种),那么:(sql server2000下测试通过)

delete from tableA where id not in
(select id = min(id) from tableA group by name)

  2.delete from tablea & truncate table tablea的区别

  truncate 语句执行速度快,占资源少,并且只记录页删除的日志;
  delete 对每条记录的删除均需要记录日志

 

posted on 2011-11-17 08:12  易初莲花  阅读(326)  评论(0编辑  收藏  举报