View Javadoc
1 // $Header: /cvsroot/daoexamples/daoexamples/src/java/daoexamples/movie/MovieUtil.java,v 1.7 2003/09/27 17:27:11 sullis Exp $ 2 3 /* 4 * 5 * 6 * 7 * 8 */ 9 10 package daoexamples.movie; 11 12 import daoexamples.exception.*; 13 import java.sql.*; 14 import java.util.Collection; 15 import javax.sql.*; 16 import javax.naming.Context; 17 import javax.naming.InitialContext; 18 import javax.naming.NamingException; 19 import javax.transaction.UserTransaction; 20 import org.apache.commons.logging.*; 21 22 /*** 23 * 24 * utilities for the Movie DAO package 25 * 26 * @author Sean C. Sullivan 27 * 28 */ 29 public final class MovieUtil 30 { 31 static final private Log log = LogFactory.getLog(MovieUtil.class); 32 33 static public String getUniqueMovieId(final Connection conn) 34 throws java.sql.SQLException 35 { 36 37 if (null == conn) 38 { 39 throw new NullPointerException("conn parameter"); 40 } 41 42 if (conn.isClosed()) 43 { 44 throw new IllegalArgumentException( 45 "connection is closed"); 46 } 47 48 String id = null; 49 50 Statement stmtSelect = null; 51 ResultSet rs = null; 52 53 StringBuffer sbSelect = new StringBuffer(); 54 55 sbSelect.append("SELECT "); 56 sbSelect.append(MovieConstants.MOVIE_ID_SEQUENCE_NAME); 57 sbSelect.append(".nextval "); 58 sbSelect.append(" FROM DUAL "); 59 60 try 61 { 62 stmtSelect = conn.createStatement(); 63 rs = stmtSelect.executeQuery(sbSelect.toString()); 64 rs.next(); 65 id = rs.getString("nextval"); 66 } 67 finally 68 { 69 MovieUtil.closeStatement(stmtSelect); 70 MovieUtil.closeResultSet(rs); 71 } 72 73 return id; 74 } 75 76 /*** 77 * 78 * @param conn must be non-null 79 * 80 * the caller is responsible for committing the transaction 81 * 82 * @throws SQLException 83 * 84 */ 85 static public void setupMovieTable() 86 throws SQLException 87 { 88 Statement stmtCreateTable = null; 89 Statement stmtDropTable = null; 90 Statement stmtCreateSeq = null; 91 Statement stmtDropSeq = null; 92 93 Connection conn = null; 94 95 try 96 { 97 // 98 // 99 // This method uses a non-XA JDBC connection because 100 // "DROP TABLE" and "CREATE TABLE" will only work 101 // with a non-XA JDBC connection. 102 // 103 conn = MovieUtil.getNonXADBConnection(); 104 conn.setAutoCommit(true); 105 106 log.debug( 107 conn.getMetaData().getDriverName() 108 + " - " 109 + conn.getMetaData().getDriverVersion()); 110 111 StringBuffer sbDropTable = new StringBuffer(); 112 sbDropTable.append("DROP TABLE "); 113 sbDropTable.append(MovieConstants.MOVIE_TABLE_NAME); 114 115 stmtDropTable = conn.createStatement(); 116 117 try 118 { 119 stmtDropTable.execute(sbDropTable.toString()); 120 } 121 catch (SQLException ignored) 122 { 123 // The 'DROP TABLE' statement may throw 124 // an exception. We ignore it. 125 } 126 127 StringBuffer sbDropSeq = new StringBuffer(); 128 sbDropSeq.append("DROP SEQUENCE "); 129 sbDropSeq.append(MovieConstants.MOVIE_ID_SEQUENCE_NAME); 130 131 stmtDropSeq = conn.createStatement(); 132 133 try 134 { 135 stmtDropSeq.execute(sbDropSeq.toString()); 136 } 137 catch (SQLException ignored) 138 { 139 // The 'DROP SEQUENCE' statement may throw 140 // an exception. We ignore it. 141 } 142 143 StringBuffer sbCreateSeq = new StringBuffer(); 144 sbCreateSeq.append("CREATE SEQUENCE "); 145 sbCreateSeq.append(MovieConstants.MOVIE_ID_SEQUENCE_NAME); 146 sbCreateSeq.append(" START WITH 1 INCREMENT BY 1 NOMAXVALUE ORDER NOCACHE"); 147 148 stmtCreateSeq = conn.createStatement(); 149 stmtCreateSeq.execute(sbCreateSeq.toString()); 150 151 StringBuffer sbCreateTable = new StringBuffer(); 152 sbCreateTable.append("CREATE TABLE "); 153 sbCreateTable.append(MovieConstants.MOVIE_TABLE_NAME); 154 sbCreateTable.append(" ( "); 155 sbCreateTable.append(" movie_id NUMBER(12), "); 156 sbCreateTable.append(" rating VARCHAR2(10), "); 157 sbCreateTable.append(" year VARCHAR2(5), "); 158 sbCreateTable.append(" title VARCHAR2(60) "); 159 sbCreateTable.append(" ) "); 160 161 stmtCreateTable = conn.createStatement(); 162 163 log.debug("CREATE TABLE statement: " 164 + sbCreateTable.toString()); 165 166 stmtCreateTable.execute(sbCreateTable.toString()); 167 168 log.debug("Statement executed: " 169 + sbCreateTable.toString()); 170 171 } 172 finally 173 { 174 MovieUtil.closeStatement(stmtDropTable); 175 MovieUtil.closeStatement(stmtDropSeq); 176 MovieUtil.closeStatement(stmtCreateTable); 177 MovieUtil.closeStatement(stmtCreateSeq); 178 MovieUtil.closeJDBCConnection(conn); 179 } 180 } 181 182 private MovieUtil() 183 { 184 // this constructor is intentionally private 185 } 186 187 static public Connection getNonXADBConnection() 188 { 189 Connection conn = null; 190 191 DataSource ds = null; 192 193 try 194 { 195 Object obj = lookup(MovieConstants.MOVIE_NONXA_DATASOURCE_NAME); 196 ds = (DataSource) narrow(obj, DataSource.class); 197 198 conn = ds.getConnection(); 199 } 200 catch (SQLException ex) 201 { 202 throw new DAORuntimeException(ex); 203 } 204 return conn; 205 } 206 207 public static Connection getXADBConnection() 208 { 209 Connection conn = null; 210 211 DataSource ds = null; 212 213 try 214 { 215 Object obj = lookup(MovieConstants.MOVIE_XA_DATASOURCE_NAME); 216 ds = (DataSource) narrow(obj, DataSource.class); 217 218 conn = ds.getConnection(); 219 } 220 catch (SQLException ex) 221 { 222 throw new DAORuntimeException(ex); 223 } 224 225 226 return conn; 227 } 228 229 public static void closeJDBCConnection(final Connection conn) 230 { 231 if (conn != null) 232 { 233 try 234 { 235 conn.close(); 236 } 237 catch (SQLException ex) 238 { 239 log.error(conn, ex); 240 } 241 } 242 } 243 244 public static void closeStatement(final Statement stmt) 245 { 246 if (stmt != null) 247 { 248 try 249 { 250 stmt.close(); 251 } 252 catch (SQLException ex) 253 { 254 log.error(stmt, ex); 255 } 256 } 257 } 258 259 public static void closeResultSet(final ResultSet rs) 260 { 261 if (rs != null) 262 { 263 try 264 { 265 rs.close(); 266 } 267 catch (SQLException ex) 268 { 269 log.error(rs, ex); 270 } 271 } 272 } 273 274 static public UserTransaction getUserTransaction() 275 { 276 Object obj = lookup("java:comp/UserTransaction"); 277 278 UserTransaction tx = (UserTransaction) narrow(obj, UserTransaction.class); 279 280 return tx; 281 } 282 283 static public Object lookup(final String name) 284 { 285 Context ctx = getInitialContext(); 286 287 Object result = null; 288 289 try 290 { 291 result = ctx.lookup(name); 292 } 293 catch (NamingException ex) 294 { 295 throw new DAORuntimeException(ex); 296 } 297 298 return result; 299 } 300 301 static public Context getInitialContext() 302 { 303 Context ctx = null; 304 305 try 306 { 307 ctx = new InitialContext(); 308 } 309 catch (NamingException ex) 310 { 311 throw new DAORuntimeException(ex); 312 } 313 return ctx; 314 } 315 316 static public Object narrow(final Object obj, final Class clazz) 317 { 318 return javax.rmi.PortableRemoteObject.narrow(obj, clazz); 319 } 320 321 /*** 322 * 323 * @param rs must be non-null 324 * 325 * @return a Collection containing zero or more {@link Movie} objects 326 * 327 */ 328 static public Collection makeMovieObjectsFromResultSet(final ResultSet rs) 329 throws java.sql.SQLException 330 { 331 Collection result = new java.util.ArrayList(); 332 333 while (rs.next()) 334 { 335 String id = rs.getString("movie_id"); 336 String rating = rs.getString("rating"); 337 String year = rs.getString("year"); 338 String title = rs.getString("title"); 339 Movie m = new MovieImpl(id, rating, year, title); 340 result.add(m); 341 } 342 343 return result; 344 } 345 346 }

This page was automatically generated by Maven