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

working XML export

parent b9e0eb11
No related branches found
No related tags found
No related merge requests found
DROP TABLE IF EXISTS Description; DROP TABLE IF EXISTS Student;
DROP TABLE IF EXISTS StudyCourse; DROP TABLE IF EXISTS StudyCourse;
CREATE TABLE StudyCourse ( CREATE TABLE StudyCourse (
...@@ -12,7 +12,7 @@ CREATE TABLE StudyCourse ( ...@@ -12,7 +12,7 @@ CREATE TABLE StudyCourse (
CREATE TABLE Student ( CREATE TABLE Student (
id INTEGER NOT NULL PRIMARY KEY id INTEGER NOT NULL PRIMARY KEY
,fullName VARCHAR(255) ,fullName VARCHAR(255) NOT NULL
,email VARCHAR(255) NOT NULL UNIQUE ,email VARCHAR(255) NOT NULL UNIQUE
,studyCourse INTEGER ,studyCourse INTEGER
,FOREIGN KEY(studyCourse) REFERENCES StudyCourse(id) ,FOREIGN KEY(studyCourse) REFERENCES StudyCourse(id)
......
...@@ -5,31 +5,20 @@ import java.sql.Connection; ...@@ -5,31 +5,20 @@ import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.SQLException; 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.Element;
import org.jdom2.output.Format; import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter; import org.jdom2.output.XMLOutputter;
import org.xml.sax.SAXException;
/** /**
* A simple SAX parser demo * A simple SAX parser demo
* *
*/ */
public class Rdbms2Xml { public class Rdbms2Xml {
private static final Logger log = LogManager.getLogger(Rdbms2Xml.class);
/** /**
* @param args * @param args Unused
* Unused
* @throws SAXException
* @throws ParserConfigurationException
* @throws IOException
*/ */
public static void main(String[] args) throws ParserConfigurationException, public static void main(String[] args) {
SAXException, IOException {
...@@ -41,7 +30,6 @@ public class Rdbms2Xml { ...@@ -41,7 +30,6 @@ public class Rdbms2Xml {
Helper.exitWithErrorMessage(Messages.getString("Xml2Rdbms.errMsgUnableRegisterDriverClass") + sqlDriverClassName + "'", 1); Helper.exitWithErrorMessage(Messages.getString("Xml2Rdbms.errMsgUnableRegisterDriverClass") + sqlDriverClassName + "'", 1);
} }
// Opening a JDBC connection // Opening a JDBC connection
// //
Connection conn = null; Connection conn = null;
...@@ -54,7 +42,6 @@ public class Rdbms2Xml { ...@@ -54,7 +42,6 @@ public class Rdbms2Xml {
Helper.exitWithErrorMessage(Messages.getString("Xml2Rdbms.errMsgUnableToConnect") + userName + "' to '" + Helper.exitWithErrorMessage(Messages.getString("Xml2Rdbms.errMsgUnableToConnect") + userName + "' to '" +
jdbcConnectionUrl + "'", 1); jdbcConnectionUrl + "'", 1);
} }
RdbmsAccess rdbmsAccess = null; RdbmsAccess rdbmsAccess = null;
try { try {
rdbmsAccess = new RdbmsAccess(conn); rdbmsAccess = new RdbmsAccess(conn);
...@@ -66,16 +53,21 @@ public class Rdbms2Xml { ...@@ -66,16 +53,21 @@ public class Rdbms2Xml {
final Element studyCourses = new Element("studyCourses"); final Element studyCourses = new Element("studyCourses");
universityElement.addContent(studyCourses); universityElement.addContent(studyCourses);
final Element students = new Element("students"); final Element students = new Element("students");
universityElement.addContent(students);
try { try {
rdbmsAccess.appendProducts(universityElement); rdbmsAccess.appendStudyCourses(studyCourses);
rdbmsAccess.appendStudents(students);
} catch (SQLException e) { } catch (SQLException e) {
Helper.exitWithErrorMessage("Unable to read database:" + e.getLocalizedMessage(), 1); Helper.exitWithErrorMessage("Unable to read database:" + e.getLocalizedMessage(), 1);
} }
// Write XML content to standard output // Write XML content to standard output
XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat()); 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);
}
} }
} }
...@@ -13,7 +13,7 @@ import org.jdom2.Element; ...@@ -13,7 +13,7 @@ import org.jdom2.Element;
*/ */
public class RdbmsAccess { public class RdbmsAccess {
final PreparedStatement selectProduct, selectDescription; final PreparedStatement selectStudyCourses, selectStudents;
/** /**
* @param conn Destination RDBMS * @param conn Destination RDBMS
...@@ -21,43 +21,43 @@ public class RdbmsAccess { ...@@ -21,43 +21,43 @@ public class RdbmsAccess {
*/ */
public RdbmsAccess(final Connection conn) throws SQLException { public RdbmsAccess(final Connection conn) throws SQLException {
conn.setAutoCommit(false); conn.setAutoCommit(false);
selectProduct = conn.prepareStatement("SELECT * FROM Product"); selectStudyCourses = conn.prepareStatement("SELECT * FROM StudyCourse");
selectDescription = conn.prepareStatement("SELECT * FROM Description WHERE product = ? ORDER BY orderIndex"); selectStudents = conn.prepareStatement("SELECT * FROM Student");
} }
public void appendProducts(final Element root) throws SQLException { public void appendStudyCourses(final Element parent) throws SQLException {
final ResultSet products = selectProduct.executeQuery(); final ResultSet studyCourses = selectStudyCourses.executeQuery();
while (products.next()) { while (studyCourses.next()) {
final int productId = products.getInt("id"); final Element studyCourse = new Element("studyCourse");
final Element productElement = new Element("product"); parent.addContent(studyCourse);
root.addContent(productElement); studyCourse.setAttribute("id", "" + studyCourses.getInt("id"))
productElement.setAttribute("id", "" + productId); .addContent(new Element("name").setText(studyCourses.getString("name")))
.setAttribute("program",studyCourses.getString("program"))
final Element nameElement = new Element("name"); ;
productElement.addContent(nameElement); final String shortName = studyCourses.getString("shortName");
nameElement.addContent(products.getString("name")); if (!studyCourses.wasNull()){
studyCourse.addContent(new Element("shortName").setText(shortName));
addDescriptions(productId, productElement); }
}
final int productAge = products.getInt("age"); studyCourses.close();
if (!products.wasNull()) { }
final Element ageElement = new Element("age");
productElement.addContent(ageElement); public void appendStudents(final Element parent) throws SQLException {
ageElement.addContent("" + productAge); final ResultSet students = selectStudents.executeQuery();
}
} while (students.next()) {
products.close(); final Element student = new Element("student");
} parent.addContent(student);
student.setAttribute("id", "" + students.getInt("id"))
private void addDescriptions(final int productId, final Element productElement) throws SQLException { .addContent(new Element("fullName").setText(students.getString("fullName")))
selectDescription.setInt(1, productId); .addContent(new Element("email").setText(students.getString("email")))
final ResultSet descriptions = selectDescription.executeQuery(); ;
while (descriptions.next()) { final int studyCourse = students.getInt("studyCourse");
final Element descriptionElement = new Element("description"); if (!students.wasNull()){
productElement.addContent(descriptionElement); student.setAttribute("studyCourse", "" + studyCourse);
descriptionElement.addContent(descriptions.getString("text")); }
} }
descriptions.close(); students.close();
} }
} }
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