📄 wordwriter.java
字号:
/* * @(#)WordWriter.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.core.exception.DuplicateWordException;import cn.edu.ynu.sei.dict.core.exception.NotFoundWordException;import cn.edu.ynu.sei.dict.core.service.IDictEditService;import cn.edu.ynu.sei.dict.core.service.Word;import cn.edu.ynu.sei.dict.plugin.db.connector.service.IDatabaseConnector;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.logging.Level;import java.util.logging.Logger;/** * Update words to db. * @author zy * @author 88250 * @version 1.0.0.1, Mar 10, 2008 */public class WordWriter implements IDictEditService { 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 WordWriter(IDatabaseConnector dbConnector) { this.dbConnector = dbConnector; } @Override public void addVocabulary(Word vocabulary) throws DuplicateWordException { WordReader wordReader = new WordReader(this.dbConnector); try { wordReader.lookup(vocabulary); throw new DuplicateWordException("此单词已存在"); } catch (NotFoundWordException ex) { this.persistWordToUserDict(vocabulary); } } @Override public void deleteVocabulary(Word vocabulary) throws NotFoundWordException { throw new UnsupportedOperationException("Not supported yet."); } @Override public void editVocabulary(Word vocabulary) { if (vocabulary.defComeFrom == null) { this.updateWord(vocabulary); } else { this.updateWordToUserDict(vocabulary); } } /** * Add a word to database. * @param word * the new word to be added to db. */ public void persisWord(Word word) { String sql = "insert into word (self,def,src) value(?,?,?)"; con = dbConnector.getDBConnection(); try { preStm = con.prepareStatement(sql); preStm.setString(1, word.self); preStm.setString(2, word.definition); preStm.setString(3, word.defComeFrom); preStm.executeUpdate(); preStm.close(); con.close(); } catch (SQLException ex) { Logger.getLogger(WordWriter.class.getName()).log(Level.SEVERE, null, ex); } } /** * Update a word to database. * @param word * the word to be updated. */ public void updateWord(Word word) { String sql = "update word set def='" + word.definition + "' where self='" + word.self + "'"; try { con = dbConnector.getDBConnection(); stm = con.createStatement(); stm.executeUpdate(sql); stm.close(); con.close(); } catch (SQLException ex) { Logger.getLogger(WordWriter.class.getName()).log(Level.SEVERE, null, ex); } } /** * Add a preword to database. * @param word * the new preword to be added to db. */ public void persisPreWord(Word word) { String sql = "insert into preword (self,def,src) value(?,?,?)"; con = dbConnector.getDBConnection(); try { preStm = con.prepareStatement(sql); preStm.setString(1, word.self); preStm.setString(2, word.definition); preStm.setString(3, word.defComeFrom); preStm.executeUpdate(); preStm.close(); con.close(); } catch (SQLException ex) { Logger.getLogger(WordWriter.class.getName()).log(Level.SEVERE, null, ex); } } /** * Delete a preword from predictionary. * @param id * the id of the prevocabulary */ public void deletePreWord(int id) { try { String sql = "delete from preword where id='" + id + "'"; con = dbConnector.getDBConnection(); stm = con.createStatement(); stm.executeUpdate(sql); stm.close(); con.close(); } catch (SQLException ex) { Logger.getLogger(WordWriter.class.getName()).log(Level.SEVERE, null, ex); } } /** * Add a new to user dict. * @param word * the new word. */ public void persistWordToUserDict(Word word) { String sql = "insert into userdict (name,self,def) value(?,?,?)"; con = dbConnector.getDBConnection(); try { preStm = con.prepareStatement(sql); preStm.setString(1, word.defComeFrom); preStm.setString(2, word.self); preStm.setString(3, word.definition); preStm.executeUpdate(); preStm.close(); con.close(); } catch (SQLException ex) { Logger.getLogger(WordWriter.class.getName()).log(Level.SEVERE, null, ex); } } /** * Update a particular user's word. * @param word * the word to be updated. */ public void updateWordToUserDict(Word word) { String sql = "update userdict set def=? where name=? and self=?"; con = dbConnector.getDBConnection(); try { preStm = con.prepareStatement(sql); preStm.setString(1, word.definition); preStm.setString(2, word.defComeFrom); preStm.setString(3, word.self); preStm.executeUpdate(); preStm.close(); con.close(); } catch (SQLException ex) { Logger.getLogger(WordWriter.class.getName()).log(Level.SEVERE, null, ex); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -