Commit d97c7dba authored by Dr. Martin Goik's avatar Dr. Martin Goik

New project exporting Rdbms data to XML

parent 3a1eced0
.project
.classpath
/.settings/
/target/
A1.log
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="catalog">
<xs:complexType>
<xs:sequence>
<xs:element ref="product" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="product">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="description" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="age" type="xs:int" minOccurs="0" maxOccurs="1"/>
</xs:sequence>
<xs:attribute name="id" type="xs:ID" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
DROP TABLE IF EXISTS Description;
DROP TABLE IF EXISTS Product;
CREATE TABLE Product (
id CHAR(20) NOT NULL PRIMARY KEY
,name VARCHAR(255) NOT NULL
,age SMALLINT
);
CREATE TABLE Description (
product CHAR(20) NOT NULL REFERENCES Product
,orderIndex int NOT NULL -- preserving the order of descriptions belonging to a given product
,text VARCHAR(255) NOT NULL
,UNIQUE(product, orderIndex)
);
-- example data corresponding to products.xml --
-- Product lacking age property --
INSERT INTO Product (id, name) VALUES ('mpt', 'Monkey Picked Tea');
INSERT INTO Description VALUES('mpt', 0, 'Picked only by specially trained monkeys');
INSERT INTO Description VALUES('mpt', 1, 'Rare wild Chinese tea');
INSERT INTO Product VALUES ('instantTent', '4-Person Instant Tent', 15);
INSERT INTO Description VALUES('instantTent', 0, 'Exclusive WeatherTec system.');
INSERT INTO Description VALUES('instantTent', 1, '4-person, 1-room tent');
INSERT INTO Description VALUES('instantTent', 2, 'Pre-attached tent poles');
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.hdm-stuttgart.mi.sda1</groupId>
<artifactId>rdbms2catalog</artifactId>
<version>0.8</version>
<packaging>jar</packaging>
<name>rdbms2catalog</name>
<url>http://www.mi.hdm-stuttgart.de/freedocs</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom2</artifactId>
<version>2.0.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.1</version>
<configuration />
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>de.hdm_stuttgart.mi.sda1.catalog2sax.App</Main-Class>
</manifestEntries>
</transformer>
</transformers>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<catalog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="Schema/catalog.xsd">
<product id="mpt">
<name>Monkey Picked Tea</name>
<description>Rare wild Chinese tea</description>
<description>Picked only by specially trained monkeys</description>
</product>
<product id="instantTent">
<name>4-Person Instant Tent</name>
<description>4-person, 1-room tent</description>
<description>Pre-attached tent poles</description>
<description>Exclusive WeatherTec system.</description>
<age>15</age>
</product>
</catalog>
\ No newline at end of file
package de.hdm_stuttgart.mi.sda1.sql2catalog;
/**
* Project wide helper methods
*
*/
public class Helper {
/**
* Write message to stderr and exit with given error code
* @param errMsg
* @param errorCode
*/
public static void exitWithErrorMessage(final String errMsg, int errorCode) {
System.err.println(errMsg);
System.exit(errorCode);
}
}
package de.hdm_stuttgart.mi.sda1.sql2catalog;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
public class Messages {
private static final String BUNDLE_NAME = "de.hdm_stuttgart.mi.sda1.sql2catalog.messages"; //$NON-NLS-1$
private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle
.getBundle(BUNDLE_NAME);
private Messages() {
}
public static String getString(String key) {
try {
return RESOURCE_BUNDLE.getString(key);
} catch (MissingResourceException e) {
return '!' + key + '!';
}
}
}
package de.hdm_stuttgart.mi.sda1.sql2catalog;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
import org.xml.sax.SAXException;
/**
* A simple SAX parser demo
*
*/
public class Rdbms2Xml {
private static final Logger log = LogManager.getLogger(Rdbms2Xml.class);
/**
* @param args
* Unused
* @throws SAXException
* @throws ParserConfigurationException
* @throws IOException
*/
public static void main(String[] args) throws ParserConfigurationException,
SAXException, IOException {
// Register Driver
final String sqlDriverClassName = Messages.getString("Xml2Rdbms.jdbcDriverName");
try {
Class.forName(sqlDriverClassName);
} catch (ClassNotFoundException e) {
Helper.exitWithErrorMessage(Messages.getString("Xml2Rdbms.errMsgUnableRegisterDriverClass") + sqlDriverClassName + "'", 1);
}
// Opening a JDBC connection
//
Connection conn = null;
final String jdbcConnectionUrl = Messages.getString("Xml2Rdbms.jdbcUrl");
final String userName = Messages.getString("Xml2Rdbms.jdbcUser");
try {
conn = DriverManager.getConnection(jdbcConnectionUrl, userName, Messages.getString("Xml2Rdbms.jdbcPasswd"));
} catch (SQLException e) {
Helper.exitWithErrorMessage(Messages.getString("Xml2Rdbms.errMsgUnableToConnect") + userName + "' to '" +
jdbcConnectionUrl + "'", 1);
}
RdbmsAccess rdbmsAccess = null;
try {
rdbmsAccess = new RdbmsAccess(conn);
} catch (SQLException e) {
Helper.exitWithErrorMessage(Messages.getString("Xml2Rdbms.errMsgUnableInitDataInsert"), 1);
}
final Element catalogElement = new Element("catalog");
try {
rdbmsAccess.appendProducts(catalogElement);
} catch (SQLException e) {
Helper.exitWithErrorMessage("Unable to read database:" + e.getLocalizedMessage(), 1);
}
// Write XML content to standard output
XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
outputter.output(catalogElement, System.out);
}
}
package de.hdm_stuttgart.mi.sda1.sql2catalog;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.jdom2.Element;
/**
* Reading product data
*
*/
public class RdbmsAccess {
final PreparedStatement selectProduct, selectDescription;
/**
* @param conn Destination RDBMS
* @throws SQLException
*/
public RdbmsAccess(final Connection conn) throws SQLException {
conn.setAutoCommit(false);
selectProduct = conn.prepareStatement("SELECT * FROM Product");
selectDescription = conn.prepareStatement("SELECT * FROM Description WHERE product = ? ORDER BY orderIndex");
}
public void appendProducts(final Element root) throws SQLException {
final ResultSet products = selectProduct.executeQuery();
while (products.next()) {
final String productId = products.getString("id");
final Element productElement = new Element("product");
root.addContent(productElement);
productElement.setAttribute("id", productId);
final Element nameElement = new Element("name");
productElement.addContent(nameElement);
nameElement.addContent(products.getString("name"));
addDescriptions(productId, productElement);
final int productAge = products.getInt("age");
if (!products.wasNull()) {
final Element ageElement = new Element("age");
productElement.addContent(ageElement);
ageElement.addContent("" + productAge);
}
}
products.close();
}
private void addDescriptions(final String productId, final Element productElement) throws SQLException {
selectDescription.setString(1, productId);
final ResultSet descriptions = selectDescription.executeQuery();
while (descriptions.next()) {
final Element descriptionElement = new Element("description");
productElement.addContent(descriptionElement);
descriptionElement.addContent(descriptions.getString("text"));
}
descriptions.close();
}
}
Xml2Rdbms.14='
Xml2Rdbms.errMsgUnableCloseRdbms=Unable to close RDBMS access
Xml2Rdbms.errMsgUnableInitDataInsert=Unable to initialize data inserter
Xml2Rdbms.errMsgUnableRegisterDriverClass=Unable to register driver class '
Xml2Rdbms.errMsgUnableToConnect=Unable to connect as user '
Xml2Rdbms.jdbcDriverName=com.mysql.jdbc.Driver
Xml2Rdbms.jdbcPasswd=XYZ
Xml2Rdbms.jdbcUrl=jdbc:mysql://localhost:3306/hdm
Xml2Rdbms.jdbcUser=hdmuser
Xml2Rdbms.xmlSourceFileName=products.xml
Xml2Rdbms.errMsgUnableCommitTransaction=Unable to commit transaction
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<File name="A1" fileName="A1.log" append="false">
<PatternLayout pattern="%t %-5p %c{2} - %m%n"/>
</File>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%C{2} (%F:%L) - %m%n"/>
</Console>
</Appenders>
<Loggers>
<!-- You my want to define class or package level per-logger rules -->
<Logger name="de.hdm_stuttgart.mi.sda1.catalog2sax.App" level="debug">
<AppenderRef ref="A1"/>
</Logger>
<Root level="debug">
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
</Configuration>
\ No newline at end of file
This diff is collapsed.
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