⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 blackdatabase.java

📁 短信开发用于文件交换处理转发的类模块
💻 JAVA
字号:
package com.pub.backserver.blackList;

import com.pub.berkeleydb.BklyDatabase;
import org.apache.log4j.*;
import com.pub.berkeleydb.BklyEnv;
import java.sql.ResultSet;
import java.io.BufferedReader;
import java.io.FileReader;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.Connection;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class BlackDatabase extends BklyDatabase {
    private static Logger log = Logger.getLogger(BlackDatabase.class);
    public BlackDatabase() {
        super._rc = new BlackRecordCreator();
        super._recordClass = BlackEntity.class;
    }

    //static String phoneFieldName = "vc2mobile";
    //static String typeFieldName = "numtype";

    public int open(String name, BklyEnv env) {
        return open(name, env, false, false, false, true, true);
    }

    public int Insert(BlackEntity be) {
        return this.put(be);
    }

    public int Delete(BlackEntity be) {
        return this.get(be, true, true);
    }

    public int Query(BlackEntity be) {
        return this.get(be, true, false);
    }

    public int Update(BlackEntity be) {
        String mobile = be.getPhone();
        int type = be.getType();
        int que = Delete(be);
        be.setPhone(mobile);
        be.setType(type);
        return Insert(be);
    }

    /**
     *
     * @param rs ResultSet
     *      字段次序必须:号码(string)、类型(int)
     * @return int
     *     > 0  updated rows;
     *     < 0  error
     */
    public int Update(ResultSet rs) {
        int count = 0;
        BlackEntity be = new BlackEntity();

        try {
            while (rs.next()) {
                try {
                    be.setPhone(rs.getString(1));
                    be.setType(rs.getInt(2));

                    if (be.getType() < 0) {
                        this.Delete(be);
                        log.debug("Delete : " + be.getPhone());
                    } else {
                        this.Insert(be);
                        log.debug("Insert : " + be.getPhone());
                    }
                    count++;
                } catch (Throwable ex) {
                    if (log.isEnabledFor(Priority.ERROR)) {
                        log.error(null, ex);
                    }
                }

            }
        } catch (Throwable ex) {
            if (log.isEnabledFor(Priority.ERROR)) {
                log.error(null, ex);
            }
            return -1;
        }
        log.debug("Insert total : " + count);
        return count;
    }

    public int LoadFromDatabase(String driver, String url, String user,
                                String pwd, String sql) {
        Connection conn = null;
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, user, pwd);
        } catch (Throwable err) {
            if (conn != null) {
                try {
                    conn.close();
                } catch (Throwable ex) {
                }
            }
            conn = null;

            if (log.isEnabledFor(Priority.ERROR)) {
                log.error(null, err);
            }

            return -1;
        }

        int count = 0;

        //load
        Statement stmt = null;
        ResultSet rs = null;
        try {
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);

            count = Update(rs);
        } catch (Throwable err) {
            if (log.isEnabledFor(Priority.ERROR)) {
                log.error(null, err);
            }
            return -2;
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (Throwable e) {}
            }
            rs = null;

            if (stmt != null) {
                try {
                    stmt.close();
                } catch (Throwable ex1) {
                }
            }
            stmt = null;

            try {
                conn.close();
            } catch (Throwable ex2) {
            }
            conn = null;
        }

        return count;
    }

    /**
     * 文件格式:文本文件,字段分割符:\t
     *          字段顺序:号码(string)、类型(int)
     *
     * @param filename String
     * @return int
     */
    public int loadFromFile(String filename) {
        FileReader fr = null;
        try {
            fr = new FileReader(filename);
        } catch (Throwable err) {
            if (log.isEnabledFor(Priority.ERROR)) {
                log.error(null, err);
            }
            if (fr != null) {
                try {
                    fr.close();
                } catch (Throwable ex) {
                }
            }
            return -1;
        }

        BufferedReader be = new BufferedReader(fr);
        String line = null;
        int lineCount = 0, validCount = 0;
        BlackEntity brd = new BlackEntity();
        try {
            while ((line = be.readLine()) != null) {
                lineCount++;
                if (line.length() > 0) {
                    String[] fds = line.split("\t");
                    if (fds.length >= 2 &&
                        fds[0].length() <= 12 && fds[0].length() >= 9 &&
                        fds[1].length() > 0 && fds[1].charAt(0) != '0') {

                        brd.setPhone(fds[0]);
                        brd.setType(fds[1].charAt(0) - '0');

                        this.Update(brd);

                        validCount++;
                    }
//          else//invlaid line
//          if (log.isInfoEnabled()) {
//            log.info("invalid line:" + line);
//          }
                }
                // else empty line
                if ((lineCount & 0x3fff) == 0 && log.isDebugEnabled())
                    log.debug("have read file line " + lineCount);

//        if((lineCount & 0x3fffff) == 0)
//          this.getOriginEnv().checkpoint(null);
            }

            if (log.isDebugEnabled()) {
                log.debug("loadFromFile lineCount=" + lineCount +
                          " validCount=" +
                          validCount);
            }
        } catch (Throwable err) {
            if (log.isEnabledFor(Priority.ERROR)) {
                log.error(null, err);
            } 
            return -2;
        }
        
        return validCount;
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -