• 11单词统计-补


    读入文件的字符串,以1-n个空格作为分隔拆分读入,全部转换为大写/小写字母(做到不区分大小写)后进行统计。若库里有该词则计数加一,否则添加字段。

    package servlet;
    
    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.util.Scanner;
    
    import dao.PROCE;
    
    public class test {
        public static void main(String[] args) throws FileNotFoundException {
            int N;
            Scanner scanner = new Scanner(new BufferedReader(new FileReader("E://HPSS.txt")));
            Scanner ipt = new Scanner(System.in);
            scanner.useDelimiter("[^A-Za-z']");
            while (scanner.hasNext()) {
                PROCE.process(scanner.next());
            }
            System.err.println("Process Done.Please Enter the Amount You want to Display:");
            N = ipt.nextInt();
            PROCE.showResult(N);
        }
    }
    package dat;
    
    public class wordPro {
        private long time;
        private String word;
    
        public wordPro() {
    
        }
    
        public wordPro(String ipt, long lipt) {
            time = lipt;
            word = ipt;
        }
    
        public void setWord(String ipt) {
            word = ipt;
        }
    
        public String getWord() {
            return word;
        }
    
        public void setTime(long ipt) {
            time = ipt;
        }
    
        public long getTime() {
            return time;
        }
    }
    package dao;
    
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    import dbu.DBUtil;
    
    public class PROCE {
        public static int showResult() {
            int flag = 0;
            String sql = "select * from word_log order by time desc";
            Connection conn = DBUtil.getConn();
            Statement state = null;
            ResultSet rs = null;
            try {
                state = conn.createStatement();
                rs = state.executeQuery(sql);
                while (rs.next()) {
                    System.out.println("Word:" + rs.getString("word") + "    Time:" + rs.getInt("time"));
                    flag = flag + 1;
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                DBUtil.close(rs, state, conn);
            }
            return flag;
        }
    
        public static int showResult(int n) {
            int flag = n;
            String sql = "select * from word_log order by time desc";
            Connection conn = DBUtil.getConn();
            Statement state = null;
            ResultSet rs = null;
            try {
                state = conn.createStatement();
                rs = state.executeQuery(sql);
                while (rs.next()) {
                    System.out.println("Word:" + rs.getString("word") + "    Time:" + rs.getInt("time"));
                    flag = flag - 1;
                    if (flag == 0) {
                        return 0;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                DBUtil.close(rs, state, conn);
            }
            return flag;
        }
    
        public static void process(String ipt) {
            boolean flag = true;
            long i = 0;
            ipt = ipt.toLowerCase();
            ipt = ipt.replace("'", "" + "\'");
            if (ipt.equals("")) {
                return;
            }
            i = haveWord(ipt);
            if (i == 0) {
                flag = addWord(ipt);
                if (!flag) {
                    System.err.println("Add Word Error!");
                    flag = true;
                }
            } else {
                flag = plusWord(ipt, i);
                if (!flag) {
                    System.err.println("Plus Word Error!");
                }
            }
            System.out.println("Successful Processed Word:" + ipt);
        }
    
        public static long haveWord(String ipt) {
            long flag = 0;
            String sql = "select * from word_log where word ='" + ipt + "'";
            Connection conn = DBUtil.getConn();
            Statement state = null;
            ResultSet rs = null;
            try {
                state = conn.createStatement();
                rs = state.executeQuery(sql);
                if (rs.next()) {
                    flag = rs.getInt("time");
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                DBUtil.close(rs, state, conn);
            }
            return flag;
        }
    
        public static boolean addWord(String ipt) {
            String sql = "insert into word_log(word,time) values('" + ipt + "',1)";
            Connection conn = DBUtil.getConn();
            Statement state = null;
            boolean f = false;
            int a = 0;
            try {
                state = conn.createStatement();
                a = state.executeUpdate(sql);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                DBUtil.close(state, conn);
            }
    
            if (a > 0) {
                f = true;
            }
            return f;
        }
    
        public static boolean plusWord(String ipt, long time) {
            time = time + 1;
            String sql = "update word_log set time = " + time + " where word='" + ipt + "'";
            Connection conn = DBUtil.getConn();
            Statement state = null;
            boolean f = false;
            int a = 0;
            try {
                state = conn.createStatement();
                a = state.executeUpdate(sql);
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                DBUtil.close(state, conn);
            }
    
            if (a > 0) {
                f = true;
            }
            return f;
        }
    }

    当时第一次尝试非Web项目的数据库读写。用数据库存储统计数据的缺点是读写比较慢,统计一次要很长时间。

  • 相关阅读:
    断点调试
    内部类
    继承2
    继承
    构造函数
    方法
    二维数组
    HTML 一
    使用mySQL与数据库进行交互(一)
    使用mySQL与数据库进行交互(二)
  • 原文地址:https://www.cnblogs.com/minadukirinno/p/13337278.html
Copyright © 2020-2023  润新知