📄 wordreader.java.netbeans-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.1, Mar 10, 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) throws NotFoundWordException { String sql = "select * from word where self like '" + word + "%' limit 1,15"; 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"); w.defComeFrom = rs.getString("src"); 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; if(key.defComeFrom == null) 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=?"; 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=?"; 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 + -