View Javadoc
1 // $Header: /cvsroot/daoexamples/daoexamples/src/java/daoexamples/movie/MovieDAOImpl.java,v 1.5 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 * 20 * <p> 21 * This <b>Data Access Object</b> demarcates transactions 22 * internally. The caller cannot demarcate transactions. 23 * Transactions are demarcated using the JDBC API. 24 * </p> 25 * <p> 26 * Instances of this class are not thread-safe. 27 * </p> 28 * 29 * @see MovieDAOImplJTA 30 * 31 * @author Sean C. Sullivan 32 * 33 * 34 */ 35 class MovieDAOImpl implements MovieDAO 36 { 37 static final private Log log = LogFactory.getLog(MovieDAOImpl.class); 38 39 private boolean bIsClosed = false; 40 41 /*** 42 * 43 * 44 * 45 * 46 */ 47 public MovieDAOImpl() 48 { 49 bIsClosed = false; 50 } 51 52 public Movie findMovieById(final String id) 53 throws MovieNotFoundException 54 { 55 if (id == null) 56 { 57 throw new NullPointerException("id parameter"); 58 } 59 60 Connection conn = MovieUtil.getNonXADBConnection(); 61 62 Movie result = null; 63 ResultSet rs = null; 64 PreparedStatement stmtSelect = null; 65 66 try 67 { 68 StringBuffer sbSelect = new StringBuffer(); 69 70 sbSelect.append("SELECT movie_id, rating, year, title FROM "); 71 sbSelect.append(MovieConstants.MOVIE_TABLE_NAME); 72 sbSelect.append(" WHERE movie_id = ?"); 73 74 stmtSelect = conn.prepareStatement(sbSelect.toString()); 75 76 stmtSelect.setString(1, id); 77 78 rs = stmtSelect.executeQuery(); 79 Collection c = MovieUtil.makeMovieObjectsFromResultSet(rs); 80 81 if (c.size() != 1) 82 { 83 throw new MovieNotFoundException("id = " + id); 84 } 85 86 Iterator iter = c.iterator(); 87 88 result = (Movie) iter.next(); 89 } 90 catch (SQLException ex) 91 { 92 log.error(ex); 93 throw new DAORuntimeException(ex); 94 } 95 finally 96 { 97 MovieUtil.closeStatement(stmtSelect); 98 MovieUtil.closeResultSet(rs); 99 MovieUtil.closeJDBCConnection(conn); 100 } 101 102 return result; 103 } 104 105 public java.util.Collection findMoviesByYear(final String year) 106 { 107 if (year == null) 108 { 109 throw new NullPointerException("year parameter"); 110 } 111 112 Connection conn = MovieUtil.getNonXADBConnection(); 113 114 Collection result = null; 115 116 ResultSet rs = null; 117 PreparedStatement stmtSelect = null; 118 119 try 120 { 121 StringBuffer sbSelect = new StringBuffer(); 122 123 sbSelect.append("SELECT movie_id, rating, year, title FROM "); 124 sbSelect.append(MovieConstants.MOVIE_TABLE_NAME); 125 sbSelect.append(" WHERE year = ?"); 126 127 stmtSelect = conn.prepareStatement(sbSelect.toString()); 128 129 stmtSelect.setString(1, year); 130 131 rs = stmtSelect.executeQuery(); 132 133 result = MovieUtil.makeMovieObjectsFromResultSet(rs); 134 135 } 136 catch (SQLException ex) 137 { 138 log.error(ex); 139 throw new DAORuntimeException(ex); 140 } 141 finally 142 { 143 MovieUtil.closeStatement(stmtSelect); 144 MovieUtil.closeResultSet(rs); 145 MovieUtil.closeJDBCConnection(conn); 146 } 147 148 return result; 149 } 150 151 public void deleteMovie(final String id) 152 throws MovieNotFoundException 153 { 154 if (id == null) 155 { 156 throw new NullPointerException("id parameter"); 157 } 158 159 Connection conn = MovieUtil.getNonXADBConnection(); 160 161 PreparedStatement stmtDelete = null; 162 163 try 164 { 165 StringBuffer sbDelete = new StringBuffer(); 166 167 sbDelete.append("DELETE FROM "); 168 sbDelete.append(MovieConstants.MOVIE_TABLE_NAME); 169 sbDelete.append(" WHERE movie_id = ?"); 170 171 stmtDelete = conn.prepareStatement(sbDelete.toString()); 172 173 stmtDelete.setString(1, id); 174 175 int rows = stmtDelete.executeUpdate(); 176 177 if (rows != 1) 178 { 179 throw new SQLException( 180 "executeUpdate return value: " 181 + rows); 182 } 183 184 } 185 catch (SQLException ex) 186 { 187 log.error(ex); 188 throw new DAORuntimeException(ex); 189 } 190 finally 191 { 192 MovieUtil.closeStatement(stmtDelete); 193 MovieUtil.closeJDBCConnection(conn); 194 } 195 } 196 197 public Movie createMovie( 198 final String rating, 199 final String year, 200 final String title) 201 { 202 if (rating == null) 203 { 204 throw new NullPointerException("rating parameter"); 205 } 206 207 if (year == null) 208 { 209 throw new NullPointerException("year parameter"); 210 } 211 if (title == null) 212 { 213 throw new NullPointerException("title parameter"); 214 } 215 216 Movie result = null; 217 PreparedStatement stmtInsert = null; 218 219 Connection conn = MovieUtil.getNonXADBConnection(); 220 221 try 222 { 223 String movie_id = MovieUtil.getUniqueMovieId(conn); 224 225 StringBuffer sbInsert = new StringBuffer(); 226 227 sbInsert.append("INSERT INTO "); 228 sbInsert.append(MovieConstants.MOVIE_TABLE_NAME); 229 sbInsert.append(" ( movie_id, rating, year, title ) "); 230 sbInsert.append(" VALUES ("); 231 sbInsert.append(movie_id); 232 sbInsert.append(", ?, ?, ?) "); 233 234 stmtInsert = conn.prepareStatement(sbInsert.toString()); 235 236 stmtInsert.setString(1, rating); 237 stmtInsert.setString(2, year); 238 stmtInsert.setString(3, title); 239 240 log.info("About to execute INSERT: values " 241 + rating 242 + ", " 243 + year 244 + ", " 245 + title); 246 247 int rows = stmtInsert.executeUpdate(); 248 249 if (rows != 1) 250 { 251 throw new SQLException( 252 "executeUpdate return value: " 253 + rows); 254 } 255 256 result = new MovieImpl(movie_id, 257 rating, 258 year, 259 title); 260 261 } 262 catch (SQLException ex) 263 { 264 log.error(ex); 265 throw new DAORuntimeException(ex); 266 } 267 finally 268 { 269 MovieUtil.closeStatement(stmtInsert); 270 MovieUtil.closeJDBCConnection(conn); 271 } 272 return result; 273 } 274 275 public void updateMovie( 276 final String id, 277 final String rating, 278 final String year, 279 final String title) throws MovieNotFoundException 280 { 281 if (id == null) 282 { 283 throw new NullPointerException("id parameter"); 284 } 285 286 if (rating == null) 287 { 288 throw new NullPointerException("rating parameter"); 289 } 290 291 if (year == null) 292 { 293 throw new NullPointerException("year parameter"); 294 } 295 if (title == null) 296 { 297 throw new NullPointerException("title parameter"); 298 } 299 300 Connection conn = MovieUtil.getNonXADBConnection(); 301 302 PreparedStatement stmtUpdate = null; 303 304 try 305 { 306 StringBuffer sbUpdate = new StringBuffer(); 307 308 sbUpdate.append("UPDATE "); 309 sbUpdate.append(MovieConstants.MOVIE_TABLE_NAME); 310 sbUpdate.append(" SET "); 311 sbUpdate.append(" rating = ?, "); 312 sbUpdate.append(" year = ?, "); 313 sbUpdate.append(" title = ? "); 314 sbUpdate.append(" WHERE movie_id = ?"); 315 316 stmtUpdate = conn.prepareStatement(sbUpdate.toString()); 317 318 stmtUpdate.setString(1, rating); 319 stmtUpdate.setString(2, year); 320 stmtUpdate.setString(3, title); 321 stmtUpdate.setString(4, id); 322 323 int rows = stmtUpdate.executeUpdate(); 324 325 if (rows != 1) 326 { 327 throw new SQLException( 328 "executeUpdate return value: " 329 + rows); 330 } 331 332 } 333 catch (SQLException ex) 334 { 335 throw new DAORuntimeException(ex); 336 } 337 finally 338 { 339 MovieUtil.closeStatement(stmtUpdate); 340 MovieUtil.closeJDBCConnection(conn); 341 } 342 } 343 344 public void close() 345 { 346 log.info("close() called"); 347 bIsClosed = true; 348 } 349 350 public boolean isClosed() 351 { 352 return bIsClosed; 353 } 354 355 }

This page was automatically generated by Maven