Listing 1  The execute method of the class BaseStatement

1		public synchronized List execute()
2		throws DAOException {
3
4		List results = new ArrayList();
5		PreparedStatement preparedStatement 6 = null;
7		ResultSet resultSet = null;
8
9			try {
10
11		getConnection();
12		preparedStatement = 13 getPreparedStatement();
14
15		processStatement(preparedStatement);
16		resultSet = 
17		executeStatement(preparedStatement);
18		results = 
19		parseResultSet(resultSet);
20
21		if (commitTransaction)
22		connection.commit();
23
	} catch (Exception e) {
       // Exception handling omitted for the sake of brevity.
      }
	return results;

}


Listing 2 Getting the connection

1			protected Connection getConnection()
2		throws NamingException, SQLException {
3
4			if (connection == null) {
5   		Context jndiContext = new
6		InitialContext();
7
8			DataSource ds =
9			(DataSource) 
10		jndiContext.lookup 
11
12		 (JNDINames.DATASOURCEJNDINAME);
13
14		connection = ds.getConnection();
15		connection.setAutoCommit(false);
16		}


return connection;
}



Listing 3 Getting and processing the statement.

1		protected PreparedStatement 
2		getPreparedStatement() 
3		throws Exception {
4
5		return 
6		connection.prepareStatement(preparedStatem
7		entText);
8		}

9
10		protected void processStatement(PreparedStatement 11 preparedStatement)
12 	throws Exception {

13		for (int i = 0; i < arguments.size(); i++) {

		preparedStatement.setObject(i + 1, arguments.get(i));
	}
}



Listing 4

1		public BaseStatement(String 2 preparedStatementText, List arguments) {
3
4		this.preparedStatementText = 5 preparedStatementText;
6		this.arguments = arguments;
7		this.commitTransaction = true;
   }



Listing 5

1		public abstract class SelectStatement 2 extends BaseStatement {
3
4		public SelectStatement(
5      String selectStatementText, List 6 args) {
7
8		super(selectStatementText, args);
9		}
10
11
12		protected ResultSet 
13		executeStatement(
14      		PreparedStatement 
15		preparedStatement) throws Exception {
		
		return 
preparedStatement.executeQuery();
	}
}



Listing 6

1		public DealerVO 
2		findByPrimaryKey(BigDecimal primaryKey)
3				throws DAOException {
4
5		List parameters = new ArrayList();
6		parameters.add(primaryKey);
7
8		SelectStatement selectStatement =
9		new SelectStatement(
10		“Select * from MFS_DEALERS where 
11		DEALER_ID = ?”,
12		parameters) {
13
14		public List parseResultSet(
15            ResultSet resultSet) throws Exception {
16
17
18		List results = new A
19		rrayList();
20
21			if (resultSet.next()) {
22				results.add(
23
24     getDealerVOFromResultSet(resultSet));
25			}
26
27			return results;
28		}
29		};
30		List results = 
31		selectStatement.execute();

	if (results != null && results.size() > 0)
		return (DealerVO) results.get(0);
	else
		return null;
}



Listing 7

1		public class ModifyStatement extends 2	BaseStatement {
3
4		public ModifyStatement(
5      String preparedStatementText, 
6      List arguments) {
7		super(preparedStatementText, 
8		arguments);
9		}
10
11		protected ResultSet 
12		executeStatement(
13      PreparedStatement 
14		preparedStatement)
15			throws Exception {
16
17			preparedStatement.execute();
18
19		return null;
20			}
21
22					protected List 
23		parseResultSet(ResultSet resultSet) 
      throws Exception {
		return null;
	}

}



Listing 8

1	public DealerVO create(DealerVO dealer)
2		throws DAOException {
3
4				String preparedStatementText =
5					"insert into MFS_DEALERS "
6			+ "(DEALER_ID, DEALER_NAME, 
7	DEALER_NUMBER) "
8			+ "values (?, ?, ?)";
9
10				List args = new ArrayList();
11				args.add(dealer.getId());
12				args.add(dealer.getName());
13				args.add(dealer.getNumber());
14
15	ModifyStatement insertStatement = 
16	new
17
18	ModifyStatement(preparedStatementText,
19	args);
20	
				insertStatement.execute();

				return dealer;
		}



		
Listing 9

1	public BaseStatement(
2		Connection connection,
3		String preparedStatementText,
4		List arguments) {
5
6		this.connection = connection;
7		this.preparedStatementText =
8 preparedStatementText;
9		this.arguments = arguments;
10	this.commitTransaction = false;
		}

	
Listing 10

1	public DealerVO create(DealerVO dealer)
2	throws DAOException {
3		return create(dealer, null);
4	}
5
6	public DealerVO create(DealerVO dealer,
7	Connection connection)
8			throws DAOException {
9
10		String preparedStatementText =
11					"insert into MFS_DEALERS "
12		+ "(DEALER_ID, DEALER_NAME,
13	DEALER_NUMBER) "
14		+ "values (?, ?, ?)";
15
16			List args = new ArrayList();
17			args.add(dealer.getId());
18			args.add(dealer.getName());
19			args.add(dealer.getNumber());
20
21			ModifyStatement insertStatement =
22	null;
23
24			if (connection == null)
25				insertStatement = new
26
27	ModifyStatement(preparedStatementText, args); 
				else
					insertStatement =
					new ModifyStatement(
                  connection, preparedStatementText, args);

	insertStatement.execute();

	return dealer;
}




Listing 11

1	public final synchronized List 
2	execute() throws DAOException {
3
4		List results = new ArrayList();
5		PreparedStatement preparedStatement
6	= null;
7		ResultSet resultSet = null;
8
9		try {
10
11		getConnection();
12		preparedStatement = 
13	getPreparedStatement();
14
15		processStatement(preparedStatement)
16	;
17		resultSet = 
18	executeStatement(preparedStatement);
19		results = 
20	parseResultSet(resultSet);
21
22		if (commitTransaction)
23		connection.commit();
24
25		} catch (Exception e) {
26	throw new DAOException(e);
27		} finally {
28
29		// release resources
30		try {
31
32		if (preparedStatement != 
33	null) {
34
35		preparedStatement.close();
36		reparedStatement = 
37	null;
38		}
39		if (resultSet != null) {
40		resultSet.close();
41		resultSet = null;
42	}
43
44			if (commitTransaction) {
45			if (connection != null) 
46	{
47			connection.close();
48			connection = null;

			}
				}

	} catch (SQLException sqle) {
	sqle.printStackTrace();
	}
		}

		return results;

}