Section 4.9. Creating JDBCCarDAO


4.9. Creating JDBCCarDAO

Our first CarDAO was fine to get the project kick-started, but ArrayLists aren't the best long-term persistence strategy. Let's create a second DAO that takes advantage of the infrastructure we've just put in place.

Since two classes provide different implementations of the same functionality, we should create a common Interface. In addition to making it trivial to switch back and forth between the two concrete implementations, it will also pave the way for us to add a third DAO implementation for Hibernate in the next chapter.

In the ch04/common source tree, notice that we renamed our old DAO class to InMemoryCarDAO. We didn't touch any of the methods, just the name of the class and the corresponding constructor names, as in Example 4-11.

Example 4-11. CarDAO.java
 package com.jbossatwork.dao; import java.util.*; import com.jbossatwork.dto.CarDTO; public class InMemoryCarDAO implements CarDAO {     private List carList;     public InMemoryCarDAO(  )     {         carList = new ArrayList(  );         carList.add(new CarDTO("Toyota", "Camry", "2005"));         carList.add(new CarDTO("Toyota", "Corolla", "1999"));         carList.add(new CarDTO("Ford", "Explorer", "2005"));     }     public List findAll(  )     {         return carList;     }  } 

The CarDAO Interface simply defines the method signature for findAll( ):

 package com.jbossatwork.dao; import java.util.*; public interface CarDAO {     public List findAll(  ); } 

The new JDBCCarDAO uses the new DataSource and ServiceLocator class to build the ArrayList of CarDTOs in Example 4-12.

Example 4-12. JDBCCarDAO.java
 package com.jbossatwork.dao; import java.util.*; import java.sql.*; import javax.sql.*; import com.jbossatwork.dto.CarDTO; import com.jbossatwork.util.*; public class JDBCCarDAO implements CarDAO {     private List carList;     private static final String DATA_SOURCE="java:comp/env/jdbc/JBossAtWorkDS";     public JDBCCarDAO(  )     {  }     public List findAll(  )     {         List carList = new ArrayList(  );         DataSource dataSource = null;         Connection conn = null;         Statement stmt = null;         ResultSet rs = null;         try         {             dataSource = ServiceLocator.getDataSource(DATA_SOURCE);             conn = dataSource.getConnection(  );             stmt = conn.createStatement(  );             rs = stmt.executeQuery("select * from CAR");             while(rs.next(  ))             {                 CarDTO car = new CarDTO(  );                 car.setMake(rs.getString("MAKE"));                 car.setModel(rs.getString("MODEL"));                 car.setModelYear(rs.getString("MODEL_YEAR"));                 carList.add(car);             }         }         catch (Exception e)         {             System.out.println(e);         }         finally         {             try             {                 if(rs != null){rs.close(  );}                 if(stmt != null){stmt.close(  );}                 if(conn != null){conn.close(  );}             }             catch(Exception e)             {                 System.out.println(e);             }         }         return carList;     }  } 

Finally let's change ControllerServlet to instantiate the correct DAO. Notice that courtesy of the new interface we created, switching between implementations is as simple as changing the "new" side of the equation, as shown in Example 4-13.

Example 4-13. ControllerServlet.java
 // perform action         if(VIEW_CAR_LIST_ACTION.equals(actionName))         {             CarDAO carDAO = new JDBCCarDAO(  );             request.setAttribute("carList", carDAO.findAll(  ));             destinationPage = "/carList.jsp";         } 

Now that everything is in place, let's compile and deploy the EAR. Change to ch04/04a-datasource and type ant. Copy the jaw.ear file to $JBOSS_HOME/server/default/deploy and visit http://localhost:8080/jaw. (Alternately, you can use the deploy or colddeploy Ant targets.)



JBoss at Work. A Practical Guide
JBoss at Work: A Practical Guide
ISBN: 0596007345
EAN: 2147483647
Year: 2004
Pages: 197

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net