KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > tutorial2 > UCListAllProducts


1 package org.apache.ojb.tutorial2;
2
3 /* Copyright 2002-2004 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20
21 import org.odmg.Implementation;
22 import org.odmg.OQLQuery;
23 import org.odmg.Transaction;
24
25 /**
26  * Use case for listing all products in the database.
27  */

28 public class UCListAllProducts extends AbstractUseCase
29 {
30     /**
31      * Creates a new list-all use case instance.
32      *
33      * @param odmg The odmg implementation to use
34      */

35     public UCListAllProducts(Implementation odmg)
36     {
37         super(odmg);
38     }
39
40     /**
41      * Returns a description of this use case.
42      *
43      * @return A description of the use case
44      */

45     public String JavaDoc getDescription()
46     {
47         return "List all product entries";
48     }
49
50     /**
51      * Performs this use case.
52      */

53     public void apply()
54     {
55         System.out.println("The list of available products:");
56
57         try
58         {
59             // 1. open a transaction
60
Transaction tx = odmg.newTransaction();
61
62             tx.begin();
63
64             // 2. get an OQLQuery object from the ODMG facade
65
OQLQuery query = odmg.newOQLQuery();
66
67             // 3. set the OQL select statement
68
query.create("select allproducts from " + Product.class.getName());
69
70             // 4. perform the query and store the result in a persistent Collection
71
List JavaDoc allProducts = (List JavaDoc) query.execute();
72
73             tx.commit();
74
75             // 5. now iterate over the result to print each product
76
for (Iterator JavaDoc iter = allProducts.iterator(); iter.hasNext();)
77             {
78                 System.out.println(iter.next());
79             }
80         }
81         catch (Throwable JavaDoc t)
82         {
83             t.printStackTrace();
84         }
85     }
86 }
87
Popular Tags