1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/java/SuperPostak/src/cz/frantovo/superPostak/DataSQL.java Sun Nov 23 22:19:20 2008 +0100
1.3 @@ -0,0 +1,50 @@
1.4 +/*
1.5 + * To change this template, choose Tools | Templates
1.6 + * and open the template in the editor.
1.7 + */
1.8 +package cz.frantovo.superPostak;
1.9 +
1.10 +import cz.frantovo.postak.InternetAddressKomu;
1.11 +import java.io.UnsupportedEncodingException;
1.12 +import java.sql.Connection;
1.13 +import java.sql.PreparedStatement;
1.14 +import java.sql.ResultSet;
1.15 +import java.sql.SQLException;
1.16 +import java.util.ArrayList;
1.17 +import java.util.Collection;
1.18 +import java.util.logging.Level;
1.19 +import java.util.logging.Logger;
1.20 +
1.21 +/**
1.22 + * Třída pro vykonávání SQL dotazů
1.23 + * @author fiki
1.24 + */
1.25 +public class DataSQL {
1.26 +
1.27 + private static Logger log = Logger.getLogger(DataSQL.class.getName());
1.28 + private Connection spojeni;
1.29 +
1.30 + public DataSQL(Connection spojeni) {
1.31 + this.spojeni = spojeni;
1.32 + }
1.33 +
1.34 + /**
1.35 + * Provede SQL příkaz a získá z něj e-mailové adresy.
1.36 + * Zatím předpokládá pouze jediný sloupec, který obsahuje adresu.
1.37 + */
1.38 + public Collection<InternetAddressKomu> getAdresy(String sql) throws SQLException, UnsupportedEncodingException {
1.39 + ArrayList<InternetAddressKomu> vysledek = new ArrayList<InternetAddressKomu>();
1.40 +
1.41 + if (sql == null || sql.length() < 3) {
1.42 + log.log(Level.FINER, "Prázdný SQL dotaz");
1.43 + return null;
1.44 + } else {
1.45 + PreparedStatement ps = spojeni.prepareStatement(sql);
1.46 + ResultSet rs = ps.executeQuery();
1.47 + while (rs.next()) {
1.48 + vysledek.add(new InternetAddressKomu(rs.getString(1), rs.getString(1)));
1.49 + }
1.50 + return vysledek;
1.51 + }
1.52 + }
1.53 +}