Skip to content
Snippets Groups Projects
Commit 4e25ba7c authored by Goik Martin's avatar Goik Martin
Browse files

Exam solution

parent e3e58d19
No related branches found
No related tags found
No related merge requests found
.idea
/target/
/.settings/
.classpath
......
......@@ -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
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment