Java Reference
In-Depth Information
Listing 8.5
Use JDBC instead of a full entity bean for lightweight problems
public java.util.List getBoardNames() throws java.rmi.RemoteException {
java.util.Vector names = new java.util.Vector();
java.sql.Connection conn = null;
java.sql.PreparedStatement stmt = null;
java.sql.ResultSet rs = null;
We use a pooled
connection (chapter 7).
try {
conn = getPooledConnection();
String sqlString = "SELECT DB2ADMIN.BOARD.NAME AS NAME" +
" FROM DB2ADMIN.BOARD";
stmt = conn.prepareStatement(sqlString);
rs = stmt.executeQuery();
marshalBoardNames(rs, names);
} catch (Exception e) {
throw new RemoteException("Database exception: " + e);
} finally {
if (rs != null) {
try { rs.close();
} catch (java.sql.SQLException e) {
}
}
if (stmt != null) {
try {
stmt.close();
} catch (java.sql.SQLException e) {
}
}
if (conn != null) {
try {
conn.close();
} catch (java.sql.SQLException e) {
}
}
}
return names;
o
o Cleanup is in
finally (chapter 7).
}
This approach lets us return a simple list of board names without having to
load all the board EJB s, which would do much more work than we need. We
may choose instead to extend our model to keep things consistent, but this
option is available as a performance optimization. A scary number of entity
beans do nothing but provide a simple reference table. In this case, the EJB
implementation will be more difficult and will provide much more capability
than we need. For reference tables, we don't have to map a full object model
Search WWH ::




Custom Search