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

Cosmetics

parent 8506af41
......@@ -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) {
......
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