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

Cosmetics, Auto close connection

parent b94abc8a
No related branches found
No related tags found
No related merge requests found
......@@ -2,10 +2,10 @@ import de.hdm_stuttgart.mi.sda1.Book2Rdbms;
public class Driver {
public static void main(String[] args) {
final Book2Rdbms book2rdbms = new Book2Rdbms();
book2rdbms.execute();
public static void main(String[] args) {
try (final Book2Rdbms book2rdbms = new Book2Rdbms()) {
book2rdbms.execute();
}
}
}
}
\ No newline at end of file
package de.hdm_stuttgart.mi.sda1;
import org.jdom2.*;
import org.jdom2.filter.ElementFilter;
import org.jdom2.input.SAXBuilder;
import org.jdom2.xpath.XPathExpression;
import org.jdom2.xpath.XPathFactory;
import java.io.File;
import java.io.IOException;
......@@ -13,91 +10,85 @@ import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class Book2Rdbms implements AutoCloseable{
public class Book2Rdbms implements AutoCloseable {
private Connection conn;
private Connection conn;
private PreparedStatement insertSection, insertPara;
private PreparedStatement insertSection, insertPara;
private Document doc;
private Element book;
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 (?, ?, ?, ?);");
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 (?, ?, ?)");
insertPara = conn.prepareStatement(
"INSERT INTO Para (section, para, revisionFlag) VALUES (?, ?, ?)");
} catch (SQLException e) {
System.err.println("Unable to establish connection: " + e);
System.exit(1);
}
final SAXBuilder builder = new SAXBuilder();
try {
doc = builder.build(new File("Schema/sampledata.xml"));
//getClass().getClassLoader().getResource("Schema/sampledata.xml"));
} catch (JDOMException e) {
e.printStackTrace();
System.err.println("Unable to parse input: " + e);
System.exit(1);
} catch (IOException e) {
e.printStackTrace();
System.err.println("Unable to read input: " + e);
System.exit(1);
}
} catch (SQLException e) {
System.err.println("Unable to establish connection: " + e);
System.exit(1);
}
public void execute() {
final XPathExpression<Element> xpathSearchPara =
XPathFactory.instance().compile(
"/book/section" ,
new ElementFilter(),
null ,
Namespace.getNamespace(null));
xpathSearchPara.evaluate(doc).forEach(this::handleSection);
}
private void handleSection(final Element section) { // Top level sections
handleSection(null, section); // No parent.
final SAXBuilder builder = new SAXBuilder();
try {
book = builder.build(new File("Schema/sampledata.xml")).getRootElement();
} catch (JDOMException e) {
e.printStackTrace();
System.err.println("Unable to parse input: " + e);
System.exit(1);
} catch (IOException e) {
e.printStackTrace();
System.err.println("Unable to read input: " + e);
System.exit(1);
}
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);
}
}
public void execute() {
// Top level <section> elements do not have a parent --> null.
book.getChildren("section").forEach(s -> handleSection(null, s));
}
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);
insertSection.setString(4, section.getAttributeValue("revisionflag"));
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) {
System.err.println("Unable to insert <para>: " + e);
System.exit(1);
}
}
);
// recurse to <section> children
section.getChildren("section").forEach(s -> handleSection(currentSectionId, s));
} 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();
}
@Override
public void close(){
try {
conn.close();
} catch (final SQLException e) {
e.printStackTrace();
}
}
}
\ 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