java/SpringDemo1/SpringDemo1-ejb/src/java/cz/frantovo/springDemo1/dao/KnihaDAOjdbc.java
changeset 5 7cf0cbef2936
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/java/SpringDemo1/SpringDemo1-ejb/src/java/cz/frantovo/springDemo1/dao/KnihaDAOjdbc.java	Wed Feb 10 23:11:38 2010 +0100
     1.3 @@ -0,0 +1,78 @@
     1.4 +package cz.frantovo.springDemo1.dao;
     1.5 +
     1.6 +import cz.frantovo.springDemo1.KnihaDAOjdbcRemote;
     1.7 +import cz.frantovo.springDemo1.dto.Kniha;
     1.8 +import java.sql.Connection;
     1.9 +import java.sql.PreparedStatement;
    1.10 +import java.sql.ResultSet;
    1.11 +import java.sql.Statement;
    1.12 +import java.util.ArrayList;
    1.13 +import java.util.Collection;
    1.14 +import java.util.logging.Level;
    1.15 +import java.util.logging.Logger;
    1.16 +import javax.annotation.Resource;
    1.17 +import javax.ejb.Stateless;
    1.18 +import javax.sql.DataSource;
    1.19 +
    1.20 +/**
    1.21 + *
    1.22 + * @author fiki
    1.23 + */
    1.24 +@Stateless
    1.25 +public class KnihaDAOjdbc implements KnihaDAOjdbcRemote {
    1.26 +
    1.27 +    @Resource(mappedName = "jdbc/sqlVyuka/piskoviste")
    1.28 +    private DataSource dataSource;
    1.29 +    private static final Logger log = Logger.getLogger(KnihaDAOjdbc.class.getSimpleName());
    1.30 +
    1.31 +    public Collection<Kniha> getKnihy() {
    1.32 +	Collection<Kniha> vysledek = new ArrayList<Kniha>();
    1.33 +	Connection db = null;
    1.34 +	PreparedStatement ps = null;
    1.35 +	ResultSet rs = null;
    1.36 +	try {
    1.37 +	    db = dataSource.getConnection();
    1.38 +	    ps = db.prepareStatement("SELECT * FROM sbirka.kniha;");
    1.39 +	    rs = ps.executeQuery();
    1.40 +	    while (rs.next()) {
    1.41 +		Kniha k = new Kniha();
    1.42 +		k.setNazev(rs.getString("nazev"));
    1.43 +		k.setAutor(rs.getInt("autor"));
    1.44 +		k.setDatumAktualizace(rs.getDate("datum"));
    1.45 +		k.setId(rs.getInt("id"));
    1.46 +		k.setIsbn(rs.getString("isbn"));
    1.47 +		k.setPocetStran(rs.getInt("pocet_stran"));
    1.48 +		k.setRokVydani(rs.getInt("rok_vydani"));
    1.49 +		vysledek.add(k);
    1.50 +	    }
    1.51 +	} catch (Exception e) {
    1.52 +	    log.log(Level.SEVERE, "Chyba načítání knížek.", e);
    1.53 +	    return null;
    1.54 +	} finally {
    1.55 +	    zavri(db, ps, rs);
    1.56 +	}
    1.57 +
    1.58 +	return vysledek;
    1.59 +    }
    1.60 +
    1.61 +    protected static void zavri(Connection spojeni, Statement prikaz, ResultSet vysledek) {
    1.62 +	if (vysledek != null) {
    1.63 +	    try {
    1.64 +		vysledek.close();
    1.65 +	    } catch (Exception e) {
    1.66 +	    }
    1.67 +	}
    1.68 +	if (prikaz != null) {
    1.69 +	    try {
    1.70 +		prikaz.close();
    1.71 +	    } catch (Exception e) {
    1.72 +	    }
    1.73 +	}
    1.74 +	if (spojeni != null) {
    1.75 +	    try {
    1.76 +		spojeni.close();
    1.77 +	    } catch (Exception e) {
    1.78 +	    }
    1.79 +	}
    1.80 +    }
    1.81 +}