Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / December 2005

Tip: Looking for answers? Try searching our database.

Newbie help! Returning an enumeration

Thread view: 
Andy Bailey - 28 Dec 2005 20:24 GMT
Hello all, i wonder if some kind soul could point me in the right direction.
I'm sure the solution is VERY straightforward but for some reason i just
can't see it.. just call me Mr. Thicky. Anyway... here it is....

I have a main method that passes a parameter to another method used to
perform an SQL query. What i want to do is to return the result (ResultSet)
as an enumeration for the main method to display. Here's the partial code...

// The main method passes 'Meat' to the getCategoryOfStk
// method
public static void main(String[] args){
try{..........
.......
StkCollection stkColl= new StkCollection
System.out.println("Testing getCategoryOfStk");
Enumeration enum = stkColl.getCategoryOfStk("Meat");
while(enum.hasMoreElements()){
System.out .println((CD)enum.nextElement());
}............

// The constructor of the StkCollection class creates the link to the
database. The getCategoryOfStk method called is shown below:

public Enumeration getCategoryOfStk(String category)throws SQLException{
String reqdCategory=category;
Enumeration e; // used to return details?
try
{
Statement state = cn.createStatement();
//perform DB query....
String getCategoryQuery =
"Select * from Stock where Category='"+reqdCategory+"'";

ResultSet rs = state.executeQuery(getCategoryQuery);
// OK all is fine to this point we have the results in ResultSet rs. My
problem is how to return the results as an enumeration type back to the main
method for display. Do i need to Cast the ResultSet rs to an Enumeration
type? Or am i totally barking up the wrong tree and i need to implement the
Enumeration interface?

}catch(Exception ex)
{System.out.println("Problem executing query"+ex);}

return e; // somehow return the enumeration
}

Any help would be gratefully received and will stop me banging my head on
the desk.

Kind Regards,
Andy
Roedy Green - 28 Dec 2005 20:34 GMT
On Wed, 28 Dec 2005 20:24:05 GMT, "Andy Bailey"
<andy.bailey1@ntlworld.com> wrote, quoted or indirectly quoted someone
who said :

> Do i need to Cast the ResultSet rs to an Enumeration
>type? Or am i totally barking up the wrong tree and i need to implement the
>Enumeration interface?

You need to return an object that implements the Enumeration (or
Iterator to be modern) Interface.

This class his burned into it the information needed to get at each
row, i.e. you pass it the resultset to its constructor.  It can be an
anonymous class. Have a look at source code in src.zip that returns
Enumerations or Iterators to see how they do it.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.