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 0ecf221676646932a2c78f2b0439ee90f63b992c..868d35129201ac2db257ffa499ea20cbd6ecd25c 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 @@ -12,6 +12,15 @@ import java.sql.SQLException; public class Book2Rdbms implements AutoCloseable { + static private final String + insertSectionSql = + "INSERT INTO Section (id, title, parent, revisionflag)" + + " VALUES (?, ?, ?, ?);", + + insertParaSql = + "INSERT INTO Para (parent, para, revisionflag) \n" + + "VALUES ( ?, ?, ?);"; + private Connection conn; private PreparedStatement insertSection, insertPara; @@ -22,11 +31,9 @@ public class Book2Rdbms implements AutoCloseable { try { conn = DriverManager.getConnection( "jdbc:postgresql://localhost/hdm", "hdmuser", "XYZ"); - insertSection = conn.prepareStatement( - "INSERT INTO Section (id, title, parent, revisionFlag) VALUES (?, ?, ?, ?);"); + insertSection = conn.prepareStatement(insertSectionSql); - insertPara = conn.prepareStatement( - "INSERT INTO Para (section, para, revisionFlag) VALUES (?, ?, ?)"); + insertPara = conn.prepareStatement(insertParaSql); } catch (SQLException e) { System.err.println("Unable to establish connection: " + e); @@ -55,6 +62,7 @@ public class Book2Rdbms implements AutoCloseable { private void handleSection(final String parentId, final Element section) { try { final String currentSectionId = section.getAttributeValue("id"); + insertSection.setString(1, currentSectionId); insertSection.setString(2, section.getChildText("title")); insertSection.setString(3, parentId); @@ -62,19 +70,9 @@ public class Book2Rdbms implements AutoCloseable { insertSection.execute(); - section.getChildren("para").forEach(p -> { - try { - insertPara.setString(1, currentSectionId); - insertPara.setString(2, p.getValue()); - insertPara.setString(3, p.getAttributeValue("revisionflag")); - insertPara.execute(); - } catch (final SQLException e) { - e.printStackTrace(); - System.err.println("Unable to insert <para>: " + e); - System.exit(1); - } - } - ); + // add <para> children of current section + handleParaChildren(section); + // recurse to <section> children section.getChildren("section").forEach(s -> handleSection(currentSectionId, s)); } catch (final SQLException e) { @@ -84,8 +82,27 @@ public class Book2Rdbms implements AutoCloseable { } } + + public void handleParaChildren(final Element section) { + + final String currentSectionId = section.getAttributeValue("id"); + section.getChildren("para").forEach( + p -> { + try { + insertPara.setString(1, currentSectionId); + insertPara.setString(2, p.getValue()); + insertPara.setString(3, p.getAttributeValue("revisionflag")); + + insertPara.execute(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + ); + } + @Override - public void close(){ + public void close() { try { conn.close(); } catch (final SQLException e) {