DAO vrstva pomocí Spring JdbcTemplate.
authorFrantišek Kučera <franta-hg@frantovo.cz>
Thu Feb 11 03:13:57 2010 +0100 (2010-02-11)
changeset 26fc3e84d87e01
parent 25 49dacfa6eae4
child 27 88f3012c60a2
DAO vrstva pomocí Spring JdbcTemplate.
java/nekurak.net-web/build.xml
java/nekurak.net-web/nbproject/build-impl.xml
java/nekurak.net-web/nbproject/genfiles.properties
java/nekurak.net-web/nbproject/project.properties
java/nekurak.net-web/nbproject/project.xml
java/nekurak.net-web/src/java/cz/frantovo/nekurak/dao/NekurakSuperDAO.java
java/nekurak.net-web/src/java/cz/frantovo/nekurak/dao/PodnikDAO.java
java/nekurak.net-web/src/java/cz/frantovo/nekurak/dao/PodnikRowMapper.java
java/nekurak.net-web/web/index.jsp
java/nekurak.net-web/web/styl.css
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/java/nekurak.net-web/build.xml	Thu Feb 11 03:13:57 2010 +0100
     1.3 @@ -0,0 +1,71 @@
     1.4 +<?xml version="1.0" encoding="UTF-8"?>
     1.5 +<!-- You may freely edit this file. See commented blocks below for -->
     1.6 +<!-- some examples of how to customize the build. -->
     1.7 +<!-- (If you delete it and reopen the project it will be recreated.) -->
     1.8 +<!-- By default, only the Clean and Build commands use this build script. -->
     1.9 +<!-- Commands such as Run, Debug, and Test only use this build script if -->
    1.10 +<!-- the Compile on Save feature is turned off for the project. -->
    1.11 +<!-- You can turn off the Compile on Save (or Deploy on Save) setting -->
    1.12 +<!-- in the project's Project Properties dialog box.-->
    1.13 +<project name="nekurak.net-web" default="default" basedir=".">
    1.14 +    <description>Builds, tests, and runs the project nekurak.net-web.</description>
    1.15 +    <import file="nbproject/build-impl.xml"/>
    1.16 +    <!--
    1.17 +
    1.18 +    There exist several targets which are by default empty and which can be 
    1.19 +    used for execution of your tasks. These targets are usually executed 
    1.20 +    before and after some main targets. They are: 
    1.21 +
    1.22 +      -pre-init:                 called before initialization of project properties 
    1.23 +      -post-init:                called after initialization of project properties 
    1.24 +      -pre-compile:              called before javac compilation 
    1.25 +      -post-compile:             called after javac compilation 
    1.26 +      -pre-compile-single:       called before javac compilation of single file
    1.27 +      -post-compile-single:      called after javac compilation of single file
    1.28 +      -pre-compile-test:         called before javac compilation of JUnit tests
    1.29 +      -post-compile-test:        called after javac compilation of JUnit tests
    1.30 +      -pre-compile-test-single:  called before javac compilation of single JUnit test
    1.31 +      -post-compile-test-single: called after javac compilation of single JUunit test
    1.32 +      -pre-dist:                 called before archive building 
    1.33 +      -post-dist:                called after archive building 
    1.34 +      -post-clean:               called after cleaning build products 
    1.35 +      -pre-run-deploy:           called before deploying
    1.36 +      -post-run-deploy:          called after deploying
    1.37 +
    1.38 +    Example of pluging an obfuscator after the compilation could look like 
    1.39 +
    1.40 +        <target name="-post-compile">
    1.41 +            <obfuscate>
    1.42 +                <fileset dir="${build.classes.dir}"/>
    1.43 +            </obfuscate>
    1.44 +        </target>
    1.45 +
    1.46 +    For list of available properties check the imported 
    1.47 +    nbproject/build-impl.xml file. 
    1.48 +
    1.49 +
    1.50 +    Other way how to customize the build is by overriding existing main targets.
    1.51 +    The target of interest are: 
    1.52 +
    1.53 +      init-macrodef-javac:    defines macro for javac compilation
    1.54 +      init-macrodef-junit:   defines macro for junit execution
    1.55 +      init-macrodef-debug:    defines macro for class debugging
    1.56 +      do-dist:                archive building
    1.57 +      run:                    execution of project 
    1.58 +      javadoc-build:          javadoc generation 
    1.59 +
    1.60 +    Example of overriding the target for project execution could look like 
    1.61 +
    1.62 +        <target name="run" depends="<PROJNAME>-impl.jar">
    1.63 +            <exec dir="bin" executable="launcher.exe">
    1.64 +                <arg file="${dist.jar}"/>
    1.65 +            </exec>
    1.66 +        </target>
    1.67 +
    1.68 +    Notice that overridden target depends on jar target and not only on 
    1.69 +    compile target as regular run target does. Again, for list of available 
    1.70 +    properties which you can use check the target you are overriding in 
    1.71 +    nbproject/build-impl.xml file. 
    1.72 +
    1.73 +    -->
    1.74 +</project>
     2.1 --- a/java/nekurak.net-web/nbproject/build-impl.xml	Wed Jan 27 05:21:57 2010 +0100
     2.2 +++ b/java/nekurak.net-web/nbproject/build-impl.xml	Thu Feb 11 03:13:57 2010 +0100
     2.3 @@ -527,10 +527,13 @@
     2.4      </target>
     2.5      <target depends="init,compile,compile-jsps,-pre-dist,-do-dist-with-manifest,-do-dist-without-manifest" name="do-dist"/>
     2.6      <target depends="init" if="dist.ear.dir" name="library-inclusion-in-manifest">
     2.7 +        <copyfiles files="${libs.spring-framework.classpath}" iftldtodir="${build.web.dir}/WEB-INF" todir="${dist.ear.dir}/lib"/>
     2.8          <mkdir dir="${build.web.dir}/META-INF"/>
     2.9          <manifest file="${build.web.dir}/META-INF/MANIFEST.MF" mode="update"/>
    2.10      </target>
    2.11 -    <target depends="init" name="library-inclusion-in-archive" unless="dist.ear.dir"/>
    2.12 +    <target depends="init" name="library-inclusion-in-archive" unless="dist.ear.dir">
    2.13 +        <copyfiles files="${libs.spring-framework.classpath}" todir="${build.web.dir}/WEB-INF/lib"/>
    2.14 +    </target>
    2.15      <target depends="init" if="dist.ear.dir" name="-clean-webinf-lib">
    2.16          <delete dir="${build.web.dir}/WEB-INF/lib"/>
    2.17      </target>
     3.1 --- a/java/nekurak.net-web/nbproject/genfiles.properties	Wed Jan 27 05:21:57 2010 +0100
     3.2 +++ b/java/nekurak.net-web/nbproject/genfiles.properties	Thu Feb 11 03:13:57 2010 +0100
     3.3 @@ -1,8 +1,8 @@
     3.4 -build.xml.data.CRC32=7602bd8b
     3.5 +build.xml.data.CRC32=e61550c0
     3.6  build.xml.script.CRC32=d97fcc7d
     3.7 -build.xml.stylesheet.CRC32=c0ebde35@1.21.1.1
     3.8 +build.xml.stylesheet.CRC32=c0ebde35@1.21.2.1
     3.9  # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
    3.10  # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
    3.11 -nbproject/build-impl.xml.data.CRC32=7602bd8b
    3.12 -nbproject/build-impl.xml.script.CRC32=dc954e54
    3.13 -nbproject/build-impl.xml.stylesheet.CRC32=b139b33b@1.21.1.1
    3.14 +nbproject/build-impl.xml.data.CRC32=e61550c0
    3.15 +nbproject/build-impl.xml.script.CRC32=78de9ffb
    3.16 +nbproject/build-impl.xml.stylesheet.CRC32=b139b33b@1.21.2.1
     4.1 --- a/java/nekurak.net-web/nbproject/project.properties	Wed Jan 27 05:21:57 2010 +0100
     4.2 +++ b/java/nekurak.net-web/nbproject/project.properties	Thu Feb 11 03:13:57 2010 +0100
     4.3 @@ -25,7 +25,8 @@
     4.4  j2ee.platform=1.5
     4.5  j2ee.server.type=gfv3ee6
     4.6  jar.compress=false
     4.7 -javac.classpath=
     4.8 +javac.classpath=\
     4.9 +    ${libs.spring-framework.classpath}
    4.10  # Space-separated list of extra javac options
    4.11  javac.compilerargs=
    4.12  javac.debug=true
     5.1 --- a/java/nekurak.net-web/nbproject/project.xml	Wed Jan 27 05:21:57 2010 +0100
     5.2 +++ b/java/nekurak.net-web/nbproject/project.xml	Thu Feb 11 03:13:57 2010 +0100
     5.3 @@ -5,7 +5,12 @@
     5.4          <data xmlns="http://www.netbeans.org/ns/web-project/3">
     5.5              <name>nekurak.net-web</name>
     5.6              <minimum-ant-version>1.6.5</minimum-ant-version>
     5.7 -            <web-module-libraries/>
     5.8 +            <web-module-libraries>
     5.9 +                <library dirs="200">
    5.10 +                    <file>${libs.spring-framework.classpath}</file>
    5.11 +                    <path-in-war>WEB-INF/lib</path-in-war>
    5.12 +                </library>
    5.13 +            </web-module-libraries>
    5.14              <web-module-additional-libraries/>
    5.15              <source-roots>
    5.16                  <root id="src.dir"/>
     6.1 --- a/java/nekurak.net-web/src/java/cz/frantovo/nekurak/dao/NekurakSuperDAO.java	Wed Jan 27 05:21:57 2010 +0100
     6.2 +++ b/java/nekurak.net-web/src/java/cz/frantovo/nekurak/dao/NekurakSuperDAO.java	Thu Feb 11 03:13:57 2010 +0100
     6.3 @@ -31,20 +31,15 @@
     6.4      }
     6.5  
     6.6      /**
     6.7 -     * @return Databázové spojení nebo null při chybě.
     6.8 +     * @return Datový zdroj nebo null při chybě.
     6.9       */
    6.10 -    protected Connection getSpojeni() {
    6.11 +    protected DataSource getSpojeni() {
    6.12  	String jndi = orizni(getVlastnost(DATABAZE.VYCHOZI, NekurakSuperDAO.class));
    6.13 -	DataSource zdroj = null;
    6.14  	try {
    6.15 -	    zdroj = (DataSource) kontext.lookup(jndi);
    6.16 -	    return zdroj.getConnection();
    6.17 +	    return (DataSource) kontext.lookup(jndi);
    6.18  	} catch (NamingException ex) {
    6.19  	    log.log(Level.SEVERE, "getSpojeni: lookup", ex);
    6.20  	    return null;
    6.21 -	} catch (SQLException ex) {
    6.22 -	    log.log(Level.SEVERE, "getSpojeni: sql", ex);
    6.23 -	    return null;
    6.24  	}
    6.25      }
    6.26  }
     7.1 --- a/java/nekurak.net-web/src/java/cz/frantovo/nekurak/dao/PodnikDAO.java	Wed Jan 27 05:21:57 2010 +0100
     7.2 +++ b/java/nekurak.net-web/src/java/cz/frantovo/nekurak/dao/PodnikDAO.java	Thu Feb 11 03:13:57 2010 +0100
     7.3 @@ -1,12 +1,8 @@
     7.4  package cz.frantovo.nekurak.dao;
     7.5  
     7.6  import cz.frantovo.nekurak.dto.Podnik;
     7.7 -import java.sql.Connection;
     7.8 -import java.sql.PreparedStatement;
     7.9 -import java.sql.ResultSet;
    7.10 -import java.util.ArrayList;
    7.11  import java.util.Collection;
    7.12 -import java.util.logging.Level;
    7.13 +import org.springframework.jdbc.core.JdbcTemplate;
    7.14  
    7.15  /**
    7.16   * Stará se o načítání a ukládání podniků (hospod) z a do databáze.
    7.17 @@ -21,27 +17,11 @@
    7.18  	UPDATE,
    7.19  	NEXTVAL
    7.20      }
    7.21 +    private PodnikRowMapper podnikRowMapper = new PodnikRowMapper();
    7.22 +    private JdbcTemplate jdbcTemplate = new JdbcTemplate(getSpojeni());
    7.23  
    7.24      public Collection<Podnik> getPodniky() {
    7.25 -	Connection db = getSpojeni();
    7.26 -	PreparedStatement ps = null;
    7.27 -	ResultSet rs = null;
    7.28 -	try {
    7.29 -	    ps = db.prepareStatement(getSQL(SQL.SELECT_VSECHNY));
    7.30 -	    rs = ps.executeQuery();
    7.31 -	    Collection<Podnik> vysledek = new ArrayList<Podnik>();
    7.32 -
    7.33 -	    while (rs.next()) {
    7.34 -		vysledek.add(new Podnik(rs.getInt("id"), rs.getString("nazev")));
    7.35 -	    }
    7.36 -
    7.37 -	    return vysledek;
    7.38 -	} catch (Exception e) {
    7.39 -	    log.log(Level.SEVERE, "Chyba při získávání podniků.", e);
    7.40 -	    return null;
    7.41 -	} finally {
    7.42 -	    zavri(db, ps, rs);
    7.43 -	}
    7.44 +	return jdbcTemplate.query(getSQL(SQL.SELECT_VSECHNY), podnikRowMapper);
    7.45      }
    7.46  
    7.47      /**
    7.48 @@ -50,36 +30,12 @@
    7.49       * @return id vloženého nebo uloženého podniku.
    7.50       */
    7.51      public int ulozPodnik(Podnik p) {
    7.52 -	Connection db = getSpojeni();
    7.53 -	PreparedStatement psUpdate = null;
    7.54 -	PreparedStatement psId = null;
    7.55 -	ResultSet rsId = null;
    7.56 -	try {
    7.57 -	    if (p.getId() == 0) {
    7.58 -		psId = db.prepareStatement(getSQL(SQL.NEXTVAL));
    7.59 -		rsId = psId.executeQuery();
    7.60 -		rsId.next();
    7.61 -		p.setId(rsId.getInt(1));
    7.62 -
    7.63 -		psUpdate = db.prepareStatement(getSQL(SQL.INSERT));
    7.64 -	    } else {
    7.65 -		psUpdate = db.prepareStatement(getSQL(SQL.UPDATE));
    7.66 -	    }
    7.67 -
    7.68 -	    psUpdate.setString(1, p.getNazev());
    7.69 -	    psUpdate.setInt(2, p.getId());
    7.70 -
    7.71 -	    psUpdate.executeUpdate();
    7.72 -
    7.73 -	    db.commit();
    7.74 -	    return p.getId();
    7.75 -	} catch (Exception e) {
    7.76 -	    rollback(db);
    7.77 -	    log.log(Level.SEVERE, "Chyba při ukládání podniku.", e);
    7.78 -	    return -1;
    7.79 -	} finally {
    7.80 -	    zavri(null, psId, rsId);
    7.81 -	    zavri(db, psUpdate, null);
    7.82 +	if (p.getId() == 0) {
    7.83 +	    p.setId(jdbcTemplate.queryForInt(getSQL(SQL.NEXTVAL)));
    7.84 +	    jdbcTemplate.update(getSQL(SQL.INSERT), new Object[]{p.getNazev(), p.getId()});
    7.85 +	} else {
    7.86 +	    jdbcTemplate.update(getSQL(SQL.UPDATE), new Object[]{p.getNazev(), p.getId()});
    7.87  	}
    7.88 +	return p.getId();
    7.89      }
    7.90  }
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/java/nekurak.net-web/src/java/cz/frantovo/nekurak/dao/PodnikRowMapper.java	Thu Feb 11 03:13:57 2010 +0100
     8.3 @@ -0,0 +1,21 @@
     8.4 +package cz.frantovo.nekurak.dao;
     8.5 +
     8.6 +import cz.frantovo.nekurak.dto.Podnik;
     8.7 +import java.sql.ResultSet;
     8.8 +import java.sql.SQLException;
     8.9 +import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
    8.10 +
    8.11 +/**
    8.12 + *
    8.13 + * @author fiki
    8.14 + */
    8.15 +public class PodnikRowMapper implements ParameterizedRowMapper<Podnik> {
    8.16 +
    8.17 +    public Podnik mapRow(ResultSet rs, int i) throws SQLException {
    8.18 +	Podnik p = new Podnik();
    8.19 +	p.setId(rs.getInt("id"));
    8.20 +	p.setNazev(rs.getString("nazev"));
    8.21 +	return p;
    8.22 +    }
    8.23 +
    8.24 +}
     9.1 --- a/java/nekurak.net-web/web/index.jsp	Wed Jan 27 05:21:57 2010 +0100
     9.2 +++ b/java/nekurak.net-web/web/index.jsp	Thu Feb 11 03:13:57 2010 +0100
     9.3 @@ -10,6 +10,7 @@
     9.4      <html xmlns="http://www.w3.org/1999/xhtml">
     9.5  	<head>
     9.6  	    <title>Nekuřák.net</title>
     9.7 +	    <link href="styl.css" type="text/css" rel="StyleSheet"/>
     9.8  	</head>
     9.9  	<body>
    9.10  	    <h1>Nekuřák.net</h1>
    9.11 @@ -25,15 +26,25 @@
    9.12  	    <c:choose>
    9.13  		<!-- if -->
    9.14  		<c:when test="${param.akce == 'seznam'}">
    9.15 -		    <p>Vypíšeme seznam hospod.</p>
    9.16 +		    <p>Vypíšeme seznam podniků:</p>
    9.17  		    <!-- Vytvoříme si instanci JavaBeany -->
    9.18  		    <jsp:useBean id="podnikyWeb" class="cz.frantovo.nekurak.web.PodnikyWeb" scope="request"/>
    9.19 -		    <ul>
    9.20 -			<c:forEach var="p" items="${podnikyWeb.podniky}">
    9.21 -			    <!-- vypíšeme název podniku (měli bychom escapovat) -->
    9.22 -			    <li>${p.nazev}</li>
    9.23 -			</c:forEach>
    9.24 -		    </ul>
    9.25 +		    <table>
    9.26 +			<thead>
    9.27 +			    <tr>
    9.28 +				<td>Číslo</td>
    9.29 +				<td>Název</td>
    9.30 +			    </tr>
    9.31 +			</thead>
    9.32 +			<tbody>
    9.33 +			    <c:forEach var="p" items="${podnikyWeb.podniky}">
    9.34 +				<tr>
    9.35 +				    <td><c:out value="${p.id}"/></td>
    9.36 +				    <td><c:out value="${p.nazev}"/></td>
    9.37 +				</tr>
    9.38 +			    </c:forEach>
    9.39 +			</tbody>
    9.40 +		    </table>
    9.41  		</c:when>
    9.42  		<!-- else if -->
    9.43  		<c:when test="${param.akce == 'podrobnosti'}">
    10.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.2 +++ b/java/nekurak.net-web/web/styl.css	Thu Feb 11 03:13:57 2010 +0100
    10.3 @@ -0,0 +1,15 @@
    10.4 +table {
    10.5 +    border: 1px solid gray;
    10.6 +    border-collapse: collapse;
    10.7 +}
    10.8 +
    10.9 +td {
   10.10 +    border: 1px solid gray;
   10.11 +    padding: 2px 4px;
   10.12 +}
   10.13 +
   10.14 +thead {
   10.15 +    background-color: silver;
   10.16 +    font-weight: bold;
   10.17 +}
   10.18 +