KickJava   Java API By Example, From Geeks To Geeks.

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


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.sql.PreparedStatement JavaDoc;
20 import java.sql.ResultSet JavaDoc;
21 import java.util.Vector JavaDoc;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.juddi.datatype.KeyedReference;
26
27 /**
28  * @author Steve Viens (sviens@apache.org)
29  */

30 class BindingCategoryTable
31 {
32   // private reference to the jUDDI logger
33
private static Log log = LogFactory.getLog(BindingCategoryTable.class);
34
35   static String JavaDoc insertSQL = null;
36   static String JavaDoc selectSQL = null;
37   static String JavaDoc deleteSQL = null;
38
39   static {
40     // buffer used to build SQL statements
41
StringBuffer JavaDoc sql = null;
42
43     // build insertSQL
44
sql = new StringBuffer JavaDoc(150);
45     sql.append("INSERT INTO BINDING_CATEGORY (");
46     sql.append("BINDING_KEY,");
47     sql.append("CATEGORY_ID,");
48     sql.append("TMODEL_KEY_REF,");
49     sql.append("KEY_NAME,");
50     sql.append("KEY_VALUE) ");
51     sql.append("VALUES (?,?,?,?,?)");
52     insertSQL = sql.toString();
53
54     // build selectSQL
55
sql = new StringBuffer JavaDoc(200);
56     sql.append("SELECT ");
57     sql.append("TMODEL_KEY_REF,");
58     sql.append("KEY_NAME,");
59     sql.append("KEY_VALUE, ");
60     sql.append("CATEGORY_ID ");
61     sql.append("FROM BINDING_CATEGORY ");
62     sql.append("WHERE BINDING_KEY=? ");
63     sql.append("ORDER BY CATEGORY_ID");
64     selectSQL = sql.toString();
65
66     // build deleteSQL
67
sql = new StringBuffer JavaDoc(100);
68     sql.append("DELETE FROM BINDING_CATEGORY ");
69     sql.append("WHERE BINDING_KEY=?");
70     deleteSQL = sql.toString();
71   }
72
73   /**
74    * Insert new row into the BINDING_CATEGORY table.
75    *
76    * @param bindingKey String to the parent BindingTemplate object.
77    * @param keyRefs A Vector of KeyedReference instances to insert.
78    * @param connection JDBC connection
79    * @throws java.sql.SQLException
80    */

81   public static void insert(
82     String JavaDoc bindingKey,
83     Vector JavaDoc keyRefs,
84     Connection JavaDoc connection)
85     throws java.sql.SQLException JavaDoc
86   {
87     PreparedStatement JavaDoc statement = null;
88
89     try
90     {
91       statement = connection.prepareStatement(insertSQL);
92       statement.setString(1, bindingKey.toString());
93
94       int listSize = keyRefs.size();
95       for (int categoryID = 0; categoryID < listSize; categoryID++)
96       {
97         KeyedReference keyRef = (KeyedReference) keyRefs.elementAt(categoryID);
98
99         // extract values to insert
100
String JavaDoc tModelKeyValue = null;
101         if (keyRef.getTModelKey() != null)
102           tModelKeyValue = keyRef.getTModelKey().toString();
103
104         // set the values
105
statement.setInt(2, categoryID);
106         statement.setString(3, tModelKeyValue);
107         statement.setString(4, keyRef.getKeyName());
108         statement.setString(5, keyRef.getKeyValue());
109
110         log.debug(
111           "insert into BINDING_CATEGORY table:\n\n\t"
112             + insertSQL
113             + "\n\t BINDING_KEY="
114             + bindingKey.toString()
115             + "\n\t CATEGORY_ID="
116             + categoryID
117             + "\n\t TMODEL_KEY_REF="
118             + tModelKeyValue
119             + "\n\t KEY_NAME="
120             + keyRef.getKeyName()
121             + "\n\t KEY_VALUE="
122             + keyRef.getKeyValue()
123             + "\n");
124
125         // insert
126
statement.executeUpdate();
127       }
128     }
129     finally
130     {
131       try
132       {
133         statement.close();
134       }
135       catch (Exception JavaDoc e)
136       { /* ignored */
137       }
138     }
139   }
140
141   /**
142    * Select all rows from the BINDING_CATEGORY table for a given BindingKey.
143    *
144    * @param bindingKey String
145    * @param connection JDBC connection
146    * @throws java.sql.SQLException
147    */

148   public static Vector JavaDoc select(String JavaDoc bindingKey, Connection JavaDoc connection)
149     throws java.sql.SQLException JavaDoc
150   {
151     Vector JavaDoc keyRefList = new Vector JavaDoc();
152     PreparedStatement JavaDoc statement = null;
153     ResultSet JavaDoc resultSet = null;
154
155     try
156     {
157       // create a statement to query with
158
statement = connection.prepareStatement(selectSQL);
159       statement.setString(1, bindingKey.toString());
160
161       log.debug(
162         "select from BINDING_CATEGORY table:\n\n\t"
163           + selectSQL
164           + "\n\t BINDING_KEY="
165           + bindingKey.toString()
166           + "\n");
167
168       // execute the statement
169
resultSet = statement.executeQuery();
170       while (resultSet.next())
171       {
172         KeyedReference keyRef = new KeyedReference();
173         keyRef.setTModelKey(resultSet.getString(1));//("TMODEL_KEY_REF"));
174
keyRef.setKeyName(resultSet.getString(2));//("KEY_NAME"));
175
keyRef.setKeyValue(resultSet.getString(3));//("KEY_VALUE"));
176
keyRefList.add(keyRef);
177       }
178
179       return keyRefList;
180     }
181     finally
182     {
183       try
184       {
185         resultSet.close();
186         statement.close();
187       }
188       catch (Exception JavaDoc e)
189       { /* ignored */
190       }
191     }
192   }
193
194   /**
195    * Delete multiple rows from the BINDING_CATEGORY table that are assigned to the
196    * BindingKey specified.
197    *
198    * @param bindingKey String
199    * @param connection JDBC connection
200    * @throws java.sql.SQLException
201    */

202   public static void delete(String JavaDoc bindingKey, Connection JavaDoc connection)
203     throws java.sql.SQLException JavaDoc
204   {
205     PreparedStatement JavaDoc statement = null;
206
207     try
208     {
209       // prepare the delete
210
statement = connection.prepareStatement(deleteSQL);
211       statement.setString(1, bindingKey.toString());
212
213       log.debug(
214         "delete from BINDING_CATEGORY table:\n\n\t"
215           + deleteSQL
216           + "\n\t BINDING_KEY="
217           + bindingKey.toString()
218           + "\n");
219
220       // execute
221
statement.executeUpdate();
222     }
223     finally
224     {
225       try
226       {
227         statement.close();
228       }
229       catch (Exception JavaDoc e)
230       { /* ignored */
231       }
232     }
233   }
234 }
235
Popular Tags