Commit 8a842c9e authored by Dr. Martin Goik's avatar Dr. Martin Goik

Cosmetics

parent 8506af41
...@@ -12,6 +12,15 @@ import java.sql.SQLException; ...@@ -12,6 +12,15 @@ import java.sql.SQLException;
public class Book2Rdbms implements AutoCloseable { 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 Connection conn;
private PreparedStatement insertSection, insertPara; private PreparedStatement insertSection, insertPara;
...@@ -22,11 +31,9 @@ public class Book2Rdbms implements AutoCloseable { ...@@ -22,11 +31,9 @@ public class Book2Rdbms implements AutoCloseable {
try { try {
conn = DriverManager.getConnection( conn = DriverManager.getConnection(
"jdbc:postgresql://localhost/hdm", "hdmuser", "XYZ"); "jdbc:postgresql://localhost/hdm", "hdmuser", "XYZ");
insertSection = conn.prepareStatement( insertSection = conn.prepareStatement(insertSectionSql);
"INSERT INTO Section (id, title, parent, revisionFlag) VALUES (?, ?, ?, ?);");
insertPara = conn.prepareStatement( insertPara = conn.prepareStatement(insertParaSql);
"INSERT INTO Para (section, para, revisionFlag) VALUES (?, ?, ?)");
} catch (SQLException e) { } catch (SQLException e) {
System.err.println("Unable to establish connection: " + e); System.err.println("Unable to establish connection: " + e);
...@@ -55,6 +62,7 @@ public class Book2Rdbms implements AutoCloseable { ...@@ -55,6 +62,7 @@ public class Book2Rdbms implements AutoCloseable {
private void handleSection(final String parentId, final Element section) { private void handleSection(final String parentId, final Element section) {
try { try {
final String currentSectionId = section.getAttributeValue("id"); final String currentSectionId = section.getAttributeValue("id");
insertSection.setString(1, currentSectionId); insertSection.setString(1, currentSectionId);
insertSection.setString(2, section.getChildText("title")); insertSection.setString(2, section.getChildText("title"));
insertSection.setString(3, parentId); insertSection.setString(3, parentId);
...@@ -62,19 +70,9 @@ public class Book2Rdbms implements AutoCloseable { ...@@ -62,19 +70,9 @@ public class Book2Rdbms implements AutoCloseable {
insertSection.execute(); insertSection.execute();
section.getChildren("para").forEach(p -> { // add <para> children of current section
try { handleParaChildren(section);
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);
}
}
);
// recurse to <section> children // recurse to <section> children
section.getChildren("section").forEach(s -> handleSection(currentSectionId, s)); section.getChildren("section").forEach(s -> handleSection(currentSectionId, s));
} catch (final SQLException e) { } catch (final SQLException e) {
...@@ -84,8 +82,27 @@ public class Book2Rdbms implements AutoCloseable { ...@@ -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 @Override
public void close(){ public void close() {
try { try {
conn.close(); conn.close();
} catch (final SQLException e) { } catch (final SQLException e) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment