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

Minor enhancements

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