JDBC Control Tutorial (Apache)
JDBC Control Tutorial (Apache)
Table of contents
1 Overview............................................................................................................................2
1.1 The Problem with JDBC: Complexity.......................................................................... 2
2 Tutorial...............................................................................................................................2
2.1 JDBC Control Tutorial.................................................................................................. 2
2.2 Extending the JDBC-Control Interface......................................................................... 2
2.3 Connecting to a Database Instance................................................................................3
2.4 Making JDBC Calls to a Database Instance..................................................................3
2.5 SQL Parameter Substitution.......................................................................................... 4
1. Overview
2. Tutorial
/**
* JdbcControl implementation for the JdbcControl sample app.
*/
@org.apache.beehive.controls.api.bean.ControlExtension
@JdbcControl.ConnectionDataSource(jndiName="java:comp/env/jdbc/JdbcControlSampleDB")
public interface SimpleDBControl extends JdbcControl {
.
.
.
}
In the sample above several Java 1.5 annotations are used. The @ControlExtension
Page 2
Copyright © 2004 The Apache Software Foundation. All rights reserved.
JDBC Control Tutorial
annotation is required and tells the Beehive control framework that this control extends an
extensible control (in this case the JdbcControl).
/**
* JdbcControl implementation for the JdbcControl sample app.
*/
@org.apache.beehive.controls.api.bean.ControlExtension
@JdbcControl.ConnectionDataSource(jndiName="java:comp/env/jdbc/JdbcControlSampleDB")
public interface SimpleDBControl
extends JdbcControl {
static final long serialVersionUID = 1L;
public static class Product {
private String _name;
private String _description;
private int _quantity;
public int getQuantity() { return _quantity; }
public void setQuantity(int i) { _quantity = i; }
public String getName() { return _name; }
public void setName(String n) { _name = n; }
Page 3
Copyright © 2004 The Apache Software Foundation. All rights reserved.
JDBC Control Tutorial
Note the use of the @SQL method annotation in SimpleDBControl.java, see the JdbcControl
Annotation Reference for additional information about the SQL annotation.
//
// simple query with param substitution
//
@SQL(statement="SELECT * FROM USERS WHERE userid={someUserId}")
public ResultSet getSomeUser(int someUserId) throws SQLException;
Page 4
Copyright © 2004 The Apache Software Foundation. All rights reserved.
JDBC Control Tutorial
//
// query with sql substitution
//
@SQL(statement="SELECT * FROM USERS WHERE {sql: where}")
public ResultSet getJustOneUser(String where) throws SQLException;
For the first method, the value of the parameter 'someUserId' gets substituted into the SQL
statement at runtime when the getSomeUser() method is invoked. For the second method, the
substitution gets added to the SQL statement as the literal value of the 'where' parameter.
Page 5
Copyright © 2004 The Apache Software Foundation. All rights reserved.