com.cometway.jdbc
Class JDBCAgent

java.lang.Object
  extended by com.cometway.props.Props
      extended by com.cometway.ak.Agent
          extended by com.cometway.jdbc.JDBCAgent
All Implemented Interfaces:
AgentInterface, HasStateMachineModel
Direct Known Subclasses:
JDBCCreateTableAgent, JDBCDropTableAgent, JDBCExecutive, JDBCServiceAgent, PooledJDBCAgent

public class JDBCAgent
extends Agent

The JDBCAgent manages a JDBC connection and performs simple database tasks such as creating and dropping tables, and executing queries, inserts and updates.


Field Summary
 
Fields inherited from class com.cometway.ak.Agent
agent_id, agentController, CREATING_STATE, debugReporter, DESTROYED_STATE, DESTROYING_STATE, errorReporter, FAILED_STATE, printlnReporter, props, RUNNING_STATE, STARTING_STATE, STOPPED_STATE, STOPPING_STATE, warningReporter
 
Constructor Summary
JDBCAgent()
           
 
Method Summary
 void closeConnection(java.sql.Connection connection)
          Properly closes the specified Connection.
 boolean createTable(java.lang.String tableName, java.util.List fieldList, java.util.List typeList)
          Creates the specified table using the specified parameters.
 boolean dropTable(java.lang.String tableName)
          Drops the specified table from the databsae.
 void error(java.lang.String message, java.lang.Exception e)
          Overrided to fully report SQLExceptions.
 boolean executeInsert(java.lang.String tableName, Props p)
          Inserts the specified Props values into a table.
 java.util.Vector executeQuery(java.lang.String sql)
          Executes the specified SQL query, returning the result as a Vector of Props.
 java.util.Vector executeQuery(java.lang.String tableName, java.lang.String uniqueKey, java.lang.String uniqueValue)
          Executes a search query based on a unique key and value, returning the result as a Vector of Props.
 boolean executeUpdate(java.lang.String sql)
          Executes the specified update SQL, returning true if the update succeeded; false otherwise.
 boolean executeUpdate(java.lang.String tableName, Props p, java.lang.String uniqueKey, java.lang.String uniqueValue)
          Updates the specified Props into the record specified by uniqueKey and uniqueValue.
 java.sql.Connection getConnection()
          Creates a new connection to the database using the current JDBC related settings.
 java.lang.String getInsertPropsSQL(java.lang.String tableName, Props p)
          Generates appropriate SQL for inserting the specified Props into a table.
 void initProps()
          Initializes the Props for this agent: "jdbc_driver" is the classname of the JDBC driver to use (default: sun.jdbc.odbc.JdbcOdbcDriver), "jdbc_url" is the JDBC URL of the database (ie: jdbc:odbc:mydb), "jdbc_username" is the database login username (default: none), "jdbc_password" is the database login password (default: none), "service_name" is the service_name used to register this agent if it is to be used as a service (default: none).
 void start()
          Starts this agent by
 void stop()
          Override this method to handle stop requests from the agent kernel.
 
Methods inherited from class com.cometway.ak.Agent
currentStateEquals, debug, destroy, error, getAgentController, getDateTimeStr, getProps, getServiceImpl, getStateMachineModel, println, register, registerService, setAgentController, setProps, toString, unregister, unregisterService, warning, warning
 
Methods inherited from class com.cometway.props.Props
addListener, append, copy, copyFrom, copyTo, decrementInteger, dump, enableListeners, enumerateKeys, format, getBoolean, getByteArray, getCharacter, getDate, getDate, getDateString, getDouble, getFloat, getHexString, getInteger, getKeys, getLong, getNextKey, getProperty, getProperty, getPropsContainer, getSize, getString, getString, getTokens, getTokens, getTrimmedString, getVector, hasProperty, incrementInteger, insertProps, loadProps, notifyPropsChanged, notifyPropsChangedIgnore, notifyPropsChangedIgnore, parse, propertyMatchesRegEx, readFromFile, regExPropertyMatches, removeAll, removeListener, removeProperty, resumeNotify, saveProps, set, setBoolean, setCharacter, setDefault, setDouble, setFloat, setInteger, setLong, setProperty, setProperty, setProperty, setPropsContainer, suspendNotify, writeToFile
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

JDBCAgent

public JDBCAgent()
Method Detail

initProps

public void initProps()
Initializes the Props for this agent: "jdbc_driver" is the classname of the JDBC driver to use (default: sun.jdbc.odbc.JdbcOdbcDriver), "jdbc_url" is the JDBC URL of the database (ie: jdbc:odbc:mydb), "jdbc_username" is the database login username (default: none), "jdbc_password" is the database login password (default: none), "service_name" is the service_name used to register this agent if it is to be used as a service (default: none).

Specified by:
initProps in interface AgentInterface
Overrides:
initProps in class Agent

start

public void start()
Starts this agent by

Specified by:
start in interface AgentInterface
Overrides:
start in class Agent

stop

public void stop()
Description copied from class: Agent
Override this method to handle stop requests from the agent kernel.

Specified by:
stop in interface AgentInterface
Overrides:
stop in class Agent

closeConnection

public void closeConnection(java.sql.Connection connection)
Properly closes the specified Connection. All Connections issued by getConnection should be closed using this method with they are no longer needed.


createTable

public boolean createTable(java.lang.String tableName,
                           java.util.List fieldList,
                           java.util.List typeList)
Creates the specified table using the specified parameters. Elements in fieldList and typeList are Strings. The type is a string similar to "varchar (5) NOT NULL" part of a sql CREATE statement. Remember to quote table and field names appropriately for your SQL server.


dropTable

public boolean dropTable(java.lang.String tableName)
Drops the specified table from the databsae.


error

public void error(java.lang.String message,
                  java.lang.Exception e)
Overrided to fully report SQLExceptions.

Specified by:
error in interface AgentInterface
Overrides:
error in class Agent

executeInsert

public boolean executeInsert(java.lang.String tableName,
                             Props p)
Inserts the specified Props values into a table. Returns true if successful; false otherwise.


executeQuery

public java.util.Vector executeQuery(java.lang.String sql)
Executes the specified SQL query, returning the result as a Vector of Props.


executeQuery

public java.util.Vector executeQuery(java.lang.String tableName,
                                     java.lang.String uniqueKey,
                                     java.lang.String uniqueValue)
Executes a search query based on a unique key and value, returning the result as a Vector of Props.


executeUpdate

public boolean executeUpdate(java.lang.String sql)
Executes the specified update SQL, returning true if the update succeeded; false otherwise.


executeUpdate

public boolean executeUpdate(java.lang.String tableName,
                             Props p,
                             java.lang.String uniqueKey,
                             java.lang.String uniqueValue)
Updates the specified Props into the record specified by uniqueKey and uniqueValue. Returns true if successful; false otherwise.


getInsertPropsSQL

public java.lang.String getInsertPropsSQL(java.lang.String tableName,
                                          Props p)
Generates appropriate SQL for inserting the specified Props into a table.


getConnection

public java.sql.Connection getConnection()
                                  throws java.sql.SQLException
Creates a new connection to the database using the current JDBC related settings.

Throws:
java.sql.SQLException