diff --git a/Klausuren/Sda1/2018Winter/Solve/.gitignore b/Klausuren/Sda1/2018Winter/Solve/.gitignore index 9b36bc511f68d6e52aeb8b8e7c5ddfe38ed74218..1c0837dafefd658e70bebe46f9daece143b477b6 100644 --- a/Klausuren/Sda1/2018Winter/Solve/.gitignore +++ b/Klausuren/Sda1/2018Winter/Solve/.gitignore @@ -1,3 +1,4 @@ +.idea /target/ /.settings/ .classpath diff --git a/Klausuren/Sda1/2018Winter/Solve/Schema/schema.sql b/Klausuren/Sda1/2018Winter/Solve/Schema/schema.sql index 5abcf8e0b4c958d75ed7f1c9fa3b7577502b7f98..dd4fafeed50dacb6d174e9cb6d32f3313a0fe72e 100644 --- a/Klausuren/Sda1/2018Winter/Solve/Schema/schema.sql +++ b/Klausuren/Sda1/2018Winter/Solve/Schema/schema.sql @@ -2,36 +2,46 @@ DROP TABLE IF EXISTS Para; DROP TABLE IF EXISTS Section; CREATE TABLE Section ( - id CHAR(100) PRIMARY KEY - ,title TEXT - ,parent CHAR(100) REFERENCES Section + id CHAR(100) + ,PRIMARY KEY(id) + ,title TEXT NOT NULL + ,parent CHAR(100) + ,FOREIGN KEY (parent) REFERENCES Section(id) ,revisionFlag CHAR(7) - ,orderValue INT NOT NULL + ,CHECK(revisionFlag in ('new', 'changed', 'deleted')) + ,orderValue INT AUTO_INCREMENT UNIQUE ); CREATE TABLE Para ( - section CHAR(100) NOT NULL REFERENCES Section + section CHAR(100) NOT NULL + ,FOREIGN KEY (section) REFERENCES Section(id) ,para LONGTEXT ,revisionFlag CHAR(7) - ,orderValue INT NOT NULL + ,CHECK(revisionFlag in ('new', 'changed', 'deleted')) + ,orderValue INT AUTO_INCREMENT UNIQUE ); -INSERT INTO Section VALUES ('categories', 'XML Document categories', NULL, 'new', 1); -INSERT INTO Section VALUES ('wellFormed', 'Well-formed documents', 'categories' ,NULL, 2); +INSERT INTO Section (id, title, parent, revisionFlag) VALUES ('categories', 'XML Document categories', NULL, 'new'); +INSERT INTO Section (id, title, parent, revisionFlag) VALUES ('wellFormed', 'Well-formed documents', 'categories', NULL); +INSERT INTO Section (id, title, parent, revisionFlag) VALUES ('valid', 'Valid documents', 'categories', NULL); +INSERT INTO Section (id, title, parent, revisionFlag) VALUES ('api', 'Available APIs', NULL, 'deleted'); -INSERT INTO Para VALUES ( - 'categories', 'Documents belonging to this category do not adhere to any schema.','changed', 3); +INSERT INTO Para (section, para, revisionFlag) VALUES + ('categories', 'Documents belonging to this category do not adhere to any schema.','changed'); -INSERT INTO Section VALUES ('valid', 'Valid documents', 'categories', NULL, 4); -INSERT INTO Para VALUES ('valid', 'Valid documents require a schema.', NULL, 5); -INSERT INTO Para VALUES ('valid', 'Common standards are DTD, XML Schema and Relax-NG.',NULL, 6); +INSERT INTO Para (section, para, revisionFlag) VALUES + ('valid', 'Valid documents require a schema.', NULL); -INSERT INTO Section VALUES ('api', 'Available APIs', NULL, 'deleted', 7); -INSERT INTO Para VALUES ('api', 'We introduce SAX and DOM here.', NULL, 8); -INSERT INTO Para VALUES ('api', 'Some APIs offer multiple language bindings.', NULL, 9); +INSERT INTO Para (section, para, revisionFlag) VALUES + ('valid', 'Common standards are DTD, XML Schema and Relax-NG.', NULL); +INSERT INTO Para (section, para, revisionFlag) VALUES + ('api', 'We introduce SAX and DOM here.', NULL); + +INSERT INTO Para (section, para, revisionFlag) VALUES + ('api', 'Some APIs offer multiple language bindings.', NULL); SELECT * FROM Section; -SELECT * FROM Para; +SELECT * FROM Para; \ No newline at end of file diff --git a/Klausuren/Sda1/2018Winter/Solve/src/main/java/de/hdm_stuttgart/mi/sda1/Book2Rdbms.java b/Klausuren/Sda1/2018Winter/Solve/src/main/java/de/hdm_stuttgart/mi/sda1/Book2Rdbms.java index 62daf216697871a07b59e3f97fe2ac0792883943..e547bc65868b0a8f924624f5e57ee453d637734f 100644 --- a/Klausuren/Sda1/2018Winter/Solve/src/main/java/de/hdm_stuttgart/mi/sda1/Book2Rdbms.java +++ b/Klausuren/Sda1/2018Winter/Solve/src/main/java/de/hdm_stuttgart/mi/sda1/Book2Rdbms.java @@ -10,18 +10,27 @@ import java.io.File; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; +import java.sql.PreparedStatement; import java.sql.SQLException; -public class Book2Rdbms { +public class Book2Rdbms implements AutoCloseable{ private Connection conn; + private PreparedStatement insertSection, insertPara; + private Document doc; public Book2Rdbms() { try { conn = DriverManager.getConnection( "jdbc:mysql://localhost/hdm?serverTimezone=UTC", "hdmuser", "XYZ"); + insertSection = conn.prepareStatement( + "INSERT INTO Section (id, title, parent, revisionFlag) VALUES (?, ?, ?, ?);"); + + insertPara = conn.prepareStatement( + "INSERT INTO Para (section, para, revisionFlag) VALUES (?, ?, ?)"); + } catch (SQLException e) { System.err.println("Unable to establish connection: " + e); System.exit(1); @@ -46,19 +55,49 @@ public class Book2Rdbms { final XPathExpression<Element> xpathSearchPara = XPathFactory.instance().compile( - "//para" , + "/book/section" , new ElementFilter(), null , Namespace.getNamespace(null)); - System.out.println("Java stream:"); - xpathSearchPara.evaluate(doc).stream(). - map(Element::getText).forEach(System.out::println); + xpathSearchPara.evaluate(doc).forEach(this::handleSection); + } + private void handleSection(final Element section) { // Top level sections + handleSection(null, section); // No parent. + } - System.out.println("\nSimple loop:"); - for (final Element e: xpathSearchPara.evaluate(doc)) { - System.out.println(e.getText()); + private void handleSection(final String parentId, final Element section) { + try { + insertSection.setString(1, section.getAttributeValue("id")); + insertSection.setString(2, section.getChildText("title")); + insertSection.setString(3, parentId); + insertSection.setString(4, section.getAttributeValue("revisionflag")); + insertSection.execute(); + + section.getChildren("section").forEach( // recurse to child <section> elements + s -> handleSection(section.getAttributeValue("id") ,s)); + + section.getChildren("para").forEach( p -> { + try { + insertPara.setString(1, section.getAttributeValue("id")); + insertPara.setString(2, p.getValue()); + insertPara.setString(3, p.getAttributeValue("revisionflag")); + insertPara.execute(); + } catch (final SQLException e) { + System.err.println("Unable to insert <para>: " + e); + System.exit(1); + } + } + ); + } catch (final SQLException e) { + e.printStackTrace(); + System.err.println("Unable to insert <section>: " + e); + System.exit(1); } + } + @Override + public void close() throws Exception { + conn.close(); } -} +} \ No newline at end of file