Applet
Example 6
This
example demonstrates how to implement the applet so that
the graph data is acquired from a database. Here we set
up the applet to acquire the configuration data from a text
file and the graph data from a server side process which
in turn acquires the data from a database.
and
here is the HTML code,
<applet
code="LineGraphApplet.class" archive="LineGraph.jar"
width="500" height="420">
<!--
Start Up Parameters -->
<PARAM name="LOADINGMESSAGE" value="Line
Graph Loading - Please Wait.">
<PARAM name="STEXTCOLOR" value="0,0,100">
<!-- Message Text Color-->
<PARAM name="STARTUPCOLOR" value="255,255,255">
<!-- Applet Background color -->
<!--
Data files -->
<PARAM name="chartproperties" value="lineprops.txt">
<PARAM name="chartdata" value="http://www.jpowered.com/siriusjava/servlet/DatabaseServlet2">
</applet>
The
applet will pick the configuration parameters from a text
file lineprops2.txt and the graph data from our servlet
DatabaseServlet ( click
here to view java code ).
The
DatabaseServlet is designed to retrieve product sales data
from a MySQL database, from a table with the following structure,
| Table
Name : ProductSales |
|
|
| ProductName |
Character |
| Quarter1sales |
Integer |
| Quarter2sales |
Integer |
| Quarter2sales |
Integer |
| Quarter2sales |
Integer |
| Year |
Integer |
Our
servlet will pick out the sales figures for the year 2001
for three products ( ProductX, ProductY and ProductZ) and
return the data in the correct format for the line graph.
The
method is as follows,
-
Set the output characteristics for the return data
-
Establish the database connection
-
Build the query statement and retrieve the database records
-
Process the database records and return the Data
-
All finished so close the database connection
The
full servlet code is contained in the DatabaseServlet.java
file.
(Click
here to view the code).
As you
will see from the code the servlet connects to the database
via JDBC and as such it is essential to have the correct
JDBC drivers for your database. ( For further information
on JDBC drivers please see http://industry.java.sun.com/products/JDBC/drivers
)
The
SQL statement used to retrieve the data from the database
is simply,
SELECT * FROM ProductSales WHERE Year='2001'
The
returned result set from this statement is then passed to
a little routine, GraphData, which reads through the records
picking out the relevant records for the three products.
Also note in this routine how the "rsltStr" is
constructed to provide the data in the correct format for
the lineGraph.
Finally
the rsltStr is returned back to the calling process ( in
this case our Line Graph ) and the database connection is
closed.
For
a full explanation of and range of values for the above
parameters please see the Documentation.
Note:
If you are using the evaluation version then in the applet
a pop-up window will appear upon the startup and an evaluation
message will be incorporated by the servlet. Both these
features have been removed from the licensed version.
Licensing information can be found at