diff --git a/Doc/Sda1/jdbc.xml b/Doc/Sda1/jdbc.xml index c94da1a2a6d4fd9cf5123799bb55f676cf35ad05..ea204c383995a26356f79f2b2711ec6a4511407b 100644 --- a/Doc/Sda1/jdbc.xml +++ b/Doc/Sda1/jdbc.xml @@ -3634,113 +3634,12 @@ INSERT INTO Product VALUES('w-124', 110.40); <methodname>de.hdm_stuttgart.mi.sda1.rdbmsxml2html.XmlRdbms2Html.addPrices()</methodname>. This method modifies the <acronym xlink:href="http://www.w3.org/DOM">DOM</acronym> input tree - prior to applying the XSL. Prices are being inserting based on - data received from the RDBMS via <trademark - xlink:href="https://en.wikipedia.org/wiki/Java_Database_Connectivity">JDBC</trademark>.</para> + prior to applying the XSL by inserting data received from the + RDBMS:</para> - <programlisting language="java">package dom.xsl; -... -public class XmlRdbms2Html { - private final SAXBuilder builder = new SAXBuilder(); - - DbAccess db = new DbAccess(); - - final XSLTransformer transformer; - Document catalog; - - final org.jdom2.xpath.XPathExpression<Object> selectProducts = - XPathFactory.instance().compile("/catalog/product"); - - /** - * @param xslFilename the stylesheet being used for subsequent - * transformations by {@link #transform(String, String)}. - * - * @throws XSLTransformException - */ - public XmlRdbms2Html(final String xslFilename) - throws XSLTransformException { - builder.setErrorHandler(new MySaxErrorHandler(System.err)); - transformer = new XSLTransformer(xslFilename); - } - - /** - * The actual workhorse carrying out the transformation - * and adding prices from the database table. - * - * @param xmlInFilename input file to be transformed - * @param resultFilename the result file holding the generated HTML - * document - * @throws JDOMException The transformation may fail for various - * reasons. - * @throws IOException - */ - public void transform(final String xmlInFilename, - final String resultFilename) throws JDOMException, - IOException { - - catalog = builder.build(xmlInFilename); - - addPrices(); - - final Document htmlResult = transformer.transform(catalog); - - // Set formatting for the XML output - final Format outFormat = Format.getPrettyFormat(); - - // Serialize to console - final XMLOutputter printer = new XMLOutputter(outFormat); - printer.output(htmlResult, System.out); - - } - private void addPrices() { - final List<Object> products = selectProducts.evaluate( - catalog.getRootElement()); - - db.connect("jdbc:mysql://localhost:3306/hdm", "hdmuser", "XYZ"); - for (Object p: products) { - final Element product = (Element ) p; - final String productId = product.getAttributeValue("id"); - product.setAttribute("price", db.readPrice(productId)); - } - db.close(); - } -}</programlisting> - - <para>The method <code>addPrices(...)</code> utilizes our - RDBMS access class:</para> - - <programlisting language="java">package dom.xsl; -... -public class DbAccess { - public void connect(final String jdbcUrl, - final String userName, final String password) { - try { - conn = DriverManager.getConnection(jdbcUrl, userName, password); - priceQuery = conn.prepareStatement(sqlPriceQuery); - } catch (SQLException e) { - System.err.println("Unable to open connection to database:" + e);} - } - public String readPrice(final String articleNumber) { - String result; - try { - priceQuery.setString(1, articleNumber); - final ResultSet rs = priceQuery.executeQuery(); - if (rs.next()) { - result = rs.getString("price"); - } else { - result = "No price available for article '" + articleNumber + "'"; - } - } catch (SQLException e) { - result = "Error reading price for article '" + articleNumber + - "':" + e; - } - return result; - } - ... -}</programlisting> - - <para>Of course the connection details should be moved to a - configuration file.</para> + <annotation role="make"> + <para role="eclipse">Sda1/rdbmsXml2Html</para> + </annotation> </answer> </qandaentry> </qandadiv> diff --git a/P/Sda1/rdbmsXml2Html/src/main/java/de/hdm_stuttgart/mi/sda1/rdbmsxml2html/XmlRdbms2Html.java b/P/Sda1/rdbmsXml2Html/src/main/java/de/hdm_stuttgart/mi/sda1/rdbmsxml2html/XmlRdbms2Html.java index 96b4d9df87b665ef44535c765619d426d31a4a13..19f63f58fc3888c24057f52f21a8b2c44878b5cf 100644 --- a/P/Sda1/rdbmsXml2Html/src/main/java/de/hdm_stuttgart/mi/sda1/rdbmsxml2html/XmlRdbms2Html.java +++ b/P/Sda1/rdbmsXml2Html/src/main/java/de/hdm_stuttgart/mi/sda1/rdbmsxml2html/XmlRdbms2Html.java @@ -2,12 +2,10 @@ package de.hdm_stuttgart.mi.sda1.rdbmsxml2html; import java.io.IOException; import java.io.PrintStream; -import java.util.List; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.jdom2.Document; -import org.jdom2.Element; import org.jdom2.JDOMException; import org.jdom2.input.SAXBuilder; import org.jdom2.output.Format; @@ -22,16 +20,16 @@ import org.jdom2.xpath.XPathFactory; * */ public class XmlRdbms2Html { - + static private final Logger log = LogManager.getLogger(XmlRdbms2Html.class); - + private final SAXBuilder builder = new SAXBuilder(); final DbAccess db = new DbAccess(); - + final XSLTransformer transformer; Document catalog; - + final org.jdom2.xpath.XPathExpression<Object> selectProducts = XPathFactory.instance().compile("/catalog/product"); @@ -45,7 +43,7 @@ public class XmlRdbms2Html { builder.setErrorHandler(new MySaxErrorHandler(System.err)); transformer = new XSLTransformer(xslFilename); } - + /** * The actual workhorse carrying out the transformation * and adding prices from the database table. @@ -67,7 +65,7 @@ public class XmlRdbms2Html { log.fatal("Unable to transform xml to html:", e); } } - + static void serializeHtml(final Document htmlResult, final String htmlResultFilename) { // Set formatting for the XML output final Format outFormat = Format.getPrettyFormat(); @@ -80,16 +78,15 @@ public class XmlRdbms2Html { System.exit(1); } } - - private void addPrices() { - final List<Element> products = catalog.getRootElement().getChildren("product"); - + private void addPrices() { db.connect("jdbc:mysql://localhost:3306/hdm", "hdmuser", "XYZ"); - for (Element product: products) { - final String productId = product.getAttributeValue("id"); - product.setAttribute("price", db.readPrice(productId)); - } + + catalog.getRootElement(). + getChildren("product"). + stream(). + forEach(p -> p.setAttribute("price", db.readPrice(p.getAttributeValue("id")))); + db.close(); } }