Java tutorial
/* * Copyright 2012 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package de.tudarmstadt.lt.nlkg; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; import org.apache.commons.collections.IteratorUtils; import org.apache.commons.collections.iterators.EmptyIterator; import org.apache.commons.collections.iterators.ListIteratorWrapper; import com.google.common.collect.Iterables; /** * * @author Steffen Remus **/ public class DT { // String _mysql_url = "jdbc:mysql://192.168.200.101:3306/nlkg_1"; String _mysql_host = "localhost"; int _mysql_port = 3306; String _mysql_dbname = "nlkg_1"; String _mysql_url() { return String.format("jdbc:mysql://%s:%d/%s", _mysql_host, _mysql_port, _mysql_dbname); }; String _mysql_driver = "com.mysql.jdbc.Driver"; String _mysql_userName = "remus"; String _mysql_password = "REMUS"; public DT() { } public static class Entry implements Iterable<Word> { @SuppressWarnings("unchecked") public final static Entry EMPTY = new Entry() { { word = Word.EMPTY; dtwords = EmptyIterator.INSTANCE; } }; public Word word; public Iterator<Word> dtwords; @Override public String toString() { return word + ": " + IteratorUtils.toList(dtwords); } @Override public Iterator<Word> iterator() { return dtwords; } } public static class Word { public final static Word EMPTY = new Word() { { word = ""; significance = 0d; } }; public String word; public double significance; @Override public String toString() { return word + "=" + significance; } } Connection c = null; Connection connect() throws SQLException { return c != null ? c : (c = DriverManager.getConnection(_mysql_url() + "?useUnicode=true&characterEncoding=UTF-8", _mysql_userName, _mysql_password)); } void disconnect() { if (c != null) try { c.close(); } catch (SQLException e) { } c = null; } public Entry get(String word, int max_dt_words) { try { String query = String.format( "SELECT word2,count FROM `dt` WHERE word1 LIKE '%s' ORDER BY count DESC LIMIT %d;", word, max_dt_words + 1); final Connection c = connect(); final Statement s = c.createStatement(); final ResultSet r = s.executeQuery(query); if (!r.next()) return Entry.EMPTY; Entry result = new Entry() { { word = new Word() { { word = r.getString(1); significance = r.getDouble(2); } }; dtwords = new Iterator<Word>() { @Override public boolean hasNext() { try { if (r.isClosed()) return false; if (r.next()) return true; r.close(); s.close(); return false; } catch (SQLException e) { e.printStackTrace(); return false; } } @Override public Word next() { try { return new Word() { { word = r.getString(1); significance = r.getDouble(2); } }; } catch (SQLException e) { e.printStackTrace(); return Word.EMPTY; } } @Override public void remove() { throw new UnsupportedOperationException(); } @Override protected void finalize() throws Throwable { r.close(); r.close(); super.finalize(); } }; } }; return result; } catch (Exception e) { e.printStackTrace(); return Entry.EMPTY; } } void queryDT(String word) { queryDT(word, 200); } void queryDT(String word, int max_results) { try { // Class.forName(_mysql_driver).newInstance(); Connection _mysql_conn = DriverManager.getConnection( _mysql_url() + "?useUnicode=true&characterEncoding=UTF-8", _mysql_userName, _mysql_password); System.out.println("Connected to the database"); String query = String.format("SELECT * FROM `dt` WHERE word1 = '%s' ORDER BY count DESC LIMIT 1,%d;", word, max_results); Statement st = _mysql_conn.createStatement(); ResultSet rs = st.executeQuery(query); // System.out.println(rs.getMetaData().getColumnCount()); // System.out.println(rs.getFetchSize()); int nc = rs.getMetaData().getColumnCount(); String format_str = new String(new char[nc + 1]).replace("\0", "%-20s\t") + "%n"; // System.out.println(format_str); List<String> values = new ArrayList<String>(); // header values.add("row"); for (int i = 1; i <= nc; i++) values.add(rs.getMetaData().getColumnName(i)); System.out.format(format_str, values.toArray()); // data while (rs.next()) { values.clear(); values.add(String.valueOf(rs.getRow())); for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) values.add(rs.getString(i)); System.out.format(format_str, values.toArray()); } st.close(); _mysql_conn.close(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { Entry e = new DT() { { _mysql_dbname = "nlkg_3"; } }.get("%::abscess", 10); System.out.println(e); while (e.dtwords.hasNext()) System.out.println(e.dtwords.next()); } }