The applet and servlet can be set to acquire data from a database ( or multiple
databases ) by utilizing a server side script or process.
Such a server side process can be written any language including PHP,JSP,ASP, Java Servlet, Perl etc. Here
we will demonstrate how to create a Java Servlet which acquires data from a single database and present the data in the correct
format to either the graph applet or graph servlet.
The DatabaseServlet3.java
is designed to retrieve product sales data from a MySQL database, from a table with the following structure,
Table Name : ProductSales |
|
|
ProductName |
Character |
Year |
Integer |
Month |
Integer |
Value |
double |
Our servlet will pick out the sales figures for three products ( ProductX,
ProductY and ProductZ) and return the data in the correct format for the 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 DatabaseServlet3.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
)
|