KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > juddi > datastore > jdbc > TestContactTable


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

16 package org.apache.juddi.datastore.jdbc;
17
18 import java.sql.Connection JavaDoc;
19 import java.util.Vector JavaDoc;
20
21 import org.apache.juddi.datatype.business.BusinessEntity;
22 import org.apache.juddi.datatype.business.Contact;
23 import org.apache.juddi.util.Config;
24 import org.apache.juddi.util.jdbc.Transaction;
25 import org.apache.juddi.uuidgen.UUIDGen;
26 import org.apache.juddi.uuidgen.UUIDGenFactory;
27
28 /**
29  * @author Steve Viens (sviens@apache.org)
30  */

31 class TestContactTable
32 {
33   public static void main(String JavaDoc[] args)
34     throws Exception JavaDoc
35   {
36     // make sure we're using a DBCP DataSource and
37
// not trying to use JNDI to aquire one.
38
Config.setStringProperty("juddi.useConnectionPool","true");
39
40     Connection JavaDoc conn = null;
41     try {
42       conn = Database.aquireConnection();
43       test(conn);
44     }
45     finally {
46       if (conn != null)
47         conn.close();
48     }
49   }
50
51   public static void test(Connection JavaDoc connection) throws Exception JavaDoc
52   {
53     Transaction txn = new Transaction();
54     UUIDGen uuidgen = UUIDGenFactory.getUUIDGen();
55
56     if (connection != null)
57     {
58       try
59       {
60         String JavaDoc businessKey = uuidgen.uuidgen();
61         BusinessEntity business = new BusinessEntity();
62         business.setBusinessKey(businessKey);
63         business.setAuthorizedName("sviens");
64         business.setOperator("www.jUDDI.org");
65
66         Vector JavaDoc contactList = new Vector JavaDoc();
67         Contact contact = null;
68
69         contact = new Contact("Steve Viens");
70         contact.setUseType("sales");
71         contactList.add(contact);
72
73         contact = new Contact("Marley Viens");
74         contact.setUseType("support");
75         contactList.add(contact);
76
77         contact = new Contact("Chris Michaels");
78         contact.setUseType("marketing");
79         contactList.add(contact);
80
81         String JavaDoc authorizedUserID = "sviens";
82
83         // begin a new transaction
84
txn.begin(connection);
85
86         // insert a new BusinessEntity
87
BusinessEntityTable.insert(business, authorizedUserID, connection);
88
89         // insert a Collection of Contact objects
90
ContactTable.insert(businessKey, contactList, connection);
91
92         // select a Collection Contact objects by BusinessKey
93
contactList = ContactTable.select(businessKey, connection);
94
95         System.out.println(contactList.size() +
96           " contacts for BusinessKey: "+businessKey);
97
98         // delete a Collection Contact objects by BusinessKey
99
ContactTable.delete(businessKey, connection);
100
101         // re-select a Collection Contact objects by BusinessKey
102
contactList = ContactTable.select(businessKey, connection);
103
104         System.out.println(contactList.size() +
105           " contacts for BusinessKey: "+businessKey);
106
107         // commit the transaction
108
txn.commit();
109       }
110       catch (Exception JavaDoc ex)
111       {
112         try
113         {
114           txn.rollback();
115         }
116         catch (java.sql.SQLException JavaDoc sqlex)
117         {
118           sqlex.printStackTrace();
119         }
120         throw ex;
121       }
122     }
123   }
124 }
125
Popular Tags