Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

opening database in servlet

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
Thanks....
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Time flies like an arrow. Fruit flies like a banana. Steve flies like a tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic