要求导出一个表中数据到文本,(完成)然后和另外一个表导出的数据进行比较记录相同数量条数,并删除和另外一个表中相同的记录Java代码实现

解决方案 »

  1.   

    package util;import java.util.*;
    import java.util.regex.*;
    import java.io.*;public class UserTreeMap { public static void main(String args[]) throws IOException {
    BufferedReader buf = new BufferedReader(new FileReader("D:\\english.txt"));
    System.out.println("Read under this dir English.txt");

    StringBuffer sbuf = new StringBuffer();// 缓冲字符串
    String line = null;
    while ((line = buf.readLine()) != null) {
    sbuf.append(line);// 追加到缓冲字符串中
    }
    buf.close();// 读取结束
    Pattern expression = Pattern.compile("[a-zA-Z]+");// 定义正则表达式匹配单词
    String string1 = sbuf.toString().toLowerCase();// 转换成小写
    Matcher matcher = expression.matcher(string1);// 定义string1的匹配器
    TreeMap myTreeMap = new TreeMap();// 创建树映射 存放键/值对
    int n = 0;// 文章中单词总数
    Object word = null;// 文章中的单词
    Object num = null;// 出现的次数
    while (matcher.find()) {// 是否匹配单词
    word = matcher.group();// 得到一个单词-树映射的键
    n++;// 单词数加1
    if (myTreeMap.containsKey(word)) {// 如果包含该键,单词出现过
    num = myTreeMap.get(word);// 得到单词出现的次数
    Integer count = (Integer) num;// 强制转化
    myTreeMap.put(word, new Integer(count.intValue() + 1));
    } else {
    myTreeMap.put(word, new Integer(1));// 否则单词第一次出现,添加到映射中
    }
    }
    System.out.println("统计分析如下:");
    System.out.println("t 文章中单词总数" + n + "个");
    System.out.println("具体的信息在当前目录的result.txt文件中");
    BufferedWriter bufw = new BufferedWriter(new FileWriter("result.txt"));
    Iterator iter = myTreeMap.keySet().iterator();// 得到树映射键集合的迭代器
    Object key = null;
    while (iter.hasNext()) {// 使用迭代器遍历树映射的键
    key = iter.next();
    bufw.write((String) key + ":" + myTreeMap.get(key));// 键/值写到文件中
    bufw.newLine();
    }
    bufw.write("english.txt中的单词总数" + n + "个");
    bufw.newLine();
    bufw.write("english.txt中不同单词" + myTreeMap.size() + "个");
    bufw.close();
    }
    }
      

  2.   

    然后和另外一个表导出的数据进行比较 -- 这个导出比较效率太低。 用一条sql可解决。关键字 exists记录相同数量条数,并删除和另外一个表中相同的记录 -- 同上
      

  3.   

    有2个表   
      一个表B的记录多,最新表:   
      id   name     tel     address   
      0       a         123       asdf   
      1       b         456       asf   
      2       c         254       as   
      3       d         789       sadf   
      4       e         110       fs   
        
      另一个表A的记录少,是以前的表  
      id   name     tel     address   
      0     peter   123       asdf   
      1       b         456       asf   
      2       c         254       as   现在我想删除A表中,B表也有的记录,MYSQL语句怎么写呢
      

  4.   

    java , mysql貌似你来错地方了.现在我想删除A表中,B表也有的记录,MYSQL语句怎么写呢delete from a where id in (select id from b)
      

  5.   

    [SQL] delete 
    from a 
    where id 
    in (select id
          from b)[Err] 1064 - You have an error in your SQL syntax.  Check the manual that corresponds to your MySQL server version for the right syntax to use near 'select id
          from b)' at line 4
      

  6.   

    delete from a where exists (select id from b where id=a.id)
      

  7.   

    大家还有一个条件我没注意说清楚,ID可能不相同,但是至少 NAME,TEL,ADDRESS中的2个相同才算同一条记录,语句怎么写呢,刚才的语句报错,注意以下时MYSQL语句
      

  8.   


    [SQL] delete 
    from a 
    where 
    exists (select id 
               from b 
              where id=a.id)[Err] 1064 - You have an error in your SQL syntax.  Check the manual that corresponds to your MySQL server version for the right syntax to use near 'exists (select id 
               from b 
              where id=a.id
      

  9.   

    引用 3 楼 tangyu477 的回复:
    有2个表 
      一个表B的记录多,最新表: 
      id  name    tel    address 
      0      a        123      asdf 
      1      b        456      asf 
      2      c        254      as 
      3      d        789      sadf 
      4      e        110      fs 
       
      另一个表A的记录少,是以前的表 
      id  name    tel    address 
      0    peter  123      asdf 
      1      b        456      asf 
      2      c        254      as 
    现在我想删除A表中,B表也有的记录,MYSQL语句怎么写呢delete a from a,b where a.id=b.id