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

Minor enhancements

parent 485a1948
...@@ -254,7 +254,7 @@ ...@@ -254,7 +254,7 @@
your project using IntelliJ. Otherwise read the first exercise regarding your project using IntelliJ. Otherwise read the first exercise regarding
the skeleton project import.</para> the skeleton project import.</para>
<para>Configuring a <productname>Mysql</productname> type database <para>Configuring a <productname>Postgresql</productname> type database
connection in <productname>Intellij</productname> (»Views« --&gt; »Tool connection in <productname>Intellij</productname> (»Views« --&gt; »Tool
Windows« --&gt; »Database«) requires:</para> Windows« --&gt; »Database«) requires:</para>
...@@ -367,7 +367,7 @@ ...@@ -367,7 +367,7 @@
<td><code>10.23</code></td> <td><code>10.23</code></td>
<td><code>0</code></td> <td><code>false</code></td>
</tr> </tr>
<tr> <tr>
...@@ -377,7 +377,7 @@ ...@@ -377,7 +377,7 @@
<td><code>11.55</code></td> <td><code>11.55</code></td>
<td><code>0</code></td> <td><code>false</code></td>
</tr> </tr>
<tr> <tr>
...@@ -387,7 +387,7 @@ ...@@ -387,7 +387,7 @@
<td><code>11.94</code></td> <td><code>11.94</code></td>
<td><code>0</code></td> <td><code>false</code></td>
</tr> </tr>
<tr> <tr>
...@@ -397,7 +397,7 @@ ...@@ -397,7 +397,7 @@
<td><code>10.02</code></td> <td><code>10.02</code></td>
<td><code>0</code></td> <td><code>false</code></td>
</tr> </tr>
<tr> <tr>
...@@ -407,7 +407,7 @@ ...@@ -407,7 +407,7 @@
<td><code>10.84</code></td> <td><code>10.84</code></td>
<td><code>0</code></td> <td><code>false</code></td>
</tr> </tr>
<tr> <tr>
...@@ -417,7 +417,7 @@ ...@@ -417,7 +417,7 @@
<td><code>10.02</code></td> <td><code>10.02</code></td>
<td><code>0</code></td> <td><code>false</code></td>
</tr> </tr>
<tr> <tr>
...@@ -427,7 +427,7 @@ ...@@ -427,7 +427,7 @@
<td><code>10.49</code></td> <td><code>10.49</code></td>
<td><code>0</code></td> <td><code>false</code></td>
</tr> </tr>
<tr> <tr>
...@@ -437,7 +437,7 @@ ...@@ -437,7 +437,7 @@
<td><code>9.35</code></td> <td><code>9.35</code></td>
<td><code>1</code></td> <td><code>true</code></td>
</tr> </tr>
</informaltable></td> </informaltable></td>
</tr> </tr>
...@@ -469,11 +469,8 @@ ...@@ -469,11 +469,8 @@
<listitem> <listitem>
<para>All but the very last <code>Competition</code> sample records <para>All but the very last <code>Competition</code> sample records
have a <code>badStart</code> value of 0 indicating a proper start. A have a <code>badStart</code> value of false indicating a proper
<code>badStart</code> value different from 0 indicates a start start.</para>
failure. Note: <productname>Mysql</productname> allows for type
boolean being an alias for type <code>bit</code> representing 0 and
1.</para>
</listitem> </listitem>
</orderedlist> </orderedlist>
...@@ -505,10 +502,9 @@ ...@@ -505,10 +502,9 @@
being created.</para> being created.</para>
<para>Extend the underlying classes to create the subsequent HTML <para>Extend the underlying classes to create the subsequent HTML
output derived from your <productname>Mysql</productname> database's output derived from your database's content. Competition results
content. Competition results shall be ordered first by ascending shall be ordered first by ascending time and second by athlete's
time and second by athlete's name. Red background rows indicate bad name. Red background rows indicate bad start attempts:</para>
start attempts:</para>
<informaltable border="0"> <informaltable border="0">
<colgroup width="24%"/> <colgroup width="24%"/>
...@@ -540,9 +536,8 @@ ...@@ -540,9 +536,8 @@
<td>. .</td> <td>. .</td>
<td valign="top"><para>Pseudo outline HTML representation, red <td valign="top"><para>Pseudo outline HTML representation, red
background indicating bad starts not to be displayed for background indicating bad starts:</para><informaltable
technical reasons, using bold style border="1">
instead:</para><informaltable border="1">
<colgroup width="15%"/> <colgroup width="15%"/>
<colgroup width="41%"/> <colgroup width="41%"/>
...@@ -562,7 +557,7 @@ ...@@ -562,7 +557,7 @@
<td><emphasis role="red">Tim Culver</emphasis></td> <td><emphasis role="red">Tim Culver</emphasis></td>
<td><emphasis role="red">10.23</emphasis></td> <td><emphasis role="red">9.35</emphasis></td>
</tr> </tr>
<tr> <tr>
......
...@@ -29,10 +29,16 @@ INSERT INTO Competition VALUES(6, 2, 10.02, FALSE); /* Tim Culver */ ...@@ -29,10 +29,16 @@ INSERT INTO Competition VALUES(6, 2, 10.02, FALSE); /* Tim Culver */
INSERT INTO Competition VALUES(7, 3, 10.49, FALSE); /* Sid Gascoine */ INSERT INTO Competition VALUES(7, 3, 10.49, FALSE); /* Sid Gascoine */
INSERT INTO Competition VALUES(8, 2, 9.35, TRUE); /* Tim Culver */ INSERT INTO Competition VALUES(8, 2, 9.35, TRUE); /* Tim Culver */
/* All runs including bad starts */
SELECT fullName, time, badStart
FROM Competition, Athlete
WHERE Competition.athlete = Athlete.id
ORDER BY time
SELECT fullName, min(time) as time /* Just the best valid run of each athlete */
SELECT fullName, min(time) AS time
FROM Competition, Athlete FROM Competition, Athlete
WHERE Competition.athlete = Athlete.id WHERE Competition.athlete = Athlete.id
AND badStart = FALSE AND NOT(badStart)
GROUP BY (fullName) GROUP BY Athlete.id
ORDER BY time, fullName; ORDER BY time, fullName;
\ No newline at end of file
...@@ -4,12 +4,13 @@ package de.hdm_stuttgart.mi.sda1; ...@@ -4,12 +4,13 @@ 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;
import java.sql.SQLException;
public class AllResults { public class AllResults {
public static void main( String[] args ) { public static void main( String[] args ) throws SQLException {
final DbRead sports = new DbReadDuplicate(); final DbRead sports = new DbReadDuplicate();
sports.readDatabase();
sports.toHtml(System.out); sports.toHtml(System.out);
} }
......
...@@ -4,12 +4,13 @@ package de.hdm_stuttgart.mi.sda1; ...@@ -4,12 +4,13 @@ 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;
import java.sql.SQLException;
public class FinalResults { public class FinalResults {
public static void main( String[] args ) { public static void main( String[] args ) throws SQLException {
final DbRead sports = new DbReadUnique(); final DbRead sports = new DbReadUnique();
sports.readDatabase();
sports.toHtml(System.out); sports.toHtml(System.out);
} }
......
...@@ -6,68 +6,29 @@ import org.jdom2.output.XMLOutputter; ...@@ -6,68 +6,29 @@ 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.*;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
abstract public class DbRead { abstract public class DbRead {
final String sqlQuery; abstract void addData()throws SQLException;
public final Element html; protected Element html, table;
private final Element table;
private final Connection conn; protected Connection conn;
public DbRead(final String sqlQuery) { public DbRead() {
this.sqlQuery = sqlQuery;
html = new Element("html"); html = new Element("html");
append(append(html, "head"), "title", "Results");
table = append(append(html, "body"), "table");
final Element headerLine = append(table, "tr");
append(headerLine, "th", "Rank");
append(headerLine, "th", "Name");
append(headerLine, "th", "Time / seconds");
Connection connTmp = null; Connection connTmp = null;
try { try {
connTmp = DriverManager.getConnection( conn = DriverManager.getConnection(
"jdbc:postgresql://localhost:5432/hdm", "hdmuser", "XYZ"); "jdbc:postgresql://localhost:5432/hdm", "hdmuser", "XYZ");
table = addHeader();
addData();
} catch (final 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;
}
protected abstract void insertRecord(final ResultSet result, final Element row, final int rank)
throws SQLException;
public void readDatabase() {
try {
final ResultSet results = conn.createStatement().executeQuery(sqlQuery);
int rank = 1;
while(results.next()) {
insertRecord(results, append(table, "tr"), rank++);
}
} catch (final SQLException e) {
System.err.println("Database access error: " + e);
System.exit(1);
}
}
static protected Element append(final Element parent, final String tagname) {
final Element child = new Element(tagname);
parent.addContent(child);
return child;
}
static protected void append(final Element parent, final String tagname, final String text) {
final Element child = new Element(tagname);
child.addContent(text);
parent.addContent(child);
} }
public void toHtml(final OutputStream out) { public void toHtml(final OutputStream out) {
...@@ -80,4 +41,43 @@ abstract public class DbRead { ...@@ -80,4 +41,43 @@ abstract public class DbRead {
System.err.println("Unable to write output: " + e); System.err.println("Unable to write output: " + e);
} }
} }
}
\ No newline at end of file private Element addHeader() throws SQLException{
html.addContent(new Element("head").addContent(new Element("title").addContent("Results")));
final Element table =
new Element(
"table")
.addContent(new Element(
"tr").addContent(new Element("th").addContent("Rank"))
.addContent(new Element("th").addContent("Name"))
.addContent(new Element("th").addContent("Time")));
html.addContent(table);
return table;
}
}
/*
*
<body>
<table>
<tr style="background: red;">
<td>1</td>
<td>Tim Culver</td>
<td>9.35</td>
</tr>
...
</table>
</body>
*
*
*
*
* */
\ 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.XMLOutputter;
import java.sql.ResultSet; import java.io.IOException;
import java.sql.SQLException; import java.io.OutputStream;
import java.sql.*;
public class DbReadDuplicate extends DbRead { public class DbReadDuplicate extends DbRead {
static private final String sqlQuery = void addData() throws SQLException {
"SELECT fullName, time, badStart\n" +
"FROM Competition, Athlete\n" +
"WHERE Competition.athlete = Athlete.id\n" +
"ORDER BY time, fullName";
public DbReadDuplicate() { final Statement queryTimes = conn.createStatement();
super(sqlQuery);
} final String selectTimes =
"SELECT fullName, time, badStart\n" +
"FROM Competition, Athlete\n" +
"WHERE Competition.athlete = Athlete.id\n" +
"ORDER BY time";
final ResultSet times = queryTimes.executeQuery(selectTimes);
int rank = 1;
while (times.next()) {
@Override final Element tr = new Element(
protected void insertRecord(final ResultSet result, final Element row, final int rank) throws SQLException { "tr")
append(row, "td", "" + rank); .addContent(new Element("td").addContent("" + rank++))
append(row, "td", result.getString("fullName")); .addContent(new Element("td").addContent(times.getString("fullname")))
append(row, "td", result.getString("time")); .addContent(new Element("td").addContent(times.getString("time")));
if (result.getBoolean("badStart")) { if (times.getBoolean("badStart")) {
row.setAttribute("style", "background: red;"); tr.setAttribute("style", "background: red;");
}
table.addContent(tr);
} }
} }
} }
\ No newline at end of file
...@@ -4,23 +4,36 @@ import org.jdom2.Element; ...@@ -4,23 +4,36 @@ import org.jdom2.Element;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement;
public class DbReadUnique extends DbRead { public class DbReadUnique extends DbRead {
static private final String sqlQuery =
"SELECT fullName, min(time) as time\n" +
"FROM Competition, Athlete\n" + void addData() throws SQLException {
"WHERE Competition.athlete = Athlete.id\n" +
" AND badStart = FALSE\n" +
"GROUP BY (fullName)\n" + final Statement queryTimes = conn.createStatement();
"ORDER BY time, fullName";
final String selectTimes =
public DbReadUnique() { "SELECT fullName, min(time) AS time\n" +
super(sqlQuery); "FROM Competition, Athlete\n" +
} "WHERE Competition.athlete = Athlete.id\n" +
@Override " AND NOT(badStart)\n" +
protected void insertRecord(final ResultSet result, final Element row, final int rank) throws SQLException { "GROUP BY Athlete.id\n" +
append(row, "td", "" + rank); "ORDER BY time, fullName";
append(row, "td", result.getString("fullName"));
append(row, "td", result.getString("time")); final ResultSet times = queryTimes.executeQuery(selectTimes);
int rank = 1;
while (times.next()) {
final Element tr = new Element(
"tr")
.addContent(new Element("td").addContent("" + rank++))
.addContent(new Element("td").addContent(times.getString("fullname")))
.addContent(new Element("td").addContent(times.getString("time")));
table.addContent(tr);
}
} }
} }
\ No newline at end of file
...@@ -175,6 +175,12 @@ ...@@ -175,6 +175,12 @@
</strong> </strong>
</xsl:template> </xsl:template>
<xsl:template match="db:emphasis[@role='red']">
<strong style="color:red">
<xsl:apply-templates/>
</strong>
</xsl:template>
<xsl:template match="db:quote"> <xsl:template match="db:quote">
<xsl:text>»</xsl:text> <xsl:text>»</xsl:text>
<xsl:apply-templates/> <xsl:apply-templates/>
......
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