# HG changeset patch # User František Kučera # Date 1265839898 -3600 # Node ID 7cf0cbef29369c9265a1c55d6129b6259d0bf413 # Parent 85754d201c93228c65632848da2a4abecc148d64 DAO v prostém JDBC – pro srovnání. diff -r 85754d201c93 -r 7cf0cbef2936 java/SpringDemo1/SpringDemo1-ejb/src/java/cz/frantovo/springDemo1/dao/KnihaDAOjdbc.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/SpringDemo1/SpringDemo1-ejb/src/java/cz/frantovo/springDemo1/dao/KnihaDAOjdbc.java Wed Feb 10 23:11:38 2010 +0100 @@ -0,0 +1,78 @@ +package cz.frantovo.springDemo1.dao; + +import cz.frantovo.springDemo1.KnihaDAOjdbcRemote; +import cz.frantovo.springDemo1.dto.Kniha; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.Collection; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.annotation.Resource; +import javax.ejb.Stateless; +import javax.sql.DataSource; + +/** + * + * @author fiki + */ +@Stateless +public class KnihaDAOjdbc implements KnihaDAOjdbcRemote { + + @Resource(mappedName = "jdbc/sqlVyuka/piskoviste") + private DataSource dataSource; + private static final Logger log = Logger.getLogger(KnihaDAOjdbc.class.getSimpleName()); + + public Collection getKnihy() { + Collection vysledek = new ArrayList(); + Connection db = null; + PreparedStatement ps = null; + ResultSet rs = null; + try { + db = dataSource.getConnection(); + ps = db.prepareStatement("SELECT * FROM sbirka.kniha;"); + rs = ps.executeQuery(); + while (rs.next()) { + Kniha k = new Kniha(); + k.setNazev(rs.getString("nazev")); + k.setAutor(rs.getInt("autor")); + k.setDatumAktualizace(rs.getDate("datum")); + k.setId(rs.getInt("id")); + k.setIsbn(rs.getString("isbn")); + k.setPocetStran(rs.getInt("pocet_stran")); + k.setRokVydani(rs.getInt("rok_vydani")); + vysledek.add(k); + } + } catch (Exception e) { + log.log(Level.SEVERE, "Chyba načítání knížek.", e); + return null; + } finally { + zavri(db, ps, rs); + } + + return vysledek; + } + + protected static void zavri(Connection spojeni, Statement prikaz, ResultSet vysledek) { + if (vysledek != null) { + try { + vysledek.close(); + } catch (Exception e) { + } + } + if (prikaz != null) { + try { + prikaz.close(); + } catch (Exception e) { + } + } + if (spojeni != null) { + try { + spojeni.close(); + } catch (Exception e) { + } + } + } +} diff -r 85754d201c93 -r 7cf0cbef2936 java/SpringDemo1/SpringDemo1-ejb/src/java/cz/frantovo/springDemo1/dao/KnihaRowMapper.java --- a/java/SpringDemo1/SpringDemo1-ejb/src/java/cz/frantovo/springDemo1/dao/KnihaRowMapper.java Wed Feb 10 21:23:45 2010 +0100 +++ b/java/SpringDemo1/SpringDemo1-ejb/src/java/cz/frantovo/springDemo1/dao/KnihaRowMapper.java Wed Feb 10 23:11:38 2010 +0100 @@ -17,6 +17,7 @@ k.setAutor(rs.getInt("autor")); k.setDatumAktualizace(rs.getDate("datum")); k.setId(rs.getInt("id")); + k.setIsbn(rs.getString("isbn")); k.setPocetStran(rs.getInt("pocet_stran")); k.setRokVydani(rs.getInt("rok_vydani")); return k; diff -r 85754d201c93 -r 7cf0cbef2936 java/SpringDemo1/SpringDemo1-lib/src/cz/frantovo/springDemo1/KnihaDAOjdbcRemote.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/SpringDemo1/SpringDemo1-lib/src/cz/frantovo/springDemo1/KnihaDAOjdbcRemote.java Wed Feb 10 23:11:38 2010 +0100 @@ -0,0 +1,16 @@ +package cz.frantovo.springDemo1; + +import cz.frantovo.springDemo1.dto.Kniha; +import java.util.Collection; +import javax.ejb.Remote; + +/** + * + * @author fiki + */ +@Remote +public interface KnihaDAOjdbcRemote { + + public Collection getKnihy(); + +} diff -r 85754d201c93 -r 7cf0cbef2936 java/SpringDemo1/SpringDemo1-war/src/java/cz/frantovo/springDemo1/web/SpringDemo1Bean.java --- a/java/SpringDemo1/SpringDemo1-war/src/java/cz/frantovo/springDemo1/web/SpringDemo1Bean.java Wed Feb 10 21:23:45 2010 +0100 +++ b/java/SpringDemo1/SpringDemo1-war/src/java/cz/frantovo/springDemo1/web/SpringDemo1Bean.java Wed Feb 10 23:11:38 2010 +0100 @@ -1,7 +1,7 @@ package cz.frantovo.springDemo1.web; import cz.frantovo.springDemo1.KnihaDAORemote; -import cz.frantovo.springDemo1.dao.KnihaDAO; +import cz.frantovo.springDemo1.KnihaDAOjdbcRemote; import cz.frantovo.springDemo1.dto.Kniha; import java.util.Collection; import java.util.logging.Level; @@ -18,22 +18,44 @@ private static final Logger log = Logger.getLogger(SpringDemo1Bean.class.getSimpleName()); private KnihaDAORemote knihaDAO; + private KnihaDAOjdbcRemote knihaDAOjdbc; + /** Spring JdbcTemplate */ public Collection getKnihy() { - return lookupCilDAO().getKnihy(); + return lookupKnihaDAO().getKnihy(); } - private KnihaDAORemote lookupCilDAO() { + /** JDBC */ + public Collection getKnihyJdbc() { + return lookupKnihaDAOjdbc().getKnihy(); + } + + /** Spring JdbcTemplate */ + private KnihaDAORemote lookupKnihaDAO() { if (knihaDAO == null) { try { Context c = new InitialContext(); knihaDAO = (KnihaDAORemote) c.lookup("cz.frantovo.springDemo1.KnihaDAORemote"); } catch (NamingException e) { - log.log(Level.SEVERE, "Chyba při hledání CilDAO", e); + log.log(Level.SEVERE, "Chyba při hledání KnihaDAO", e); throw new RuntimeException(e); } } return knihaDAO; } + /** JDBC */ + private KnihaDAOjdbcRemote lookupKnihaDAOjdbc() { + if (knihaDAOjdbc == null) { + try { + Context c = new InitialContext(); + knihaDAOjdbc = (KnihaDAOjdbcRemote) c.lookup("cz.frantovo.springDemo1.KnihaDAOjdbcRemote"); + } catch (NamingException e) { + log.log(Level.SEVERE, "Chyba při hledání KnihaDAOjdbc", e); + throw new RuntimeException(e); + } + } + return knihaDAOjdbc; + } + } diff -r 85754d201c93 -r 7cf0cbef2936 java/SpringDemo1/SpringDemo1-war/web/index.jsp --- a/java/SpringDemo1/SpringDemo1-war/web/index.jsp Wed Feb 10 21:23:45 2010 +0100 +++ b/java/SpringDemo1/SpringDemo1-war/web/index.jsp Wed Feb 10 23:11:38 2010 +0100 @@ -10,19 +10,34 @@ SpringDemo1 +

SpringDemo1

-

Spring JDBC…

+

Spring JdbcTemplate

Vypíšeme seznam knih:

-
    - -
  • -
    -
- + + + + + + + + + + + + + + + + + + + +
Název knihyRok vydáníPočet stranISBN
diff -r 85754d201c93 -r 7cf0cbef2936 java/SpringDemo1/SpringDemo1-war/web/styl.css --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/SpringDemo1/SpringDemo1-war/web/styl.css Wed Feb 10 23:11:38 2010 +0100 @@ -0,0 +1,15 @@ +table { + border: 1px solid gray; + border-collapse: collapse; +} + +td { + border: 1px solid gray; + padding: 2px 4px; +} + +thead { + background-color: silver; + font-weight: bold; +} +