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

📄 wordreader.java.svn-base

📁 使用OSGi框架开发的分布式电子辞典
💻 SVN-BASE
字号:
/*  * @(#)WordReader.java *  * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. *  * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU Library General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */package cn.edu.ynu.sei.dict.plugin.dictmanager;import cn.edu.ynu.sei.dict.plugin.db.connector.service.IDatabaseConnector;import cn.edu.ynu.sei.dict.core.service.IDictQueryService;import cn.edu.ynu.sei.dict.core.service.Word;import cn.edu.ynu.sei.dict.core.exception.NotFoundWordException;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.ArrayList;import java.util.List;import java.util.logging.Level;import java.util.logging.Logger;/** * Query word form db. * @author zy * @author 88250 * @version 1.0.0.2, Mar 16, 2008 */public class WordReader implements IDictQueryService {    private IDatabaseConnector dbConnector = null;    private Connection con = null;    private Statement stm = null;    private PreparedStatement preStm = null;    private ResultSet rs = null;    /**     * Constructor with argument.     * @param dbConnector dictionary manager     */    public WordReader(IDatabaseConnector dbConnector) {        this.dbConnector = dbConnector;    }    @Override    public String lookupSimilarList(String word, String sname)            throws NotFoundWordException {        String sql = null;        System.out.println("word:"+word);        System.out.println("sname:"+sname);        if (sname.equals("null") || sname.equals("ld")) {            sql = "select * from word where self like '" + word +                    "%' limit 1,15";        } else {            sql = "select * from userdict where self like '" + word +                    "%' and name='" + sname + "'limit 0,15";        }        System.out.println("sql:"+sql);        Word w = null;        List<Word> words = new ArrayList<Word>();        try {            con = dbConnector.getDBConnection();            stm = con.createStatement();            rs = stm.executeQuery(sql);            while (rs.next()) {                w = new Word();                w.self = rs.getString("self");                w.definition = rs.getString("def");                words.add(w);            }            stm.close();            rs.close();            con.close();        } catch (SQLException ex) {            Logger.getLogger(WordReader.class.getName()).log(Level.SEVERE, null, ex);        }        String result = "";        if (words == null) {            throw new NotFoundWordException(word);        }        for (Word aword : words) {            result += aword.self + "###";        }        result += "@";        return result;    }    @Override    public Word lookup(Word key) throws NotFoundWordException {        Word word;        System.out.println("key.def:"+key.defComeFrom);        if (key.defComeFrom.equals("null") || key.defComeFrom.equals("ld")) {            word = getWord(key.self);        } else {            word = this.getWordFromUserDict(key.self, key.defComeFrom);        }        if (word == null) {            throw new NotFoundWordException(key.self);        }        return word;    }    // FIXME: query in different table.    /**     * Get a word from db.     * @param key     *         the key for searching     * @return     *         the key word, if failed, return null     */    private Word getWord(String key) {        Word word = null;        con = dbConnector.getDBConnection();        String sql = "select * from word where self=?";        System.out.println(sql);        try {            preStm = con.prepareStatement(sql);            preStm.setString(1, key);            rs = preStm.executeQuery();            while (rs.next()) {                word = new Word();                word.self = rs.getString("self");                word.definition = rs.getString("def");                word.defComeFrom = rs.getString("src");                word.rank = rs.getInt("class");            }            rs.close();            stm.close();            con.close();        } catch (SQLException ex) {            Logger.getLogger(WordReader.class.getName()).log(Level.SEVERE, null, ex);        }        return word;    }    /**     * Get all the preword List.     * @return     *        all the preword List.If the predictionary is empty,return null.     */    private List<Word> findAll() {        List<Word> wordList = new ArrayList<Word>();        Word word = null;        String sql = "select * from preword";        try {            con = dbConnector.getDBConnection();            stm = con.createStatement();            rs = stm.executeQuery(sql);            while (rs.next()) {                word = new Word();                word.id = rs.getInt("id");                word.self = rs.getString("self");                word.definition = rs.getString("def");                word.defComeFrom = rs.getString("src");                wordList.add(word);            }            stm.close();            rs.close();            con.close();        } catch (SQLException ex) {            Logger.getLogger(WordReader.class.getName()).log(Level.SEVERE, null,                    ex);        }        return wordList.isEmpty() ? null : wordList;    }    /**     * Get a word according spell and the author.     * @param key     *         the spell of the word.     * @param name     *         the name of the author.     * @return     *         the write word which is user wanted,if failed,return null.     */    private Word getWordFromUserDict(String key, String name) {        Word word = null;        String sql = "select * from userdict where self=? and name=?";        System.out.println(sql);        con = dbConnector.getDBConnection();        try {            preStm = con.prepareStatement(sql);            preStm.setString(1, key);            preStm.setString(2, name);            ResultSet rs = preStm.executeQuery();            while (rs.next()) {                word = new Word();                word.id = rs.getInt("id");                word.self = rs.getString("self");                word.definition = rs.getString("def");                word.defComeFrom = rs.getString("name");            }            rs.close();            preStm.close();            con.close();        } catch (SQLException ex) {        }        return word;    }}

⌨️ 快捷键说明

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