Using Apache Beehive JDBC Control with Tomcat web server
and batchUpdate=true:

Recently I have tried Apache Beehive Control and JdbcControl
with a web application, deployable to Tomcat web server.

1. Create a workspace and create appropriate Java files.
such as 

Sample.java
----------------------------------------------------------------
package demo;
import org.apache.beehive.controls.api.bean.ControlInterface;
//control interface
@ControlInterface
public interface Sample  
{
    //test method
    public void test();
    public void insertUsers();
}
-----------------------------------------------------------------

Implementing the interface "Sample" and using database control.

SampleImpl.java
------------------------------------------------------------------
package demo;
import org.apache.beehive.controls.api.bean.Control;
import org.apache.beehive.controls.api.bean.ControlImplementation;
import java.sql.SQLException;

//control implementation
@ControlImplementation
public class SampleImpl implements Sample, java.io.Serializable
{
    //database control private variable
    @Control
    private SampleDBControl dbControl;

    public void test() {
        try{
            System.out.println("Name :" + dbControl.fetchName());
        } catch(SQLException ex) {
            ex.printStackTrace();
        }
    }
    public void insertUsers() {
        try{
            String[] id = {"1", "2", "3"};
            String[] name = {"my_name1", "my_name1", "my_name1"};
            dbControl.createUsers(id, name);
        } catch(SQLException ex) {
            ex.printStackTrace();
        }
    }
}
------------------------------------------------------------------


Database control should be interface and it should extend JdbcControl
interface, and it should be annotated as ControlExtension.

SampleDBControl.java
------------------------------------------------------------------
package demo;
import org.apache.beehive.controls.system.jdbc.JdbcControl;
import org.apache.beehive.controls.system.jdbc.JdbcControl.SQL;
import java.sql.SQLException;
import org.apache.beehive.controls.api.bean.ControlExtension;

@ControlExtension
@JdbcControl.ConnectionDriver(databaseDriverClass="org.gjt.mm.mysql.Driver",
                              databaseURL="jdbc:mysql://localhost:3306/test",
                              userName="<username>",
                              password="<password>")
public interface SampleDBControl extends JdbcControl
{
    @SQL(statement="select name from user")
    public String fetchName() throws SQLException;
    @SQL(batchUpdate=true, 
         statement="insert into user(id, name) values({id},{name})")
    public int[] createUsers(String[] id, String[] name) throws SQLException;
}
------------------------------------------------------------------

As these Java files are having packages as demo.
There should be a external folder "ext" and one should require
at least following Jar files:
1. beehive-controls.jar  (Beehive common control Java file)
2. beehive-jdbc-control.jar  (Beehive JDBC Control Java file)
3. commons-discovery-0.2.jar  
4. commons-logging-1.0.4.jar  (Apache Commons Logging Jar file)
5. log4j-1.2.8.jar
6. mysql-connector-java-5.0.4-bin.jar (MySQL JDBC driver Jar file)

------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">

  <display-name>My Example</display-name>
    
  <filter>
    <filter-name>ControlFilter</filter-name>
    <filter-class>
    org.apache.beehive.controls.runtime.servlet.ControlFilter
    </filter-class>
  </filter>

  <filter-mapping>
    <filter-name>ControlFilter</filter-name>
    <url-pattern>*.jsp</url-pattern>
  </filter-mapping>

  <welcome-file-list>
    <welcome-file>sample.jsp</welcome-file>
  </welcome-file-list>

</web-app>
------------------------------------------------------------------

Front end is a browser based, following JSP file as follows:
------------------------------------------------------------------
<%@ page import="org.apache.beehive.controls.api.bean.Controls,
  org.apache.beehive.controls.api.context.ControlThreadContext,
  demo.SampleBean" %>
<%
    SampleBean sampleBean = Controls.instantiate(SampleBean.class,
                            null,
                            ControlThreadContext.getContext(),null);
    sampleBean.insertUsers();
%>
------------------------------------------------------------------

The build properties file has the path of the Beehive Home folder.
build.properties:
------------------------------------------------------------------
beehive.home=../..
------------------------------------------------------------------

and build.xml :
------------------------------------------------------------------
........
........
<path id="webapp.classpath.controls">
    <fileset dir="${build}/WEB-INF/lib"/>
    <fileset dir="${build}/WEB-INF/classes"/>
    <path refid="controls.dependency.path"/>
    <path refid="webapp.classpath"/>
    <path refid="velocity.dependency.path"/>
    <path refid="apt.task.classpath"/>
</path>

<taskdef name="apt" 
    classname="org.apache.beehive.controls.runtime.generator.AptTask"
    classpathref="apt.task.classpath" onerror="fail"/>
<apt sourcepath="src" 
     srcdir="src" 
     destdir="${build}/WEB-INF/classes" 
     gendir="${build}/tmpbeansrc" 
     classpathref="webapp.classpath.controls" 
     nocompile="false"/>
<javac srcdir="${build}/tmpbeansrc"
       destdir="${build}/WEB-INF/classes"
       classpathref="webapp.classpath.controls" 
       debug="on"/>
........
........
------------------------------------------------------------------

You can find help in the samples folder of Apache Beehive for
completing above build.xml file.

Above mentioned section are the ones I would like to emphasize
on. Firstly setting path or setting up classpath, secondly using
<apt> tag for generating required Beehive controls supporting
files. These includes Java files and Manifest files.
And then using <javac> tag for compiling these Java files.

Once all the files are set in build folder as per the WAR file
structure, then deploy this WAR file on Tomcat web server.

If this WAR file is deployed successfully, then access the desired
URL , for me it is http://localhost:8080/Sample/sample.jsp,
provided the WAR file name is "Sample".

After execution , one can find those three records inserted into user
table, and JdbcControl uses Batch Update , that is JDBC Batch 
DML query execution.

One thing one has to remember is to access respective Bean, like for
example , in my case if Sample is the  interface, then the bean that
is auto generated by Beehive APT tag is SampleBean.

Conclusion:
It is a very easy to understand topic, but still if any Developer wants to
contact me for any clarification, you are welcome, please drop me an email
at usingframeworks at gmail dot com.

References:
1. http://beehive.apache.org

Copyright © 2008, DeveloperKnowHow-Articles, All Rights Reserved.