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

Using Postgresql, dependency upgrades

parent 095a2a6a
DROP TABLE IF EXISTS Competition; DROP TABLE IF EXISTS Competition;
DROP TABLE IF EXISTS Athlete; DROP TABLE IF EXISTS Athlete;
/* TODO add schema here */ CREATE TABLE Athlete (
id INT NOT NULL PRIMARY KEY
,fullName CHAR(20) NOT NULL
);
CREATE TABLE Competition (
id INT PRIMARY KEY
,athlete INT NOT NULL REFERENCES Athlete(id)
,time NUMERIC(6,2) NOT NULL
,badStart BOOLEAN NOT NULL
);
INSERT INTO Athlete VALUES(1, 'Jesse Owens'); INSERT INTO Athlete VALUES(1, 'Jesse Owens');
INSERT INTO Athlete VALUES(2, 'Tim Culver'); INSERT INTO Athlete VALUES(2, 'Tim Culver');
...@@ -9,13 +19,20 @@ INSERT INTO Athlete VALUES(3, 'Sid Gascoine'); ...@@ -9,13 +19,20 @@ INSERT INTO Athlete VALUES(3, 'Sid Gascoine');
INSERT INTO Athlete VALUES(4, 'John Hooker'); INSERT INTO Athlete VALUES(4, 'John Hooker');
INSERT INTO Athlete VALUES(5, 'Brad Duncan'); INSERT INTO Athlete VALUES(5, 'Brad Duncan');
/* 100 metre race results */ /* 100 metre race */
INSERT INTO Competition VALUES(1, 1, 10.23, 0); /* Jesse Owens */ INSERT INTO Competition VALUES(1, 1, 10.23, FALSE); /* Jesse Owens */
INSERT INTO Competition VALUES(2, 2, 11.55, 0); /* Tim Culver */ INSERT INTO Competition VALUES(2, 2, 11.55, FALSE); /* Tim Culver */
INSERT INTO Competition VALUES(3, 3, 11.94, 0); /* Sid Gascoine */ INSERT INTO Competition VALUES(3, 3, 11.94, FALSE); /* Sid Gascoine */
INSERT INTO Competition VALUES(4, 4, 10.02, 0); /* John Hooker */ INSERT INTO Competition VALUES(4, 4, 10.02, FALSE); /* John Hooker */
INSERT INTO Competition VALUES(5, 5, 10.84, 0); /* Brad Duncan */ INSERT INTO Competition VALUES(5, 5, 10.84, FALSE); /* Brad Duncan */
INSERT INTO Competition VALUES(6, 2, 10.02, 0); /* Tim Culver */ INSERT INTO Competition VALUES(6, 2, 10.02, FALSE); /* Tim Culver */
INSERT INTO Competition VALUES(7, 3, 10.49, 0); /* Sid Gascoine */ INSERT INTO Competition VALUES(7, 3, 10.49, FALSE); /* Sid Gascoine */
INSERT INTO Competition VALUES(8, 2, 9.35, 1); /* Tim Culver */ INSERT INTO Competition VALUES(8, 2, 9.35, TRUE); /* Tim Culver */
SELECT fullName, min(time) as time
FROM Competition, Athlete
WHERE Competition.athlete = Athlete.id
AND badStart = FALSE
GROUP BY (fullName)
ORDER BY time, fullName;
\ No newline at end of file
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
<name>Exam</name> <name>Exam</name>
<url>http://www.mi.hdm-stuttgart.de/freedocs/topic/de.hdm_stuttgart.mi.lectures/sd1SectUsingMaven.html</url> <url>https://freedocs.mi.hdm-stuttgart.de/sd1_sect_mavenCli.html</url>
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
...@@ -56,9 +56,9 @@ ...@@ -56,9 +56,9 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>mysql</groupId> <groupId>postgresql</groupId>
<artifactId>mysql-connector-java</artifactId> <artifactId>postgresql</artifactId>
<version>6.0.6</version> <version>9.1-901-1.jdbc4</version>
</dependency> </dependency>
<dependency> <dependency>
...@@ -109,16 +109,15 @@ ...@@ -109,16 +109,15 @@
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version> <version>3.8.1</version>
<configuration> <configuration>
<source>1.9</source> <source>11</source>
<target>1.9</target> <target>11</target>
<compilerArgument>-proc:none</compilerArgument> <compilerArgument>-proc:none</compilerArgument>
</configuration> </configuration>
</plugin> </plugin>
...@@ -126,22 +125,32 @@ ...@@ -126,22 +125,32 @@
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId> <artifactId>maven-javadoc-plugin</artifactId>
<version>3.0.1</version> <version>3.1.0</version>
<configuration> <configuration>
<links> <javaApiLinks>
<link>https://docs.oracle.com/javase/10/docs/api</link> <property>
</links> <name>api_11</name>
<value>https://klausur.mi.hdm-stuttgart.de/doc/openjdk-11-doc/api/</value>
</property>
</javaApiLinks>
<stylesheetfile>localstyles.css</stylesheetfile>
<additionalJOptions> <additionalJOptions>
<additionalJOption>-html5</additionalJOption> <additionalJOption>-html5</additionalJOption>
</additionalJOptions> </additionalJOptions>
<javadocExecutable>${java.home}/bin/javadoc</javadocExecutable>
</configuration> </configuration>
</plugin> </plugin>
<plugin> <plugin>
<artifactId>maven-assembly-plugin</artifactId> <artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version> <version>3.1.1</version>
<configuration> <configuration>
<descriptor>src/main/assembly/assembly.xml</descriptor> <descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration> </configuration>
<executions> <executions>
<execution> <execution>
...@@ -160,7 +169,6 @@ ...@@ -160,7 +169,6 @@
</execution> </execution>
</executions> </executions>
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
</project> </project>
\ No newline at end of file
package de.hdm_stuttgart.mi.sda1; package de.hdm_stuttgart.mi.sda1;
import de.hdm_stuttgart.mi.sda1.impl.DbRead; import de.hdm_stuttgart.mi.sda1.impl.DbRead;
public class AllResults { public class AllResults {
public static void main( String[] args ) { public static void main( String[] args ) {
final DbRead sports = new DbRead(); final DbRead sports = new DbRead();
sports.toHtml(System.out); sports.toHtml(System.out);
} }
} }
\ No newline at end of file \ No newline at end of file
package de.hdm_stuttgart.mi.sda1.impl; package de.hdm_stuttgart.mi.sda1.impl;
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 java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.SQLException; import java.sql.SQLException;
public class DbRead { public class DbRead {
public final Element html; public final Element html;
private final Connection conn; private final Connection conn;
public DbRead() { public DbRead() {
html = new Element("html"); html = new Element("html");
Connection connTmp = null; Connection connTmp = null;
try { try {
connTmp = DriverManager.getConnection( connTmp = DriverManager.getConnection(
"jdbc:mysql://localhost/hdm?serverTimezone=UTC", "hdmuser", "XYZ"); "jdbc:postgresql://localhost:5432/hdm", "hdmuser", "XYZ");
} catch (SQLException e) { } catch (final SQLException e) {
System.err.println("Unable to establish connection: " + e); System.err.println("Unable to establish connection: " + e);
System.exit(1); System.exit(1);
}
conn = connTmp;
} }
conn = connTmp;
}
public void toHtml(final OutputStream out) { public void toHtml(final OutputStream out) {
final XMLOutputter printer = final XMLOutputter printer =
new XMLOutputter(Format.getPrettyFormat()); new XMLOutputter(Format.getPrettyFormat());
try { try {
printer.output(html, out); printer.output(html, out);
} catch (final IOException e) { } catch (final IOException e) {
System.err.println("Unable to write output: " + e); System.err.println("Unable to write output: " + e);
}
} }
} }
\ No newline at end of file }
\ No newline at end of file
...@@ -20,19 +20,19 @@ INSERT INTO Athlete VALUES(4, 'John Hooker'); ...@@ -20,19 +20,19 @@ INSERT INTO Athlete VALUES(4, 'John Hooker');
INSERT INTO Athlete VALUES(5, 'Brad Duncan'); INSERT INTO Athlete VALUES(5, 'Brad Duncan');
/* 100 metre race */ /* 100 metre race */
INSERT INTO Competition VALUES(1, 1, 10.23, 0); /* Jesse Owens */ INSERT INTO Competition VALUES(1, 1, 10.23, FALSE); /* Jesse Owens */
INSERT INTO Competition VALUES(2, 2, 11.55, 0); /* Tim Culver */ INSERT INTO Competition VALUES(2, 2, 11.55, FALSE); /* Tim Culver */
INSERT INTO Competition VALUES(3, 3, 11.94, 0); /* Sid Gascoine */ INSERT INTO Competition VALUES(3, 3, 11.94, FALSE); /* Sid Gascoine */
INSERT INTO Competition VALUES(4, 4, 10.02, 0); /* John Hooker */ INSERT INTO Competition VALUES(4, 4, 10.02, FALSE); /* John Hooker */
INSERT INTO Competition VALUES(5, 5, 10.84, 0); /* Brad Duncan */ INSERT INTO Competition VALUES(5, 5, 10.84, FALSE); /* Brad Duncan */
INSERT INTO Competition VALUES(6, 2, 10.02, 0); /* Tim Culver */ INSERT INTO Competition VALUES(6, 2, 10.02, FALSE); /* Tim Culver */
INSERT INTO Competition VALUES(7, 3, 10.49, 0); /* Sid Gascoine */ INSERT INTO Competition VALUES(7, 3, 10.49, FALSE); /* Sid Gascoine */
INSERT INTO Competition VALUES(8, 2, 9.35, 1); /* Tim Culver */ INSERT INTO Competition VALUES(8, 2, 9.35, TRUE); /* Tim Culver */
SELECT fullName, min(time) as time SELECT fullName, min(time) as time
FROM Competition, Athlete FROM Competition, Athlete
WHERE Competition.athlete = Athlete.id WHERE Competition.athlete = Athlete.id
AND badStart =0 AND badStart = FALSE
GROUP BY (fullName) GROUP BY (fullName)
ORDER BY time, fullName; ORDER BY time, fullName;
\ No newline at end of file
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
<name>Solve</name> <name>Solve</name>
<url>http://www.mi.hdm-stuttgart.de/freedocs/topic/de.hdm_stuttgart.mi.lectures/sd1SectUsingMaven.html</url> <url>https://freedocs.mi.hdm-stuttgart.de/sd1_sect_mavenCli.html</url>
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
...@@ -56,9 +56,9 @@ ...@@ -56,9 +56,9 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>mysql</groupId> <groupId>postgresql</groupId>
<artifactId>mysql-connector-java</artifactId> <artifactId>postgresql</artifactId>
<version>6.0.6</version> <version>9.1-901-1.jdbc4</version>
</dependency> </dependency>
<dependency> <dependency>
...@@ -109,16 +109,15 @@ ...@@ -109,16 +109,15 @@
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version> <version>3.8.1</version>
<configuration> <configuration>
<source>1.9</source> <source>11</source>
<target>1.9</target> <target>11</target>
<compilerArgument>-proc:none</compilerArgument> <compilerArgument>-proc:none</compilerArgument>
</configuration> </configuration>
</plugin> </plugin>
...@@ -126,22 +125,32 @@ ...@@ -126,22 +125,32 @@
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId> <artifactId>maven-javadoc-plugin</artifactId>
<version>3.0.1</version> <version>3.1.0</version>
<configuration> <configuration>
<links> <javaApiLinks>
<link>https://docs.oracle.com/javase/10/docs/api</link> <property>
</links> <name>api_11</name>
<value>https://klausur.mi.hdm-stuttgart.de/doc/openjdk-11-doc/api/</value>
</property>
</javaApiLinks>
<stylesheetfile>localstyles.css</stylesheetfile>
<additionalJOptions> <additionalJOptions>
<additionalJOption>-html5</additionalJOption> <additionalJOption>-html5</additionalJOption>
</additionalJOptions> </additionalJOptions>
<javadocExecutable>${java.home}/bin/javadoc</javadocExecutable>
</configuration> </configuration>
</plugin> </plugin>
<plugin> <plugin>
<artifactId>maven-assembly-plugin</artifactId> <artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version> <version>3.1.1</version>
<configuration> <configuration>
<descriptor>src/main/assembly/assembly.xml</descriptor> <descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration> </configuration>
<executions> <executions>
<execution> <execution>
......
package de.hdm_stuttgart.mi.sda1; package de.hdm_stuttgart.mi.sda1;
import de.hdm_stuttgart.mi.sda1.impl.DbRead; import de.hdm_stuttgart.mi.sda1.impl.DbRead;
import de.hdm_stuttgart.mi.sda1.impl.DbReadDuplicate; import de.hdm_stuttgart.mi.sda1.impl.DbReadDuplicate;
public class AllResults { public class AllResults {
public static void main( String[] args ) { public static void main( String[] args ) {
final DbRead sports = new DbReadDuplicate(); final DbRead sports = new DbReadDuplicate();
sports.readDatabase(); sports.readDatabase();
sports.toHtml(System.out); sports.toHtml(System.out);
} }
} }
\ No newline at end of file \ No newline at end of file
package de.hdm_stuttgart.mi.sda1; package de.hdm_stuttgart.mi.sda1;
import de.hdm_stuttgart.mi.sda1.impl.DbRead; import de.hdm_stuttgart.mi.sda1.impl.DbRead;
import de.hdm_stuttgart.mi.sda1.impl.DbReadUnique; import de.hdm_stuttgart.mi.sda1.impl.DbReadUnique;
public class FinalResults { public class FinalResults {
public static void main( String[] args ) { public static void main( String[] args ) {
final DbRead sports = new DbReadUnique(); final DbRead sports = new DbReadUnique();
sports.readDatabase(); sports.readDatabase();
sports.toHtml(System.out); sports.toHtml(System.out);
} }
} }
\ No newline at end of file \ No newline at end of file
package de.hdm_stuttgart.mi.sda1.impl; package de.hdm_stuttgart.mi.sda1.impl;
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 java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
abstract public class DbRead { abstract public class DbRead {
final String sqlQuery; final String sqlQuery;
public final Element html; public final Element html;
private final Element table; private final Element table;
private final Connection conn; private final Connection conn;
public DbRead(final String sqlQuery) { public DbRead(final String sqlQuery) {
this.sqlQuery = sqlQuery; this.sqlQuery = sqlQuery;
html = new Element("html"); html = new Element("html");
append(append(html, "head"), "title", "Results"); append(append(html, "head"), "title", "Results");
table = append(append(html, "body"), "table"); table = append(append(html, "body"), "table");
final Element headerLine = append(table, "tr"); final Element headerLine = append(table, "tr");
append(headerLine, "th", "Rank"); append(headerLine, "th", "Rank");
append(headerLine, "th", "Name"); append(headerLine, "th", "Name");
append(headerLine, "th", "Time / seconds"); append(headerLine, "th", "Time / seconds");
Connection connTmp = null; Connection connTmp = null;
try { try {
connTmp = DriverManager.getConnection( connTmp = DriverManager.getConnection(
"jdbc:mysql://localhost/hdm?serverTimezone=UTC", "hdmuser", "XYZ"); "jdbc:postgresql://localhost:5432/hdm", "hdmuser", "XYZ");
} catch (SQLException e) { } catch (final SQLException e) {
System.err.println("Unable to establish connection: " + e); System.err.println("Unable to establish connection: " + e);
System.exit(1); System.exit(1);
}
conn = connTmp;
} }
conn = connTmp;
}
protected abstract void insertRecord(final ResultSet result, final Element row, final int rank) protected abstract void insertRecord(final ResultSet result, final Element row, final int rank)
throws SQLException; throws SQLException;
public void readDatabase() { public void readDatabase() {
try { try {
final ResultSet results = conn.createStatement().executeQuery(sqlQuery); final ResultSet results = conn.createStatement().executeQuery(sqlQuery);
int rank = 1; int rank = 1;
while(results.next()) { while(results.next()) {
insertRecord(results, append(table, "tr"), rank++); insertRecord(results, append(table, "tr"), rank++);
}
} catch (final SQLException e) {
System.err.println("Database access error: " + e);
System.exit(1);
} }
} catch (final SQLException e) {
System.err.println("Database access error: " + e);
System.exit(1);
} }
}
static protected Element append(final Element parent, final String tagname) { static protected Element append(final Element parent, final String tagname) {
final Element child = new Element(tagname); final Element child = new Element(tagname);
parent.addContent(child); parent.addContent(child);
return child; return child;
} }
static protected void append(final Element parent, final String tagname, final String text) { static protected void append(final Element parent, final String tagname, final String text) {
final Element child = new Element(tagname); final Element child = new Element(tagname);
child.addContent(text); child.addContent(text);
parent.addContent(child); parent.addContent(child);
} }
public void toHtml(final OutputStream out) { public void toHtml(final OutputStream out) {
final XMLOutputter printer = final XMLOutputter printer =
new XMLOutputter(Format.getPrettyFormat()); new XMLOutputter(Format.getPrettyFormat());
try { try {
printer.output(html, out); printer.output(html, out);
} catch (final IOException e) { } catch (final IOException e) {
System.err.println("Unable to write output: " + e); System.err.println("Unable to write output: " + e);
}
} }
} }
\ No newline at end of file }
\ No newline at end of file
...@@ -27,4 +27,4 @@ public class DbReadDuplicate extends DbRead { ...@@ -27,4 +27,4 @@ public class DbReadDuplicate extends DbRead {
row.setAttribute("style", "background: red;"); row.setAttribute("style", "background: red;");
} }
} }
} }
\ No newline at end of file
...@@ -10,20 +10,17 @@ public class DbReadUnique extends DbRead { ...@@ -10,20 +10,17 @@ public class DbReadUnique extends DbRead {
"SELECT fullName, min(time) as time\n" + "SELECT fullName, min(time) as time\n" +
"FROM Competition, Athlete\n" + "FROM Competition, Athlete\n" +
"WHERE Competition.athlete = Athlete.id\n" + "WHERE Competition.athlete = Athlete.id\n" +
" AND badStart = 0\n" + " AND badStart = FALSE\n" +
"GROUP BY (fullName)\n" + "GROUP BY (fullName)\n" +
"ORDER BY time, fullName"; "ORDER BY time, fullName";
public DbReadUnique() { public DbReadUnique() {
super(sqlQuery); super(sqlQuery);
} }
@Override @Override
protected void insertRecord(final ResultSet result, final Element row, final int rank) throws SQLException { protected void insertRecord(final ResultSet result, final Element row, final int rank) throws SQLException {
append(row, "td", "" + rank); append(row, "td", "" + rank);
append(row, "td", result.getString("fullName")); append(row, "td", result.getString("fullName"));
append(row, "td", result.getString("time"));