1 // $Header: /cvsroot/daoexamples/daoexamples/src/java/daoexamples/movie/MovieDAOImplJTA.java,v 1.7 2003/08/25 03:30:12 sullis Exp $
2
3 /*
4 *
5 *
6 *
7 *
8 */
9
10 package daoexamples.movie;
11
12 import java.sql.*;
13 import java.util.*;
14 import daoexamples.exception.*;
15
16 import org.apache.commons.logging.*;
17
18 /***
19 * <p>
20 * This class is a <b>Data Access Object</b>.
21 * </p>
22 * <p>
23 * This class is designed for use with the Java Transaction API.
24 * </p>
25 * <p>
26 * This class assumes that the caller is using JTA to demarcate
27 * transactions.
28 * </p>
29 * <p>
30 * You can learn more about JTA at
31 * <a href="http://java.sun.com/products/jta/">
32 * http://java.sun.com/products/jta/</a>
33 * </p>
34 * <p>
35 * Instances of this class are not thread-safe.
36 * </p>
37 *
38 * @see MovieDAOImpl
39 *
40 * @author Sean C. Sullivan
41 *
42 *
43 */
44 class MovieDAOImplJTA implements MovieDAO
45 {
46 static final private Log log = LogFactory.getLog(MovieDAOImplJTA.class);
47
48 private java.sql.Connection conn = null;
49 private boolean bIsClosed;
50
51 /***
52 *
53 *
54 *
55 *
56 */
57 public MovieDAOImplJTA()
58 {
59 bIsClosed = false;
60 conn = MovieUtil.getXADBConnection();
61 }
62
63 public Movie findMovieById(final String id)
64 throws MovieNotFoundException
65 {
66 if (id == null)
67 {
68 throw new NullPointerException("id parameter");
69 }
70
71 Movie result = null;
72 ResultSet rs = null;
73 PreparedStatement stmtSelect = null;
74
75 StringBuffer sbSelect = new StringBuffer();
76
77 try
78 {
79 sbSelect.append("SELECT movie_id, rating, year, title FROM ");
80 sbSelect.append(MovieConstants.MOVIE_TABLE_NAME);
81 sbSelect.append(" WHERE movie_id = ?");
82
83 stmtSelect = conn.prepareStatement(sbSelect.toString());
84
85 stmtSelect.setString(1, id);
86
87 rs = stmtSelect.executeQuery();
88 Collection c = MovieUtil.makeMovieObjectsFromResultSet(rs);
89
90 log.debug("sbSelect = "
91 + sbSelect.toString()
92 + ", id = "
93 + id
94 + ", "
95 + c.size()
96 + " rows found");
97
98 if (c.size() != 1)
99 {
100 throw new MovieNotFoundException("id = " + id);
101 }
102
103 Iterator iter = c.iterator();
104
105 result = (Movie) iter.next();
106 }
107 catch (SQLException ex)
108 {
109 StringBuffer sbMsg = new StringBuffer();
110 sbMsg.append("sql = { ");
111 sbMsg.append(String.valueOf(sbSelect));
112 sbMsg.append(" }, id = '");
113 sbMsg.append(id);
114 sbMsg.append("'");
115 throw new DAORuntimeException(
116 sbMsg.toString(),
117 ex);
118 }
119 finally
120 {
121 MovieUtil.closeStatement(stmtSelect);
122 MovieUtil.closeResultSet(rs);
123 }
124
125 return result;
126 }
127
128 public java.util.Collection findMoviesByYear(final String year)
129 {
130 if (year == null)
131 {
132 throw new NullPointerException("year parameter");
133 }
134
135 Collection result = null;
136
137 ResultSet rs = null;
138 PreparedStatement stmtSelect = null;
139
140 StringBuffer sbSelect = new StringBuffer();
141
142 try
143 {
144 sbSelect.append("SELECT movie_id, rating, year, title FROM ");
145 sbSelect.append(MovieConstants.MOVIE_TABLE_NAME);
146 sbSelect.append(" WHERE year = ?");
147
148 stmtSelect = conn.prepareStatement(sbSelect.toString());
149
150 stmtSelect.setString(1, year);
151
152 rs = stmtSelect.executeQuery();
153
154 result = MovieUtil.makeMovieObjectsFromResultSet(rs);
155
156 }
157 catch (SQLException ex)
158 {
159 StringBuffer sbMsg = new StringBuffer();
160 sbMsg.append("sbSelect = ");
161 sbMsg.append(String.valueOf(sbSelect));
162 sbMsg.append(", year = ");
163 sbMsg.append(year);
164 throw new DAORuntimeException(
165 sbMsg.toString(),
166 ex);
167 }
168 finally
169 {
170 MovieUtil.closeStatement(stmtSelect);
171 MovieUtil.closeResultSet(rs);
172 }
173
174 return result;
175 }
176
177 public void deleteMovie(final String id)
178 throws MovieNotFoundException
179 {
180 if (id == null)
181 {
182 throw new NullPointerException("id parameter");
183 }
184
185 PreparedStatement stmtDelete = null;
186
187 StringBuffer sbSQLDelete = new StringBuffer();
188
189 try
190 {
191 sbSQLDelete.append("DELETE FROM ");
192 sbSQLDelete.append(MovieConstants.MOVIE_TABLE_NAME);
193 sbSQLDelete.append(" WHERE movie_id = ?");
194
195 stmtDelete = conn.prepareStatement(sbSQLDelete.toString());
196
197 stmtDelete.setString(1, id);
198
199 int rows = stmtDelete.executeUpdate();
200
201 if (rows != 1)
202 {
203 StringBuffer sbMsg = new StringBuffer();
204 sbMsg.append("executeUpdate returned ");
205 sbMsg.append(rows);
206 sbMsg.append(", id = ");
207 sbMsg.append(id);
208 throw new DAORuntimeException(
209 sbMsg.toString());
210 }
211
212 }
213 catch (SQLException ex)
214 {
215 StringBuffer sbMsg = new StringBuffer();
216 sbMsg.append("sql = { ");
217 sbMsg.append(String.valueOf(sbSQLDelete));
218 sbMsg.append(" }, id = ");
219 sbMsg.append(id);
220 throw new DAORuntimeException(
221 sbMsg.toString(),
222 ex);
223 }
224 finally
225 {
226 MovieUtil.closeStatement(stmtDelete);
227 }
228 }
229
230 public Movie createMovie(
231 final String rating,
232 final String year,
233 final String title)
234 {
235 if (rating == null)
236 {
237 throw new NullPointerException("rating parameter");
238 }
239
240 if (year == null)
241 {
242 throw new NullPointerException("year parameter");
243 }
244 if (title == null)
245 {
246 throw new NullPointerException("title parameter");
247 }
248
249 Movie result = null;
250 PreparedStatement stmtInsert = null;
251
252
253 StringBuffer sbInsert = new StringBuffer();
254
255 try
256 {
257 String movie_id = MovieUtil.getUniqueMovieId(conn);
258
259 sbInsert.append("INSERT INTO ");
260 sbInsert.append(MovieConstants.MOVIE_TABLE_NAME);
261 sbInsert.append(" ( movie_id, rating, year, title ) ");
262 sbInsert.append(" VALUES (");
263 sbInsert.append(movie_id);
264 sbInsert.append(", ?, ?, ?) ");
265
266 stmtInsert = conn.prepareStatement(sbInsert.toString());
267
268 stmtInsert.setString(1, rating);
269 stmtInsert.setString(2, year);
270 stmtInsert.setString(3, title);
271
272 log.debug("About to execute INSERT: values "
273 + rating
274 + ", "
275 + year
276 + ", "
277 + title);
278
279 int rows = stmtInsert.executeUpdate();
280
281 if (rows != 1)
282 {
283 StringBuffer sbMsg = new StringBuffer();
284 sbMsg.append("executeUpdate returned ");
285 sbMsg.append(rows);
286 sbMsg.append(", sql = { ");
287 sbMsg.append(String.valueOf(sbInsert));
288 sbMsg.append(" }");
289 throw new DAORuntimeException(
290 sbMsg.toString());
291 }
292
293 result = new MovieImpl(movie_id,
294 rating,
295 year,
296 title);
297
298 }
299 catch (SQLException ex)
300 {
301 StringBuffer sbMsg = new StringBuffer();
302 sbMsg.append("sql = { ");
303 sbMsg.append(String.valueOf(sbInsert));
304 sbMsg.append(" }");
305 throw new DAORuntimeException(
306 sbMsg.toString(),
307 ex);
308 }
309 finally
310 {
311 MovieUtil.closeStatement(stmtInsert);
312 }
313 return result;
314 }
315
316 public void updateMovie(
317 final String id,
318 final String rating,
319 final String year,
320 final String title) throws MovieNotFoundException
321 {
322 if (id == null)
323 {
324 throw new NullPointerException("id parameter");
325 }
326
327 if (rating == null)
328 {
329 throw new NullPointerException("rating parameter");
330 }
331
332 if (year == null)
333 {
334 throw new NullPointerException("year parameter");
335 }
336 if (title == null)
337 {
338 throw new NullPointerException("title parameter");
339 }
340
341 PreparedStatement stmtUpdate = null;
342
343 try
344 {
345 StringBuffer sbUpdate = new StringBuffer();
346
347 sbUpdate.append("UPDATE ");
348 sbUpdate.append(MovieConstants.MOVIE_TABLE_NAME);
349 sbUpdate.append(" SET ");
350 sbUpdate.append(" rating = ?, ");
351 sbUpdate.append(" year = ?, ");
352 sbUpdate.append(" title = ? ");
353 sbUpdate.append(" WHERE movie_id = ?");
354
355 stmtUpdate = conn.prepareStatement(sbUpdate.toString());
356
357 stmtUpdate.setString(1, rating);
358 stmtUpdate.setString(2, year);
359 stmtUpdate.setString(3, title);
360 stmtUpdate.setString(4, id);
361
362 int rows = stmtUpdate.executeUpdate();
363
364 if (rows != 1)
365 {
366 StringBuffer sbMsg = new StringBuffer();
367 sbMsg.append("executeUpdate returned ");
368 sbMsg.append(rows);
369 sbMsg.append(", id = '");
370 sbMsg.append(id);
371 sbMsg.append("'");
372 throw new DAORuntimeException(
373 sbMsg.toString());
374 }
375 }
376 catch (SQLException ex)
377 {
378 StringBuffer sbMsg = new StringBuffer();
379 sbMsg.append("id = ");
380 sbMsg.append(id);
381 throw new DAORuntimeException(
382 sbMsg.toString(),
383 ex);
384 }
385 finally
386 {
387 MovieUtil.closeStatement(stmtUpdate);
388 }
389 }
390
391 public void close()
392 {
393 log.info("close() called");
394
395 if ( ! isClosed() )
396 {
397 bIsClosed = true;
398 try
399 {
400 if (conn != null)
401 {
402 MovieUtil.closeJDBCConnection(conn);
403 }
404 }
405 finally
406 {
407 conn = null;
408 }
409
410 }
411 }
412
413 public boolean isClosed()
414 {
415 return bIsClosed;
416 }
417
418 }
This page was automatically generated by Maven