KickJava   Java API By Example, From Geeks To Geeks.

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


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.sql.SQLException JavaDoc;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.juddi.datatype.publisher.Publisher;
26
27 /**
28  * @author Steve Viens (sviens@apache.org)
29  */

30 public class PublisherTable
31 {
32   // private reference to the jUDDI logger
33
private static Log log = LogFactory.getLog(PublisherTable.class);
34
35   static String JavaDoc insertSQL = null;
36   static String JavaDoc selectSQL = null;
37   static String JavaDoc deleteSQL = null;
38   static String JavaDoc updateSQL = null;
39   static String JavaDoc verifyAdminSQL = null;
40
41   static
42   {
43     // buffer used to build SQL statements
44
StringBuffer JavaDoc sql = null;
45
46     // build insertSQL
47
sql = new StringBuffer JavaDoc(150);
48     sql.append("INSERT INTO PUBLISHER (");
49     sql.append("PUBLISHER_ID,");
50     sql.append("PUBLISHER_NAME,");
51     sql.append("EMAIL_ADDRESS,");
52     sql.append("IS_ADMIN, ");
53     sql.append("IS_ENABLED) ");
54     sql.append("VALUES (?,?,?,?,?)");
55     insertSQL = sql.toString();
56
57     // build selectSQL
58
sql = new StringBuffer JavaDoc(200);
59     sql.append("SELECT ");
60     sql.append("PUBLISHER_NAME,");
61     sql.append("EMAIL_ADDRESS,");
62     sql.append("IS_ADMIN,");
63     sql.append("IS_ENABLED ");
64     sql.append("FROM PUBLISHER ");
65     sql.append("WHERE PUBLISHER_ID=?");
66     selectSQL = sql.toString();
67
68     // build deleteSQL
69
sql = new StringBuffer JavaDoc(200);
70     sql.append("DELETE FROM PUBLISHER ");
71     sql.append("WHERE PUBLISHER_ID=?");
72     deleteSQL = sql.toString();
73
74     // build updateSQL
75
sql = new StringBuffer JavaDoc(200);
76     sql.append("UPDATE PUBLISHER ");
77     sql.append("SET PUBLISHER_NAME=?,");
78     sql.append("EMAIL_ADDRESS=?,");
79     sql.append("IS_ADMIN=?,");
80     sql.append("IS_ENABLED=? ");
81     sql.append("WHERE PUBLISHER_ID=?");
82     updateSQL = sql.toString();
83   }
84
85   /**
86    * Insert new row into the PUBLISHER table.
87    *
88    * @param publisher
89    * @param connection JDBC connection
90    * @throws SQLException
91    */

92   public static void insert(Publisher publisher,Connection JavaDoc connection)
93     throws SQLException JavaDoc
94   {
95     if (publisher == null)
96       return; // nothing to insert
97

98     PreparedStatement JavaDoc statement = null;
99
100     try
101     {
102       statement = connection.prepareStatement(insertSQL);
103       statement.setString(1,publisher.getPublisherID());
104       statement.setString(2,publisher.getName());
105       statement.setString(3,publisher.getEmailAddress());
106       statement.setString(4,String.valueOf(publisher.isAdmin()));
107       statement.setString(5,String.valueOf(publisher.isEnabled()));
108
109       log.debug("insert into PUBLISHER table:\n\n\t" + insertSQL +
110         "\n\t PUBLISHER_ID=" + publisher.getPublisherID() +
111         "\n\t PUBLISHER_NAME=" + publisher.getName() +
112         "\n\t EMAIL_ADDRESS=" + publisher.getEmailAddress() +
113         "\n\t IS_ADMIN=" + publisher.isAdmin() +
114         "\n\t IS_ENABLED=" + publisher.isEnabled() + "\n");
115
116       // insert
117
statement.executeUpdate();
118     }
119     catch(SQLException JavaDoc sqlex)
120     {
121       log.error(sqlex.getMessage());
122       throw sqlex;
123     }
124     finally
125     {
126       try { statement.close(); } catch (Exception JavaDoc e) { /* ignored */ }
127     }
128   }
129
130   /**
131    * Select one row from the PUBLISHER table.
132    *
133    * @param publisherID
134    * @param connection JDBC connection
135    * @return Publisher
136    * @throws SQLException
137    */

138   public static Publisher select(String JavaDoc publisherID,Connection JavaDoc connection)
139     throws SQLException JavaDoc
140   {
141     // return 'null' if a null publisherID is specified.
142
if (publisherID == null)
143       return null;
144
145     Publisher publisher = null;
146     PreparedStatement JavaDoc statement = null;
147     ResultSet JavaDoc resultSet = null;
148
149     try
150     {
151       statement = connection.prepareStatement(selectSQL);
152       statement.setString(1,publisherID);
153
154       log.debug("select from PUBLISHER table:\n\n\t" + selectSQL +
155         "\n\t PUBLISHER_ID=" + publisherID + "\n");
156
157       resultSet = statement.executeQuery();
158       if (resultSet.next())
159       {
160         publisher = new Publisher();
161         publisher.setPublisherID(publisherID);
162         publisher.setName(resultSet.getString(1));//("PUBLISHER_NAME"));
163
publisher.setEmailAddress(resultSet.getString(2));//("EMAIL_ADDRESS"));
164
publisher.setAdminValue(resultSet.getString(3));//("IS_ADMIN"));
165
publisher.setEnabledValue(resultSet.getString(4));//("IS_ENABLED"));
166
}
167
168       return publisher;
169     }
170     catch(SQLException JavaDoc sqlex)
171     {
172       log.error(sqlex.getMessage());
173       throw sqlex;
174     }
175     finally
176     {
177       try { resultSet.close(); } catch (Exception JavaDoc e) { /* ignored */ }
178       try { statement.close(); } catch (Exception JavaDoc e) { /* ignored */ }
179     }
180   }
181
182   /**
183    * Delete row from the PUBLISHER table.
184    *
185    * @param publisherID
186    * @param connection
187    * @throws SQLException
188    */

189   public static void delete(String JavaDoc publisherID,Connection JavaDoc connection)
190     throws SQLException JavaDoc
191   {
192     PreparedStatement JavaDoc statement = null;
193
194     try
195     {
196       // prepare the delete
197
statement = connection.prepareStatement(deleteSQL);
198       statement.setString(1,publisherID);
199
200       log.debug("delete from PUBLISHER table:\n\n\t" + deleteSQL +
201         "\n\t PUBLISHER_ID=" + "\n");
202
203       // execute the delete
204
statement.executeUpdate();
205     }
206     catch(SQLException JavaDoc sqlex)
207     {
208       log.error(sqlex.getMessage());
209       throw sqlex;
210     }
211     finally
212     {
213       try { statement.close(); } catch (Exception JavaDoc e) { /* ignored */ }
214     }
215   }
216
217   /**
218    * Update the PUBLISHER table for a particular PublisherID.
219    *
220    * @param publisher
221    * @param connection JDBC connection
222    * @throws SQLException
223    */

224   public static void update(Publisher publisher,Connection JavaDoc connection)
225     throws SQLException JavaDoc
226   {
227     PreparedStatement JavaDoc statement = null;
228
229     try
230     {
231       // prepare
232
statement = connection.prepareStatement(updateSQL);
233       statement.setString(1,publisher.getName());
234       statement.setString(2,publisher.getEmailAddress());
235       statement.setString(3,String.valueOf(publisher.isAdmin()));
236       statement.setString(4,String.valueOf(publisher.isEnabled()));
237       statement.setString(5,publisher.getPublisherID());
238
239       log.debug("update PUBLISHER table:\n\n\t" + updateSQL +
240         "\n\t PUBLISHER_NAME=" + publisher.getName() +
241         "\n\t EMAIL_ADDRESS=" + publisher.getEmailAddress() +
242         "\n\t IS_ADMIN=" + publisher.isAdmin() +
243         "\n\t IS_ENABLED=" + publisher.isEnabled() +
244         "\n\t PUBLISHER_ID=" + publisher.getPublisherID() + "\n");
245
246       // execute
247
statement.executeUpdate();
248     }
249     catch(SQLException JavaDoc sqlex)
250     {
251       log.error(sqlex.getMessage());
252       throw sqlex;
253     }
254     finally
255     {
256       try { statement.close(); } catch (Exception JavaDoc e) { /* ignored */ }
257     }
258   }
259 }
Popular Tags