public boolean insertWord(String word, String labels) throws SQLException { // treat null case // if (word == null) return false; // perform a stemming // String stemmedWord = Stemmer.stem(word); // check if word exists, by performing a load operation // if (loadWord(stemmedWord) != null) { // word exists // return false; } else { // word doesn't exist // String query = "insert into Word(content,labels) values(?,?)"; PreparedStatement statement = connection.prepareStatement(query); statement.setString(1, stemmedWord); statement.setString(2, labels); statement.execute(); statement.close(); return true; } }
// perform a 'bulk' insert of words (at least the ones // not found in database) // public void insertWords(String[] words) throws SQLException { // treat null case // if (words == null) return; String query = "insert into Word(content) values(?)"; PreparedStatement statement = connection.prepareStatement(query); for (String w : words) { // check if word exists, by performing a load operation // if (loadWord(w) != null) { continue; } else { // perform a stemming // String stemmedWord = Stemmer.stem(w); statement.setString(1, stemmedWord); statement.execute(); } } statement.close(); }
public Word loadWord(String word) throws SQLException { // thread null case // if (word == null) return null; // perform a stemming // String stemmedWord = Stemmer.stem(word); // now prepare the stement // String query = "select * from Word where content like ?"; PreparedStatement statement = connection.prepareStatement(query); statement.setString(1, stemmedWord); ResultSet set = statement.executeQuery(); if (!set.next()) { set.close(); statement.close(); // no word was found // return null; } else { // load the word, then close the resources used // Word w = new Word(); w.setId(set.getInt(1)); w.setContent(set.getString(2)); w.setLanguage(null); set.close(); statement.close(); return w; } }