diff --git a/Sda1/Etest/rdbms2catalog/Schema/schema.sql b/Sda1/Etest/rdbms2catalog/Schema/schema.sql index d08f1730625e067ad07db13788988f67d465635e..3f3d2c86f8eba5f7f682d060d9b8a04a2b77b8d9 100644 --- a/Sda1/Etest/rdbms2catalog/Schema/schema.sql +++ b/Sda1/Etest/rdbms2catalog/Schema/schema.sql @@ -1,4 +1,4 @@ -DROP TABLE IF EXISTS Description; +DROP TABLE IF EXISTS Student; DROP TABLE IF EXISTS StudyCourse; CREATE TABLE StudyCourse ( @@ -12,7 +12,7 @@ CREATE TABLE StudyCourse ( CREATE TABLE Student ( id INTEGER NOT NULL PRIMARY KEY - ,fullName VARCHAR(255) + ,fullName VARCHAR(255) NOT NULL ,email VARCHAR(255) NOT NULL UNIQUE ,studyCourse INTEGER ,FOREIGN KEY(studyCourse) REFERENCES StudyCourse(id) diff --git a/Sda1/Etest/rdbms2catalog/src/main/java/de/hdm_stuttgart/mi/sda1/sql2catalog/Rdbms2Xml.java b/Sda1/Etest/rdbms2catalog/src/main/java/de/hdm_stuttgart/mi/sda1/sql2catalog/Rdbms2Xml.java index 31ebb18b92c39ebd6bce2e3d013a6cc9a2e27533..f8cc7c88c22bfbb12c726758447893891666b3ce 100644 --- a/Sda1/Etest/rdbms2catalog/src/main/java/de/hdm_stuttgart/mi/sda1/sql2catalog/Rdbms2Xml.java +++ b/Sda1/Etest/rdbms2catalog/src/main/java/de/hdm_stuttgart/mi/sda1/sql2catalog/Rdbms2Xml.java @@ -5,31 +5,20 @@ 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 + * @param args Unused */ - public static void main(String[] args) throws ParserConfigurationException, - SAXException, IOException { + public static void main(String[] args) { @@ -41,7 +30,6 @@ public class Rdbms2Xml { Helper.exitWithErrorMessage(Messages.getString("Xml2Rdbms.errMsgUnableRegisterDriverClass") + sqlDriverClassName + "'", 1); } - // Opening a JDBC connection // Connection conn = null; @@ -54,7 +42,6 @@ public class Rdbms2Xml { Helper.exitWithErrorMessage(Messages.getString("Xml2Rdbms.errMsgUnableToConnect") + userName + "' to '" + jdbcConnectionUrl + "'", 1); } - RdbmsAccess rdbmsAccess = null; try { rdbmsAccess = new RdbmsAccess(conn); @@ -66,16 +53,21 @@ public class Rdbms2Xml { final Element studyCourses = new Element("studyCourses"); universityElement.addContent(studyCourses); final Element students = new Element("students"); + universityElement.addContent(students); try { - rdbmsAccess.appendProducts(universityElement); + rdbmsAccess.appendStudyCourses(studyCourses); + rdbmsAccess.appendStudents(students); } 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(universityElement, System.out); + try { + outputter.output(universityElement, System.out); + } catch (IOException e) { + Helper.exitWithErrorMessage("Unable to write XML data:" + e.getLocalizedMessage(), 1); + } } - } diff --git a/Sda1/Etest/rdbms2catalog/src/main/java/de/hdm_stuttgart/mi/sda1/sql2catalog/RdbmsAccess.java b/Sda1/Etest/rdbms2catalog/src/main/java/de/hdm_stuttgart/mi/sda1/sql2catalog/RdbmsAccess.java index d13fc1070fc39906b9af984eb3be977bd7248bfd..e2f10f5669f68bd434731235e684930394072dff 100644 --- a/Sda1/Etest/rdbms2catalog/src/main/java/de/hdm_stuttgart/mi/sda1/sql2catalog/RdbmsAccess.java +++ b/Sda1/Etest/rdbms2catalog/src/main/java/de/hdm_stuttgart/mi/sda1/sql2catalog/RdbmsAccess.java @@ -13,7 +13,7 @@ import org.jdom2.Element; */ public class RdbmsAccess { - final PreparedStatement selectProduct, selectDescription; + final PreparedStatement selectStudyCourses, selectStudents; /** * @param conn Destination RDBMS @@ -21,43 +21,43 @@ public class RdbmsAccess { */ 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"); + selectStudyCourses = conn.prepareStatement("SELECT * FROM StudyCourse"); + selectStudents = conn.prepareStatement("SELECT * FROM Student"); } - public void appendProducts(final Element root) throws SQLException { - final ResultSet products = selectProduct.executeQuery(); - - while (products.next()) { - final int productId = products.getInt("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 int productId, final Element productElement) throws SQLException { - selectDescription.setInt(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(); - } + public void appendStudyCourses(final Element parent) throws SQLException { + final ResultSet studyCourses = selectStudyCourses.executeQuery(); + + while (studyCourses.next()) { + final Element studyCourse = new Element("studyCourse"); + parent.addContent(studyCourse); + studyCourse.setAttribute("id", "" + studyCourses.getInt("id")) + .addContent(new Element("name").setText(studyCourses.getString("name"))) + .setAttribute("program",studyCourses.getString("program")) + ; + final String shortName = studyCourses.getString("shortName"); + if (!studyCourses.wasNull()){ + studyCourse.addContent(new Element("shortName").setText(shortName)); + } + } + studyCourses.close(); + } + + public void appendStudents(final Element parent) throws SQLException { + final ResultSet students = selectStudents.executeQuery(); + + while (students.next()) { + final Element student = new Element("student"); + parent.addContent(student); + student.setAttribute("id", "" + students.getInt("id")) + .addContent(new Element("fullName").setText(students.getString("fullName"))) + .addContent(new Element("email").setText(students.getString("email"))) + ; + final int studyCourse = students.getInt("studyCourse"); + if (!students.wasNull()){ + student.setAttribute("studyCourse", "" + studyCourse); + } + } + students.close(); + } }