1. 程式人生 > 實用技巧 >11單詞統計-補

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專案的資料庫讀寫。用資料庫儲存統計資料的缺點是讀寫比較慢,統計一次要很長時間。