The moose likes Servlets and the fly likes opening database in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "opening database in servlet" Watch "opening database in servlet" New topic

opening database in servlet

alan dool

Joined: Nov 04, 2001
Posts: 1
Hi all,
I'm sort of new to this, so bear with me ...
I've got a servlet which I want to open a DB2 database and read in the records. The DB2 database resides on an AS400 mid-range computer.
Right now the servlet is just reading in the data from another class (MyCatalog)where the data was hard-coded in, shown below:
public abstract class MyCatalogPage extends HttpServlet {
private MyItem[] items;
private String[] itemIDs;
private String title;
/** Given an array of item IDs, look them up in the
* Catalog and put their corresponding Item entry
* into the items array. The Item contains a short
* description, a long description, and a price,
* using the item ID as the unique key.
* <P>
* Servlets that extend CatalogPage <B>must</B> call
* this method (usually from init) before the servlet
* is accessed.

protected void setItems(String[] itemIDs) {
this.itemIDs = itemIDs;
items = new MyItem[itemIDs.length];
for(int i=0; i<items.length; i++) {
>>>>>>> items[i] = MyCatalog.getItem(itemIDs[i]); <<<<<<<<
My question is: 1) How do I read these in from the DB2 database??
(I'm a little famaliar with ODBC and JDBC)
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13018
Architecturally speaking, you need to remove all those instance variables from the servlet since a single servlet instance can be shared by multiple requests. I suggest you create another class to handle the JDBC query - it will be easier to test off-line.
I agree. Here's the link:
subject: opening database in servlet
It's not a secret anymore!