码迷,mamicode.com
首页 > 其他好文 > 详细

11单词统计-补

时间:2020-07-18 22:27:12      阅读:84      评论:0      收藏:0      [点我收藏+]

标签:case   als   display   lease   asn   字符   success   iter   resultset   

读入文件的字符串,以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项目的数据库读写。用数据库存储统计数据的缺点是读写比较慢,统计一次要很长时间。

 

11单词统计-补

标签:case   als   display   lease   asn   字符   success   iter   resultset   

原文地址:https://www.cnblogs.com/minadukirinno/p/13337278.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!