1. Packages
-----------
com.anjib.action (Business Component also known as client.)
com.anjib.dao (Usually intterfaces to access the tables.)
com.anjib.impl (Implementation for the DAOs)
com.anjib.to (Transfer objects)
2. Transfer Object Example
--------------------------
public class Agency implements Serializable {
private Integer agencyId;
private String agencyCode;
private String agencyName;
public String getAgencyCode() {
return agencyCode;
}
public void setAgencyCode(String agencyCode) {
this.agencyCode = agencyCode;
}
public Integer getAgencyId() {
return agencyId;
}
public void setAgencyId(Integer agencyId) {
this.agencyId = agencyId;
}
public String getAgencyName() {
return agencyName;
}
public void setAgencyName(String agencyName) {
this.agencyName = agencyName;
}
}
3. DAOs
-------
public class AgencyDAO {
private static Log log = LogFactory.getLog(AgencyDAO.class.getName());
private DataSource dataSource;
public AgencyImpl throws DAOException {
try{
this.dataSource = ServiceLocator.getInstance().getDataSource("jdbc/anjibdb");
}catch (Exception ex) {
log.error ("Error in looking up JNDI name for service locator", ex);
}
}
public Agency findAgency(Integer agencyId) throws DAOException {
Connection conn = dataSource.getConnection();
Agency agency = null;
PreparedStatement prepStmt = null;
ResultSet rs = null;
try{
String sql = "SELECT agency_id, agency_code, agency_name FROM agency WHERE agency_id = ?";
prepStmt = conn.prepareStatement(sql);
prepStmt.setInt(1, agencyId);
rs.prepStmt.executeQuery();
if(rs.next()){
agency = new Agency();
agency.setAgencyId(rs.getInteger("agency_id"));
agency.setAgencyCode(rs.getString("agency_code"));
agency.setAgencyName(rs.getString("agency_name"));
}
} catch (Exception ex) {
log.error(ex);
} finally {
conn.close();
}
return agency;
}
}
4. Service locator class
------------------------
public class ServiceLocator {
private InitialContext initialContext;
private Map cache;
private static ServiceLocator serviceLocatorInstance = new ServiceLocator();
private ServiceLocator() throws ServiceLocatorException {
try{
this.initialContext = new InitialContext();
this.cache = Collections.synchronizedMap(new HashMap());
} catch (NamingException nex) {
log.error (nex);
}
}
public static ServiceLocator getInstance() {
return serviceLocatorInstance;
}
public DataSource getdataSource(String dataSourceName) {
DataSource dataSource = null;
try {
if(this.cache.containsKey(dataSourceName)) {
dataSource = this.cache.get(dataSourceName);
} else {
Context envContext = (Context) initialContext.lookup("java:comp/env");
dataSource = (DataSource) envContext.lookup(dataSourceName);
this.cache.put(datSourceName, dataSource);
}
} catch (NamingException ex) {
log.error("Error in CTX lookup");
}
return dataSource;
}
}
5. Define JNDI connection to DB in servelt container (web.xml)
--------------------------------------------------------------
6. Bind the DataSource to the JNDI context (context.xml)
--------------------------------------------------------
driverClassName="oracle.jdbc.driver.OracleDriver"
maxActive="20"
maxIdle="10"
maxWait="-1"
name="jdbc/anjibdb"
password="password"
type="javax.sql.DataSource"
url="jdbc:oracle:thin:@//localhost:4001/ANJIB"
username="username"/>
No comments:
Post a Comment