OOP-ResearchMake It Simpler by Object Oriented Programming

Java API for making your DAO (Data Access Object) simpler: How to use JDBC PreparedStatement and JNDI DataSource

JNDI DataSource from JNDI InitialContext or JDBC Connection, JDBC PreparedStatement is created and executed. Your DAO (Data Access Object) need not include any source code related with JNDI or JDBC.
If JNDI DataSource is available in your environment, this Java API looks up JNDI DataSource on JNDI InitialContext and establishes JDBC Connection. Otherwise, this Java API gets JDBC Connection from JDBC driver. Or, if you use OOP PooledStatement, this Java API can take the advantage of its pooling mechanism. In any case, it creates JDBC PreparedStatement based on the available JDBC Connection.
On the JDBC PreparedStatement, this Java API can insert the arbitrary number of Java objects and values. And it executes the JDBC PreparedStatement.

By this Java API, a few lines of codes are enough for your DAO (Data Access Object). As described above, this Java API creates JDBC PreparedStatement, by the way suitable for your environment. This means you can make your Web Applications more portable. Please download and try this Java API. It's FREE!

Related Pages:


What is it?

This is 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:

  • Creates the PreparedStatement object
  • Sets the specified values on the PreparedStatement object
  • Executes the PreparedStatement object
On the QueryData object, you can specify:
  • The arbitrary SQL statement
  • A series of data to be inserted into the SQL statement
How to create PreparedStatement depends on the implementation. At this time, the following 3 implementations are available: As for the first one, the JNDI DataSource must be found by:
  • java:comp/env/jdbc/DaoHelperDataSource
As for the rest of them, the JDBC driver name and the related information must be written in the property resource file. For the details, please look into README.html of this API. All these implementations are packaged under com.oopreserch.dao package.
Given the fully qualified class names, DaoHelperFactory returns the intended implementation of SimpleDaoHelper interface.

Go Site Map

Easy Programming Interface

SimpleDaoHelper interface implements the 2 important methods, one for SELECT and another for INSERT, UPDATE or DELETE. And both of them takes the QueryData object as their parameters. They are:

  • select(QueryData,int[])
  • update(QueryData)
For example, please look into the very simple SQL table below:
Column NameSQL TypeJava Type
idDECIMAL(3,0)int
nameVARCHAR(255)String
ageDECIMAL(3,0)int

If you'd like to insert the following row into the above table:
  • id=50
  • name=John Dow
  • age=30
Your code will look like this:
  // 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.

For example, if the JNDI environment valuables are available in your environment:

  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.

But, as you know, looking up something on the InitialContext again and again will be somewhat resource-consuming task. To avoid this, JndiLocator is included in this package. You can think of it as a kind of ServiceLocator. Once the intended object is looked up, this class keeps its reference in the cache. So, the subsequent call with the same JNDI name can return very quickly, without looking up it on the InitialContext again. For example, the above 4 lines of codes can be:

  // 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");


As you see, you need not to be concerned with JNDI related tasks by yourself, but can get the better performance.

For details, please consult API documentation of OOP Simple DAO Helper.

Go Site Map

Example of DAO (Data Access Object)

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.

Go Site Map

Download

This API is now a part of Bento framework. There are the better documentations and source code examples on the web site for Bento framework. There, you will also find the link to download the entire framework or each API in it.

Caution!
All the APIs for Servlet/JSP introduced by this web site are now included in Bento framework:
  • Simpler than JSTL or Apache Struts
  • MVC framework by HTML
  • Input validation from CGI FORM
  • Easy user authentication
  • Easy localization (L10n)
To download the APIs and source code examples, please visit the web site of Bento framework.


JBuilder 2007


General Information

For Java Development

Java and all Java-based trademarks and logos are trademarks or registered of Sun Microsystems, Inc. in the United States and other countries.


ALL CONTENTS COPYRIGHT 1997-2007, OOP-Research Corporation. All rights reserved.