CISC370-011 --
demo code from class #9This page provides links to some short snippets of code from class #9 (Nov 4).
Elist.java uses JDBC to build a list of employees and their titles, then formats the result as an HTML file.
PrepStat.java demonstrates a prepared statement.
DbMeta.java uses database metadata to list all tables in a database.
ExecSQL.java is a very simple
interactive query tool. It reads its database connection from
a property file (using the Java Properties API!)
named ExecSQL.properties,
then reads SQL commands from standard input and writes their
results to standard output. Each command must be terminated
by a line containing only the word "GO". To end
the program, enter a line containing only the word "quit".
Here is a sample session of the program:
D:\CIS370\Web\demo\class09>type ExecSQL.properties
jdbc.drivers=org.hsqldb.jdbcDriver
jdbc.url=jdbc:hsqldb:hsql://localhost:9001
jdbc.username=sa
jdbc.password=
D:\CIS370\Web\demo\class09>java ExecSQL
Enter command (or 'quit' to exit):
select *
from turkey
go
Query is:
select *
from turkey
Output is:
NAME, VALUE
walt, hunk
sharon, babe
paul, bozo
dan, brainiac
fred, flintstone
Enter command (or 'quit' to exit):
create table dingleberry (
species VARCHAR(32) NOT NULL,
howMany INT DEFAULT 0
)
go
Query is:
create table dingleberry (
species VARCHAR(32) NOT NULL,
howMany INT DEFAULT 0
)
No output.
Enter command (or 'quit' to exit):
insert into dingleberry(species,howMany) values('Chickadee',17)
insert into dingleberry(species,howMany) values('Titmouse',12)
insert into dingleberry(species,howMany) values('Cardinal',20)
insert into dingleberry(species,howMany) values('Goldfinch',5)
go
Query is:
insert into dingleberry(species,howMany) values('Chickadee',17)
insert into dingleberry(species,howMany) values('Titmouse',12)
insert into dingleberry(species,howMany) values('Cardinal',20)
insert into dingleberry(species,howMany) values('Goldfinch',5)
No output.
Enter command (or 'quit' to exit):
select sum(howMany)
from dingleberry
go
Query is:
select sum(howMany)
from dingleberry
Output is:
HOWMANY
54
Enter command (or 'quit' to exit):
quit
D:\CIS370\Web\demo\class09>
EmployeeList.java is a simple servlet that returns an employee list formatted as an HTML document.
ImageTest.java illustrates how a servlet (or a CGI script) can return a document of type other than HTML (in this case, a PNG image). Note the use of the excellent JFreeChart plotting library. A screen dump of this servlet's output is: