Do I have a database?
Yes, I have. You can create your own by following
http://www.cs.indiana.edu/classes/a348/fall2009/1201.txt
-bash-3.2$ cd /nobackup/
-bash-3.2$ cd dgerman/
-bash-3.2$ cd mysql-5.0.22/
-bash-3.2$ ./connect_as_lbird
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.0.22-log
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> use tournament;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+----------------------+
| Tables_in_tournament |
+----------------------+
| enrollment |
| matches |
| participation |
| players |
| spectators |
| venues |
+----------------------+
6 rows in set (0.00 sec)
mysql> select username, sum(points)
from participation inner join matches on
matches.matchid = participation.matchid and
round in ('round one', 'round two', 'round three', 'round four')
group by username order by sum(points) desc;
+----------+-------------+
| username | sum(points) |
+----------+-------------+
| lbird | 4 |
| mjordan | 2.5 |
| rmiller | 1.5 |
| tduncan | 1.5 |
| tkukoc | 1.5 |
| cbarkley | 1 |
+----------+-------------+
6 rows in set (0.28 sec)
mysql> exit;
Bye
-bash-3.2$
So the query is:
select username, sum(points)
from participation inner join matches on
matches.matchid = participation.matchid and
round in ('round one', 'round two', 'round three', 'round four')
group by username order by sum(points) desc;
How can I make this available on the web?
1. In Perl:
http://www.cs.indiana.edu/classes/a290-web/fall2008/whatsnew/project/extract.txt
http://silo.cs.indiana.edu:8346/cgi-bin/query001
#!/usr/bin/perl
use DBI;
use CGI;
$q = new CGI;
print "Content-type: text/html\n\n";
$DB = "dbi:mysql:tournament:silo.cs.indiana.edu:port=8974";
$username = "lbird";
$password = "dribl";
$dbh = DBI->connect($DB,
$username,
$password, {PrintError => 0}) ||
die "Couldn't open database: ", $DBI::errstr;
$query = "select username, sum(points) from participation inner join matches on matches.matchid = participation.matchid and round in ('round one', 'round two', 'round three', 'round four') group by username order by sum(points) desc;";
$sth = $dbh->prepare($query) || die $dbh->errstr;
$sth->execute() || die $sth->errstr;
print qq{
|
| Player
| Points
};
$i = 0;
while (my $row = $sth->fetch) {
my(@values) = @$row;
$i += 1;
print " |
|---|
| $i ";
foreach $value (@values) {
print " | $value";
}
print "\n";
}
print " |
";
$sth->finish;
2. PHP:
http://silo.cs.indiana.edu:8346/query002.php
http://silo.cs.indiana.edu:8346/query002.phps
3. JSP:
a) We need a driver
b) We are in a Tomcat context
-bash-3.2$ cd $CATALINA_HOME
-bash-3.2$ cd webapps/
-bash-3.2$ ls
balancer jsp-examples rctmpx servlets-examples watermelon whatever
chat raupenfahrzeug ROOT tomcat-docs webdav
-bash-3.2$ cd raupenfahrzeug/
-bash-3.2$ pwd
/u/dgerman/apache-tomcat-5.5.17/webapps/raupenfahrzeug
-bash-3.2$
Getting the driver:
-bash-3.2$ pwd
/u/dgerman/apache-tomcat-5.5.17/webapps/raupenfahrzeug
-bash-3.2$ cd WEB-INF/
-bash-3.2$ cd lib
-bash-3.2$ ls
+-------------------------------------------------------------------------------+
| -bash-3.2$ cp /l/www/classes/a348/sum2006/software/mm.mysql-2.0.2-bin.jar . |
+-------------------------------------------------------------------------------+
-bash-3.2$ pwd
/u/dgerman/apache-tomcat-5.5.17/webapps/raupenfahrzeug/WEB-INF/lib
-bash-3.2$ ls -l
total 80
-rw-r--r-- 1 dgerman faculty 71328 Dec 3 17:30 mm.mysql-2.0.2-bin.jar
-bash-3.2$
c) Write the code:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class One extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws IOException,
ServletException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
try {
Class.forName("org.gjt.mm.mysql.Driver");
} catch (Exception e) {
out.println("Can't load JDBC Driver. " + "Make sure classpath is correct.");
System.exit(0);
}
String url = "jdbc:mysql://silo.cs.indiana.edu:13038/teddy",
username = "mrbean",
password = "b3an";
Connection connection;
try {
connection = DriverManager.getConnection(url, username, password);
Statement statement = connection.createStatement();
ResultSet result;
out.println("Querying database...");
result = statement.executeQuery(
" select name, age from person " +
" where lives_in = '" + request.getParameter("where") + "' "
);
while (result.next()) {
out.println(" " + result.getString(1) + ", " + result.getString(2));
}
statement.close();
connection.close();
} catch (SQLException e) {
out.println("An SQLException occurred: " + e.getMessage());
} catch (Exception e) {
out.println("Exception: " + e);
}
}
}
This is the JSP, to be finished:
<%@ page import ="java.sql.*" %>
<%
try {
Class.forName("org.gjt.mm.mysql.Driver");
} catch (Exception e) {
out.println("Can't load JDBC Driver. " + "Make sure classpath is correct.");
System.exit(0);
}
String url = "jdbc:mysql://silo.cs.indiana.edu:8974/tournament",
username = "lbird",
password = "dribl";
Connection connection;
try {
connection = DriverManager.getConnection(url, username, password);
Statement statement = connection.createStatement();
ResultSet result;
out.println("Querying database...");
result = statement.executeQuery(
"select username, sum(points) from participation inner join matches on matches.matchid = participation.matchid and round in ('round one', 'round two', 'round three', 'round four') group by username order by sum(points) desc;"
);
while (result.next()) {
out.println(" " + result.getString(1) + ", " + result.getString(2));
}
statement.close();
connection.close();
} catch (SQLException e) {
out.println("An SQLException occurred: " + e.getMessage());
} catch (Exception e) {
out.println("Exception: " + e);
}
%>
This needs to be debugged, or at least tested.
So today (Dec 4, 2009) I check Tomcat isn't running I start, access the JSP, it works.
I change the JSP as follows, only improving the format a bit:
-bash-3.2$ pwd
/u/dgerman/apache-tomcat-5.5.17/webapps/raupenfahrzeug
-bash-3.2$ cat one.jsp
<%@ page import ="java.sql.*" %>
<%
try {
Class.forName("org.gjt.mm.mysql.Driver");
} catch (Exception e) {
out.println("Can't load JDBC Driver. " + "Make sure classpath is correct.");
System.exit(0);
}
String url = "jdbc:mysql://silo.cs.indiana.edu:8974/tournament",
username = "lbird",
password = "dribl";
Connection connection;
try {
connection = DriverManager.getConnection(url, username, password);
Statement statement = connection.createStatement();
ResultSet result;
out.println("Querying database...");
result = statement.executeQuery(
"select username, sum(points) from participation inner join matches on matches.matchid = participation.matchid and round in ('round one', 'round two', 'round three', 'round four') group by username order by sum(points) desc;"
);
%>
| Player Name | No. of Points
<%
while (result.next()) {
out.println(" |
| " + result.getString(1) + " | " + result.getString(2));
}
%>
|
<%
statement.close();
connection.close();
} catch (SQLException e) {
out.println("An SQLException occurred: " + e.getMessage());
} catch (Exception e) {
out.println("Exception: " + e);
}
%>
http://silo.cs.indiana.edu:8972/raupenfahrzeug/one.jsp
(If my Tomcat is up, that is).