Commit 6ddf7fda authored by Dr. Martin Goik's avatar Dr. Martin Goik

Completing exercise

parent 0dc7cf5e
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Exercise 1</title>
</head>
<body><h2>Objective</h2><p>Transforming a relational into an XML
schema</p><h2>Preparations / Description</h2><ol>
<li>Download <a
href="/iliasData/goik/rdbmsStudents2xml_zcx9sazr3FFDS_++vcds/rdbmsStudents2xml.zip">rdbmsStudents2xml.zip</a>
and import it as a project into your Eclipse workspace.</li>
<li><p>Your imported project <code>rdbmsstudent2xml</code>
contains:</p><ul>
<li><p><strong><code>Schema/schema.sql</code> describing university
data:</strong></p><ul>
<li>A relational schema consisting of two related tables
<code>StudyCourse</code> and <code>Student</code>.</li>
<li><code>INSERT</code> statements providing sample data.</li>
</ul></li>
<li><p><strong><code>Schema/testdata.xml</code> corresponding to the
previously mentioned relational data being contained in
<code>Schema/schema.sql</code>.</strong></p></li>
<li><p><strong> <code>Schema/university.xsd</code>, a sample XML
schema to be extended</strong></p></li>
</ul></li>
</ol><h2>ToDo</h2><p>Complete <code>Schema/university.xsd</code> to match
all integrity constraints of the corresponding relational
<code>Schema/schema.sql</code>. The XML sample
<code>Schema/testdata.xml</code> may guide you but may be misleading, too:
Examine the two <code>CREATE TABLE</code> statements thoroughly in order to
find all data integrity constraints.</p><p>On completion upload your file
<code>university.xsd</code> to the exam system.</p></body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Exercise 2</title>
</head>
<body><h2>Objective</h2><p>Transforming relational data into XML
output.</p><h2>Preparations / Description</h2><ol>
<li>Download <a
href="/iliasData/goik/rdbmsStudents2xml_zcx9sazr3FFDS_++vcds/rdbmsStudents2xml.zip">rdbmsStudents2xml.zip</a>
and import it as a project into your Eclipse workspace.</li>
<li><p>Your imported project <code>rdbmsstudent2xml</code>
contains:</p><ul>
<li><p><strong><code>Schema/schema.sql</code> describing university
data:</strong></p><ul>
<li>A relational schema consisting of two related tables
<code>StudyCourse</code> and <code>Student</code>.</li>
<li><code>INSERT</code> statements providing sample data.</li>
</ul></li>
<li><p><strong>A driver class
<code>de.hdm_stuttgart.mi/sda1.sql2catalog.Rdbms2Xml</code>:</strong></p><p>On
Java execution this class currently just produces:</p><pre>&lt;university&gt;
&lt;studyCourses/&gt;
&lt;students/&gt;
&lt;/university&gt;</pre></li>
<li><p><strong><code>Schema/testdata.xml</code> corresponding to the
previously mentioned relational data being contained in
<code>Schema/schema.sql</code>.</strong></p></li>
</ul></li>
</ol><h2>ToDo</h2><p>Extend
<code>de.hdm_stuttgart.mi/sda1.sql2catalog.Rdbms2Xml</code> to generate
suitable XML data from relational sources. Data being contained in
<code>Schema/schema.sql</code> shall for example be transformed to
<code>Schema/testdata.xml</code>. But off course your implementation is
expected to work for arbitrary relational data input being consistent with
the current relational schema.</p><p>It might be a good idea to extend class
<code>de.hdm_stuttgart.mi.sda1.sql2catalog.RdbmsAccess</code> implementing a
database access layer.</p><p>On completion export your project as a
<code>.zip</code> file and upload it. Only the last uploaded file will
become subject to marking.</p></body>
</html>
.project
.classpath
/.settings/
/target/
A1.log
\ No newline at end of file
DROP TABLE IF EXISTS Student;
DROP TABLE IF EXISTS StudyCourse;
CREATE TABLE StudyCourse (
id INTEGER NOT NULL PRIMARY KEY
,name VARCHAR(255) NOT NULL
,shortName CHAR(3)
,program CHAR(1) /* Bachelor or Master program? */
,CHECK (program IN ('B', 'M')) /* Mysql does not really honour CHECK constraints, but pretend it does! */
) ENGINE=InnoDB;
CREATE TABLE Student (
id INTEGER NOT NULL PRIMARY KEY
,fullName VARCHAR(255) NOT NULL
,email VARCHAR(255) NOT NULL UNIQUE
,studyCourse INTEGER
,FOREIGN KEY(studyCourse) REFERENCES StudyCourse(id)
) ENGINE=InnoDB;
-- example data corresponding to university.xml --
INSERT INTO StudyCourse VALUES (1, 'Mobile Media', 'MMB', 'B');
INSERT INTO StudyCourse VALUES (2, 'Computer Science in Media', NULL, 'M');
INSERT INTO Student VALUES(33, 'Jim Bone', 'bone@foo.org', NULL);
INSERT INTO Student VALUES(40, 'Tracy Adams', 'adams@security.com', 2);
-- Selects --
SELECT * FROM Student;
SELECT * FROM StudyCourse;
<?xml version="1.0" encoding="UTF-8"?>
<university xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="university.xsd">
<studyCourses>
<studyCourse id="1" program="B">
<name>Mobile Media</name>
<shortName>MMB</shortName>
</studyCourse>
<studyCourse id="2" program="M">
<name>Computer Science in Media</name>
</studyCourse>
</studyCourses>
<students>
<student id="33">
<fullName>Jim Bone</fullName>
<email>bone@foo.org</email>
</student>
<student id="40" studyCourse="2">
<fullName>Tracy Adams</fullName>
<email>adams@security.com</email>
</student>
</students>
</university>
\ 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="university">
</xs:element>
</xs:schema>
<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>rdbmsstudent2xml</artifactId>
<version>0.8</version>
<packaging>jar</packaging>
<name>rdbmsstudent2xml</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>
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.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
/**
* A simple SAX parser demo
*
*/
public class Rdbms2Xml {
/**
* @param args Unused
*/
public static void main(String[] args) {
// Register Driver
final String sqlDriverClassName = "com.mysql.jdbc.Driver";
try {
Class.forName(sqlDriverClassName);
} catch (ClassNotFoundException e) {
Helper.exitWithErrorMessage("Unable to register driver class '" + sqlDriverClassName + "'", 1);
}
// Opening a JDBC connection
//
Connection conn = null;
final String jdbcConnectionUrl = "jdbc:mysql://localhost:3306/hdm";
final String userName = "hdmuser";
try {
conn = DriverManager.getConnection(jdbcConnectionUrl, userName, "XYZ");
} catch (SQLException e) {
Helper.exitWithErrorMessage("Unable to connect as user '" + userName + "' to '" +
jdbcConnectionUrl + "'", 1);
}
final Element universityElement = new Element("university");
final Element studyCourses = new Element("studyCourses");
universityElement.addContent(studyCourses);
final Element students = new Element("students");
universityElement.addContent(students);
// Write XML content to standard output
XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
try {
outputter.output(universityElement, System.out);
} catch (IOException e) {
Helper.exitWithErrorMessage("Unable to write XML data:" + e.getLocalizedMessage(), 1);
}
}
}
package de.hdm_stuttgart.mi.sda1.sql2catalog;
import java.sql.Connection;
import java.sql.SQLException;
/**
* Reading study course and student data
*
*/
public class RdbmsAccess {
/**
* @param conn Destination RDBMS
*/
public RdbmsAccess(final Connection conn) {
}
}
<?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
......@@ -3,13 +3,12 @@ DROP TABLE IF EXISTS StudyCourse;
CREATE TABLE StudyCourse (
id INTEGER NOT NULL PRIMARY KEY
,name VARCHAR(255) NOT NULL
,shortName CHAR(3)
,name VARCHAR(255) NOT NULL UNIQUE
,shortName CHAR(3) UNIQUE
,program CHAR(1) /* Bachelor or Master program? */
,CHECK (program IN ('B', 'M')) /* Mysql does not really honour CHECK constraints, but pretend it does! */
) ENGINE=InnoDB;
CREATE TABLE Student (
id INTEGER NOT NULL PRIMARY KEY
,fullName VARCHAR(255) NOT NULL
......@@ -23,7 +22,6 @@ CREATE TABLE Student (
INSERT INTO StudyCourse VALUES (1, 'Mobile Media', 'MMB', 'B');
INSERT INTO StudyCourse VALUES (2, 'Computer Science in Media', NULL, 'M');
INSERT INTO Student VALUES(33, 'Jim Bone', 'bone@foo.org', NULL);
INSERT INTO Student VALUES(40, 'Tracy Adams', 'adams@security.com', 2);
......
<?xml version="1.0" encoding="UTF-8"?>
<university xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="university.xsd">
<studyCourses>
<studyCourse id="1" program="B">
<name>Mobile Media</name>
<shortName>MMB</shortName>
</studyCourse>
<studyCourse id="2" program="M">
<name>Computer Science in Media</name>
</studyCourse>
</studyCourses>
<students>
<student id="33">
<fullName>Jim Bone</fullName>
<email>bone@foo.org</email>
</student>
<student id="40" studyCourse="2">
<fullName>Tracy Adams</fullName>
<email>adams@security.com</email>
</student>
</students>
</university>
\ 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="studyCourse">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="shortName" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="3"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
<xs:attribute name="id" type="xs:int" use="required"/>
<xs:attribute name="program">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="B"/>
<xs:enumeration value="M"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="studyCourses">
<xs:complexType>
<xs:sequence>
<xs:element ref="studyCourse" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="student">
<xs:complexType>
<xs:sequence>
<xs:element name="fullName" type="xs:string"/>
<xs:element name="email" type="xs:string"/>
</xs:sequence>
<xs:attribute name="id" type="xs:int" use="required"/>
<xs:attribute name="studyCourse" type="xs:int" use="optional"/>
</xs:complexType>
</xs:element>
<xs:element name="students">
<xs:complexType>
<xs:sequence>
<xs:element ref="student" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="university">
<xs:complexType>
<xs:sequence>
<xs:element ref="studyCourses"/>
<xs:element ref="students"/>
</xs:sequence>
</xs:complexType>
<xs:key name="uniqueStudyCourse">
<xs:selector xpath="studyCourses/studyCourse"/>
<xs:field xpath="@id"/>
</xs:key>
<xs:keyref refer="uniqueStudyCourse" name="studyCourse2student">
<xs:selector xpath="students/student"/>
<xs:field xpath="@studyCourse"/>
</xs:keyref>
<xs:key name="uniqueStudent">
<xs:selector xpath="students/student"/>
<xs:field xpath="@id"/>
</xs:key>
</xs:element>
</xs:schema>
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