# HG changeset patch # User František Kučera # Date 1265854437 -3600 # Node ID fc3e84d87e01fa62ed34691182ebc1f1a131bb99 # Parent 49dacfa6eae46f7dfc27d61bb3d7dad89156a252 DAO vrstva pomocí Spring JdbcTemplate. diff -r 49dacfa6eae4 -r fc3e84d87e01 java/nekurak.net-web/build.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/nekurak.net-web/build.xml Thu Feb 11 03:13:57 2010 +0100 @@ -0,0 +1,71 @@ + + + + + + + + + + + Builds, tests, and runs the project nekurak.net-web. + + + diff -r 49dacfa6eae4 -r fc3e84d87e01 java/nekurak.net-web/nbproject/build-impl.xml --- a/java/nekurak.net-web/nbproject/build-impl.xml Wed Jan 27 05:21:57 2010 +0100 +++ b/java/nekurak.net-web/nbproject/build-impl.xml Thu Feb 11 03:13:57 2010 +0100 @@ -527,10 +527,13 @@ + - + + + diff -r 49dacfa6eae4 -r fc3e84d87e01 java/nekurak.net-web/nbproject/genfiles.properties --- a/java/nekurak.net-web/nbproject/genfiles.properties Wed Jan 27 05:21:57 2010 +0100 +++ b/java/nekurak.net-web/nbproject/genfiles.properties Thu Feb 11 03:13:57 2010 +0100 @@ -1,8 +1,8 @@ -build.xml.data.CRC32=7602bd8b +build.xml.data.CRC32=e61550c0 build.xml.script.CRC32=d97fcc7d -build.xml.stylesheet.CRC32=c0ebde35@1.21.1.1 +build.xml.stylesheet.CRC32=c0ebde35@1.21.2.1 # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. -nbproject/build-impl.xml.data.CRC32=7602bd8b -nbproject/build-impl.xml.script.CRC32=dc954e54 -nbproject/build-impl.xml.stylesheet.CRC32=b139b33b@1.21.1.1 +nbproject/build-impl.xml.data.CRC32=e61550c0 +nbproject/build-impl.xml.script.CRC32=78de9ffb +nbproject/build-impl.xml.stylesheet.CRC32=b139b33b@1.21.2.1 diff -r 49dacfa6eae4 -r fc3e84d87e01 java/nekurak.net-web/nbproject/project.properties --- a/java/nekurak.net-web/nbproject/project.properties Wed Jan 27 05:21:57 2010 +0100 +++ b/java/nekurak.net-web/nbproject/project.properties Thu Feb 11 03:13:57 2010 +0100 @@ -25,7 +25,8 @@ j2ee.platform=1.5 j2ee.server.type=gfv3ee6 jar.compress=false -javac.classpath= +javac.classpath=\ + ${libs.spring-framework.classpath} # Space-separated list of extra javac options javac.compilerargs= javac.debug=true diff -r 49dacfa6eae4 -r fc3e84d87e01 java/nekurak.net-web/nbproject/project.xml --- a/java/nekurak.net-web/nbproject/project.xml Wed Jan 27 05:21:57 2010 +0100 +++ b/java/nekurak.net-web/nbproject/project.xml Thu Feb 11 03:13:57 2010 +0100 @@ -5,7 +5,12 @@ nekurak.net-web 1.6.5 - + + + ${libs.spring-framework.classpath} + WEB-INF/lib + + diff -r 49dacfa6eae4 -r fc3e84d87e01 java/nekurak.net-web/src/java/cz/frantovo/nekurak/dao/NekurakSuperDAO.java --- a/java/nekurak.net-web/src/java/cz/frantovo/nekurak/dao/NekurakSuperDAO.java Wed Jan 27 05:21:57 2010 +0100 +++ b/java/nekurak.net-web/src/java/cz/frantovo/nekurak/dao/NekurakSuperDAO.java Thu Feb 11 03:13:57 2010 +0100 @@ -31,20 +31,15 @@ } /** - * @return Databázové spojení nebo null při chybě. + * @return Datový zdroj nebo null při chybě. */ - protected Connection getSpojeni() { + protected DataSource getSpojeni() { String jndi = orizni(getVlastnost(DATABAZE.VYCHOZI, NekurakSuperDAO.class)); - DataSource zdroj = null; try { - zdroj = (DataSource) kontext.lookup(jndi); - return zdroj.getConnection(); + return (DataSource) kontext.lookup(jndi); } catch (NamingException ex) { log.log(Level.SEVERE, "getSpojeni: lookup", ex); return null; - } catch (SQLException ex) { - log.log(Level.SEVERE, "getSpojeni: sql", ex); - return null; } } } diff -r 49dacfa6eae4 -r fc3e84d87e01 java/nekurak.net-web/src/java/cz/frantovo/nekurak/dao/PodnikDAO.java --- a/java/nekurak.net-web/src/java/cz/frantovo/nekurak/dao/PodnikDAO.java Wed Jan 27 05:21:57 2010 +0100 +++ b/java/nekurak.net-web/src/java/cz/frantovo/nekurak/dao/PodnikDAO.java Thu Feb 11 03:13:57 2010 +0100 @@ -1,12 +1,8 @@ package cz.frantovo.nekurak.dao; import cz.frantovo.nekurak.dto.Podnik; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.util.ArrayList; import java.util.Collection; -import java.util.logging.Level; +import org.springframework.jdbc.core.JdbcTemplate; /** * Stará se o načítání a ukládání podniků (hospod) z a do databáze. @@ -21,27 +17,11 @@ UPDATE, NEXTVAL } + private PodnikRowMapper podnikRowMapper = new PodnikRowMapper(); + private JdbcTemplate jdbcTemplate = new JdbcTemplate(getSpojeni()); public Collection getPodniky() { - Connection db = getSpojeni(); - PreparedStatement ps = null; - ResultSet rs = null; - try { - ps = db.prepareStatement(getSQL(SQL.SELECT_VSECHNY)); - rs = ps.executeQuery(); - Collection vysledek = new ArrayList(); - - while (rs.next()) { - vysledek.add(new Podnik(rs.getInt("id"), rs.getString("nazev"))); - } - - return vysledek; - } catch (Exception e) { - log.log(Level.SEVERE, "Chyba při získávání podniků.", e); - return null; - } finally { - zavri(db, ps, rs); - } + return jdbcTemplate.query(getSQL(SQL.SELECT_VSECHNY), podnikRowMapper); } /** @@ -50,36 +30,12 @@ * @return id vloženého nebo uloženého podniku. */ public int ulozPodnik(Podnik p) { - Connection db = getSpojeni(); - PreparedStatement psUpdate = null; - PreparedStatement psId = null; - ResultSet rsId = null; - try { - if (p.getId() == 0) { - psId = db.prepareStatement(getSQL(SQL.NEXTVAL)); - rsId = psId.executeQuery(); - rsId.next(); - p.setId(rsId.getInt(1)); - - psUpdate = db.prepareStatement(getSQL(SQL.INSERT)); - } else { - psUpdate = db.prepareStatement(getSQL(SQL.UPDATE)); - } - - psUpdate.setString(1, p.getNazev()); - psUpdate.setInt(2, p.getId()); - - psUpdate.executeUpdate(); - - db.commit(); - return p.getId(); - } catch (Exception e) { - rollback(db); - log.log(Level.SEVERE, "Chyba při ukládání podniku.", e); - return -1; - } finally { - zavri(null, psId, rsId); - zavri(db, psUpdate, null); + if (p.getId() == 0) { + p.setId(jdbcTemplate.queryForInt(getSQL(SQL.NEXTVAL))); + jdbcTemplate.update(getSQL(SQL.INSERT), new Object[]{p.getNazev(), p.getId()}); + } else { + jdbcTemplate.update(getSQL(SQL.UPDATE), new Object[]{p.getNazev(), p.getId()}); } + return p.getId(); } } diff -r 49dacfa6eae4 -r fc3e84d87e01 java/nekurak.net-web/src/java/cz/frantovo/nekurak/dao/PodnikRowMapper.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/nekurak.net-web/src/java/cz/frantovo/nekurak/dao/PodnikRowMapper.java Thu Feb 11 03:13:57 2010 +0100 @@ -0,0 +1,21 @@ +package cz.frantovo.nekurak.dao; + +import cz.frantovo.nekurak.dto.Podnik; +import java.sql.ResultSet; +import java.sql.SQLException; +import org.springframework.jdbc.core.simple.ParameterizedRowMapper; + +/** + * + * @author fiki + */ +public class PodnikRowMapper implements ParameterizedRowMapper { + + public Podnik mapRow(ResultSet rs, int i) throws SQLException { + Podnik p = new Podnik(); + p.setId(rs.getInt("id")); + p.setNazev(rs.getString("nazev")); + return p; + } + +} diff -r 49dacfa6eae4 -r fc3e84d87e01 java/nekurak.net-web/web/index.jsp --- a/java/nekurak.net-web/web/index.jsp Wed Jan 27 05:21:57 2010 +0100 +++ b/java/nekurak.net-web/web/index.jsp Thu Feb 11 03:13:57 2010 +0100 @@ -10,6 +10,7 @@ Nekuřák.net +

Nekuřák.net

@@ -25,15 +26,25 @@ -

Vypíšeme seznam hospod.

+

Vypíšeme seznam podniků:

-
    - - -
  • ${p.nazev}
  • -
    -
+ + + + + + + + + + + + + + + +
ČísloNázev
diff -r 49dacfa6eae4 -r fc3e84d87e01 java/nekurak.net-web/web/styl.css --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/nekurak.net-web/web/styl.css Thu Feb 11 03:13:57 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; +} +