|
OOP SimpleDaoHelper Version 1.2 | ||||||||
| PREV PACKAGE NEXT PACKAGE | FRAMES NO FRAMES | ||||||||
See:
Description
| Interface Summary | |
|---|---|
| SimpleDaoHelper | Given the QueryData object, this class creates
the PreparedStatement object and executes it. |
| Class Summary | |
|---|---|
| DaoHelperFactory | Given the fully qualified class name, this class creates
the instance of SimpleDaoHelper interface. |
| DaoHelperForJdbc | Implementation of SimpleDaoHelper interface. |
| DaoHelperForJndi | Implementation of SimpleDaoHelper interface. |
| DaoHelperForPooledStatement | Implementation of SimpleDaoHelper interface. |
| JndiLocator | Service Locator. |
| QueryData | To set the values within the PreparedStatement object, please add the intended values to this object. |
| SimpleDaoHelperImpl | The abstract super class for the implementation of
SimpleDaoHelper interface. |
The Java API for making DAO (Data Access Object) simpler.
The main interface of this Java API is SimpleDaoHelper. Given the QueryData object, the implementation of SimpleDaoHelper interface:
| Column Name | SQL Type | Java Type |
|---|---|---|
| id | DECIMAL(3,0) | int |
| name | VARCHAR(255) | String |
| age | DECIMAL(3,0) | int |
// Let's try DaoHelperForJndi, which creates
// PreparedStatement from JNDI DataSource.
// JNDI DataSource name must be available as the
// environment valuable.
String className="com.oopreserch.dao.DaoHelperForJndi";
SimpleDaoHelper helper=DaoHelperFactory.getDaoHelper(className);
// Please specify your SQL statement here...
String insert="INSERT INTO example (id,name,age) VALUES(?,?,?)";
QueryData query=new QueryData(insert);
// Specify the values to be set on the SQL statement.
// The order is important!
query.addInt(50);
query.addString("John Dow");
query.addInt(30);
// Its time to execute your SQL statement...
int result=helper.update(query);
In case of SELECT, the array of return types must also be specified.
If you'd like to find the id and name for the person older than 20:
// Let's try DaoHelperForJndi, which creates
// PreparedStatement from JNDI DataSource.
// JNDI DataSource name must be available as the
// environment valuable.
String className="com.oopreserch.dao.DaoHelperForJndi";
SimpleDaoHelper helper=DaoHelperFactory.getDaoHelper(className);
// Please specify your SQL statement here...
String select="SELECT id,name FROM example WHERE age > ?";
QueryData query=new QueryData(select);
// Specify the value to be set on the SQL statement.
query.addInt(20);
// Please specify the return type(s) as the int array.
// This int array must consist of the pre-defined int
// constants...
// The order is important!
int[] types={QueryData.INT,QueryData.STRING};
// Its time to execute your SQL statement...
List list=helper.select(query,types);
// The returned List includes the selected rows.
// You can iterate each row...
Iterator it=list.iterator();
while(it.hasNext()){
// Each object in the returned List is also the List.
// And this nested List object represents each row...
List row=(List)(it.next());
// Get the first column...
int id=(Integer)(row.get(0)).intValue();
// Get the second column...
String name=(String)(row.get(1));
}
In the above examples, the fully qualified class name of SimpleDaoHelper interface and the SQL statements are hard-coded within the source code.
But, in the production code, all these Strings should be read from some external resource, such as the deployment descriptor or property resource file.
InitialContext ic=new InitialContext();
String className=(String)(ic.lookup("java:comp/env/ejb/example/DaoHelperClass"));
String insert=(String)(ic.lookup("java:comp/env/ejb/example/Insert"));
String select=(String)(ic.lookup("java:comp/env/ejb/example/SelectByAge"));
By this way, your code can be independent from the SQL table name and its column names.
// Get the instance of JndiLocator
JndiLocator locator=JndiLocator.getInstance();
// Look up the environment values...
String className=locator.getStringEnv("java:comp/env/ejb/example/DaoHelperClass");
String insert=locator.getStringEnv("java:comp/env/ejb/example/Insert");
String select=locator.getStringEnv("java:comp/env/ejb/example/SelectByAge");
Let's look into more practical example. Assuming that you are going to implement some very simple DAO (Data Access Object), your source code will look like this:
public class ExampleDaoforOracle
implements ExampleDao{
private SimpleDaoHelper helper;
private String insert;
private String select;
public ExampleDaoforOracle(){
helper=DaoHelperFactory.getDaoHelper(JNDINames.HELPER_CLASS);
JndiLocator locator=JndiLocator.getInstance();
insert=locator.getStringEnv(JNDINames.INSERT);
select=locator.getStringEnv(JNDINames.SELECT);
}
public void insert(ExampleValue value){
QueryData query=new QueryData(insert);
query.addInt(value.getId());
query.addString(value.getName());
helper.update(query);
}
public ExampleValue select(int id){
QueryData query=new QueryData(select);
query.addInt(id);
int[] types={QueryData.INT,QueryData.STRING};
List list=helper.select(query,types);
Iterator it=list.iterator();
ExampleValue value=null;
if(it.hasNext()){
List row=(List)(it.next());
value=new ExampleValue();
value.setId((Integer)(row.get(0)).intValue());
value.setSubject((String)(row.get(1)));
}
return value;
}
}
As you see, the source code of your DAO (Data Access Object) can be free from JNDI/JDBC related stuff and be very simple.
|
OOP SimpleDaoHelper Version 1.2 | ||||||||
| PREV PACKAGE NEXT PACKAGE | FRAMES NO FRAMES | ||||||||
ALL CONTENTS COPYRIGHT 2005, OOP-Research Corporation. All rights reserved.
Any questions and comments are welcome to OOP-Research Corporation.