DAO v prostém JDBC – pro srovnání.
authorFrantišek Kučera <franta-hg@frantovo.cz>
Wed Feb 10 23:11:38 2010 +0100 (2010-02-10)
changeset 57cf0cbef2936
parent 4 85754d201c93
child 6 0ae95f929a1a
DAO v prostém JDBC – pro srovnání.
java/SpringDemo1/SpringDemo1-ejb/src/java/cz/frantovo/springDemo1/dao/KnihaDAOjdbc.java
java/SpringDemo1/SpringDemo1-ejb/src/java/cz/frantovo/springDemo1/dao/KnihaRowMapper.java
java/SpringDemo1/SpringDemo1-lib/src/cz/frantovo/springDemo1/KnihaDAOjdbcRemote.java
java/SpringDemo1/SpringDemo1-war/src/java/cz/frantovo/springDemo1/web/SpringDemo1Bean.java
java/SpringDemo1/SpringDemo1-war/web/index.jsp
java/SpringDemo1/SpringDemo1-war/web/styl.css
     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 +}
     2.1 --- a/java/SpringDemo1/SpringDemo1-ejb/src/java/cz/frantovo/springDemo1/dao/KnihaRowMapper.java	Wed Feb 10 21:23:45 2010 +0100
     2.2 +++ b/java/SpringDemo1/SpringDemo1-ejb/src/java/cz/frantovo/springDemo1/dao/KnihaRowMapper.java	Wed Feb 10 23:11:38 2010 +0100
     2.3 @@ -17,6 +17,7 @@
     2.4  	k.setAutor(rs.getInt("autor"));
     2.5  	k.setDatumAktualizace(rs.getDate("datum"));
     2.6  	k.setId(rs.getInt("id"));
     2.7 +	k.setIsbn(rs.getString("isbn"));
     2.8  	k.setPocetStran(rs.getInt("pocet_stran"));
     2.9  	k.setRokVydani(rs.getInt("rok_vydani"));
    2.10  	return k;
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/java/SpringDemo1/SpringDemo1-lib/src/cz/frantovo/springDemo1/KnihaDAOjdbcRemote.java	Wed Feb 10 23:11:38 2010 +0100
     3.3 @@ -0,0 +1,16 @@
     3.4 +package cz.frantovo.springDemo1;
     3.5 +
     3.6 +import cz.frantovo.springDemo1.dto.Kniha;
     3.7 +import java.util.Collection;
     3.8 +import javax.ejb.Remote;
     3.9 +
    3.10 +/**
    3.11 + *
    3.12 + * @author fiki
    3.13 + */
    3.14 +@Remote
    3.15 +public interface KnihaDAOjdbcRemote {
    3.16 +
    3.17 +    public Collection<Kniha> getKnihy();
    3.18 +
    3.19 +}
     4.1 --- a/java/SpringDemo1/SpringDemo1-war/src/java/cz/frantovo/springDemo1/web/SpringDemo1Bean.java	Wed Feb 10 21:23:45 2010 +0100
     4.2 +++ b/java/SpringDemo1/SpringDemo1-war/src/java/cz/frantovo/springDemo1/web/SpringDemo1Bean.java	Wed Feb 10 23:11:38 2010 +0100
     4.3 @@ -1,7 +1,7 @@
     4.4  package cz.frantovo.springDemo1.web;
     4.5  
     4.6  import cz.frantovo.springDemo1.KnihaDAORemote;
     4.7 -import cz.frantovo.springDemo1.dao.KnihaDAO;
     4.8 +import cz.frantovo.springDemo1.KnihaDAOjdbcRemote;
     4.9  import cz.frantovo.springDemo1.dto.Kniha;
    4.10  import java.util.Collection;
    4.11  import java.util.logging.Level;
    4.12 @@ -18,22 +18,44 @@
    4.13  
    4.14      private static final Logger log = Logger.getLogger(SpringDemo1Bean.class.getSimpleName());
    4.15      private KnihaDAORemote knihaDAO;
    4.16 +    private KnihaDAOjdbcRemote knihaDAOjdbc;
    4.17  
    4.18 +    /** Spring JdbcTemplate */
    4.19      public Collection<Kniha> getKnihy() {
    4.20 -	return lookupCilDAO().getKnihy();
    4.21 +	return lookupKnihaDAO().getKnihy();
    4.22      }
    4.23  
    4.24 -    private KnihaDAORemote lookupCilDAO() {
    4.25 +    /** JDBC */
    4.26 +    public Collection<Kniha> getKnihyJdbc() {
    4.27 +	return lookupKnihaDAOjdbc().getKnihy();
    4.28 +    }
    4.29 +
    4.30 +    /** Spring JdbcTemplate */
    4.31 +    private KnihaDAORemote lookupKnihaDAO() {
    4.32          if (knihaDAO == null) {
    4.33              try {
    4.34                  Context c = new InitialContext();
    4.35                  knihaDAO = (KnihaDAORemote) c.lookup("cz.frantovo.springDemo1.KnihaDAORemote");
    4.36              } catch (NamingException e) {
    4.37 -                log.log(Level.SEVERE, "Chyba při hledání CilDAO", e);
    4.38 +                log.log(Level.SEVERE, "Chyba při hledání KnihaDAO", e);
    4.39                  throw new RuntimeException(e);
    4.40              }
    4.41          }
    4.42          return knihaDAO;
    4.43      }
    4.44  
    4.45 +    /** JDBC */
    4.46 +    private KnihaDAOjdbcRemote lookupKnihaDAOjdbc() {
    4.47 +        if (knihaDAOjdbc == null) {
    4.48 +            try {
    4.49 +                Context c = new InitialContext();
    4.50 +                knihaDAOjdbc = (KnihaDAOjdbcRemote) c.lookup("cz.frantovo.springDemo1.KnihaDAOjdbcRemote");
    4.51 +            } catch (NamingException e) {
    4.52 +                log.log(Level.SEVERE, "Chyba při hledání KnihaDAOjdbc", e);
    4.53 +                throw new RuntimeException(e);
    4.54 +            }
    4.55 +        }
    4.56 +        return knihaDAOjdbc;
    4.57 +    }
    4.58 +
    4.59  }
     5.1 --- a/java/SpringDemo1/SpringDemo1-war/web/index.jsp	Wed Feb 10 21:23:45 2010 +0100
     5.2 +++ b/java/SpringDemo1/SpringDemo1-war/web/index.jsp	Wed Feb 10 23:11:38 2010 +0100
     5.3 @@ -10,19 +10,34 @@
     5.4      <html xmlns="http://www.w3.org/1999/xhtml">
     5.5  	<head>
     5.6  	    <title>SpringDemo1</title>
     5.7 +	    <link href="styl.css" type="text/css" rel="StyleSheet"/>
     5.8  	</head>
     5.9  	<body>
    5.10  	    <h1>SpringDemo1</h1>
    5.11 -	    <p>Spring JDBC…</p>
    5.12 +	    <p>Spring JdbcTemplate</p>
    5.13  	    <p>Vypíšeme seznam knih:</p>
    5.14  
    5.15  	    <jsp:useBean id="demo" class="cz.frantovo.springDemo1.web.SpringDemo1Bean" scope="request"/>
    5.16 -	    <ul>
    5.17 -		<c:forEach var="kniha" items="${demo.knihy}">
    5.18 -		    <li><c:out value="${kniha.nazev}"/></li>
    5.19 -		</c:forEach>
    5.20 -	    </ul>
    5.21 -
    5.22 +	    <table>
    5.23 +		<thead>
    5.24 +		    <tr>
    5.25 +			<td>Název knihy</td>
    5.26 +			<td>Rok vydání</td>
    5.27 +			<td>Počet stran</td>
    5.28 +			<td>ISBN</td>
    5.29 +		    </tr>
    5.30 +		</thead>
    5.31 +		<tbody>
    5.32 +		    <c:forEach var="kniha" items="${demo.knihyJdbc}">
    5.33 +			<tr>
    5.34 +			    <td><c:out value="${kniha.nazev}"/></td>
    5.35 +			    <td><c:out value="${kniha.rokVydani}"/></td>
    5.36 +			    <td><c:out value="${kniha.pocetStran}"/></td>
    5.37 +			    <td><c:out value="${kniha.isbn}"/></td>
    5.38 +			</tr>
    5.39 +		    </c:forEach>
    5.40 +		</tbody>
    5.41 +	    </table>
    5.42  
    5.43  	</body>
    5.44      </html>
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/java/SpringDemo1/SpringDemo1-war/web/styl.css	Wed Feb 10 23:11:38 2010 +0100
     6.3 @@ -0,0 +1,15 @@
     6.4 +table {
     6.5 +    border: 1px solid gray;
     6.6 +    border-collapse: collapse;
     6.7 +}
     6.8 +
     6.9 +td {
    6.10 +    border: 1px solid gray;
    6.11 +    padding: 2px 4px;
    6.12 +}
    6.13 +
    6.14 +thead {
    6.15 +    background-color: silver;
    6.16 +    font-weight: bold;
    6.17 +}
    6.18 +