Java Reference
In-Depth Information
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/YachtClubDB");
Connection conn = ds.getConnection();
PreparedStatement pStmt = cx.preparedStatement("SELECT yacht_name,
engine_type FROM
Yacht WHERE yacht_name=?")
PStmt.setString(1, yachtName);
ResultSet rs = pStmt.execute();
String engineType = rs.getString(2);
Note that the code snippet is for illustration only. When you develop real-life applications, you must code
more defensively against exceptions. For example, you should check whether the result is null before
you try to get the value of engine_type .
For the JDO approach, the code may look like this:
… …
InitialContext ctx = new InitialContext();
PersistenceManagerFactory pmf = (PersistenceManagerFactory) ctx.lookup(
"java:comp/env/jdbc/YachtClubPMF");
PersistenceManager pm = pmf.getPersistenceManager();
YachtKey yKey = new YachtKey(yachtName);
Yacht aYacht = (Yacht) pm.getObjectById(yKey);
String engineType = aYacht.getEngineType();
… …
Note that the code snippet is again simplified to demonstrate the core concept. The preceding code can
be part of the session EJBs, servlets, or JSPs, depending upon your application design. It is seen that
the session EJBs and servlets/JSPs use either JDBC or JDO in a similar manner.
Entity beans with bean-manager persistence can also take advantage of the simple and transparent
persistence JDO provides. Instead of writing SQL code, developers access persisted objects' states via
JDO-persistent class instances. Recall the discussion in Chapter 21 regarding that data-access codes
are mostly in the following methods: ejbCreate , ejbRemove , ejbFindByxxx , ejbLoad and
ejbStore . If JDO is used, you will write these methods using JDO APIs. For example, ejbCreate
creates a persistent instance by first instantiating a persistent object and then calling the
PersistenceManager 's makePpersistent method. ejbRemove deletes a persistent instance by
calling the PersistenceManager 's deletePersistent method. ejbLoad retrieves a persistent
instance by calling a getObjectByxxx method.
Entity beans with container-managed persistence have a unique method for persistence management.
The concrete bean class is automatically generated by the EJB container at the deployment phase
based on the information provided in the deployment descriptor. Database access is transparent to the
bean developer, although certain knowledge regarding database structure is required to map the
persistent and relational fields to corresponding database columns for deployment. There is typically no
need or viable mechanism to integrate CMP entity beans with JDO. You may have to choose either one
in your application based on your specific needs and constraints.
Summary
JDO bridges the gap between the Java object model and relational data model by providing a very
natural yet very simple object-oriented mechanism to store and retrieve java objects. In this chapter, you
have learned the following:
Search WWH ::




Custom Search