× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



Xu, the concepts of creating a data source are not difficult, but they may
be a bit too much for the mailing list.  Are you going to COMMON?  If so, my
lab is number 410228, "Putting Java in Your Wallet".  I'll be showing how
various Java concepts can be used in developing business applications.

To give you a brief summary, though, let's say you have a class called Item.
Item is represented on the database as a file with a single alpha key field.
For the Item class, you would create an abstract class called
ItemDataSource.  ItemDataSource is responsible for actual access to the
database.  We'll give it two methods: get an Item and update an Item (in a
real example, you would have more methods such as add and delete, but that's
enough for this example).

public abstract class ItemDataSource {
  public abstract Item getItem(String itemNumber);
  public abstract void updateItem(Item item);
}

You can now create two subclasses of this class.  You can have different
names or put them in different packages.  For simplicity, let's use class
names: ItemSQL and ItemAS400.  ItemSQL is the SQL datasource, and ItemAS400
is the AS/400-specific datasource.  You then write the two classes:

public class ItemSQL extends ItemDataSource
{
  public Item getItem(String itemNumber)
  {
     ... SQL statements to build Item ...
  }

  public void updateItem(Item item)
  {
     ... SQL statements to update Item ...
  }
}

public class ItemAS400 extends ItemDataSource
{
  public Item getItem(String itemNumber)
  {
     ... Use ProgramCall class to get item ...
  }

  public void updateItem(Item item)
  {
     ... Use ProgramCall class to update Item ...
  }
}

Okay, so how does this work?  Well, now you add a couple of methods to your
Item class:

  private static ItemDataSource datasource;

  public static void setDataSource(ItemDataSource newDataSource)
  {
    dataSource = newDataSource;
  }

  public static Item getItem(String itemNumber)
  {
    return dataSource.getItem(itemNumber);
  }

  public void update()
  {
    dataSource.update(this);
  }

That's all the code you need.  (Note that the getItem method is static,
because it returns a new instance of Item, but update is an instance method,
because it updates the current Item.)  All that's left is to do is to set
the data source to the appropriate subclass prior to doing any database
access.

Here's how the code might look:

// Item.setDataSource(new ItemSQL());
   Item.setDataSource(new ItemAS400());

   Item item = Item.getItem(itemNumber);
   item.setDescription("Tom's Desk");
   item.update();


Now, by simply changing which data source you set at the beginning of your
application, you can either use SQL access or AS/400-specific access.  This
is the best of all worlds.  I hope this helps give you a little idea of the
power of true OO business programming.  I hope you can come to my lab at
COMMON, where I will show the attendees how to build an application using
this technique (among others).

Joe


> -----Original Message-----
> From: Xu, Weining
>
> Could you refer to more info about the technique of encapsulate
> the database
> access in an object? or your class  details?  Thanks a lot.



As an Amazon Associate we earn from qualifying purchases.

This thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.