org.apache.tools.ant.taskdefs

Class JDBCTask

Implemented Interfaces:
Cloneable
Known Direct Subclasses:
SQLExec

public abstract class JDBCTask
extends Task

Handles JDBC configuration needed by SQL type tasks.

The following example class prints the contents of the first column of each row in TableName.

package examples;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.taskdefs.JDBCTask;
public class SQLExampleTask extends JDBCTask {
private String tableName;
public void execute() throws BuildException {
Connection conn = getConnection();
Statement stmt=null;
try {
if (tableName == null) {
throw new BuildException("TableName must be specified",location);
}
String sql = "SELECT * FROM "+tableName;
stmt= conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
log(rs.getObject(1).toString());
}
} catch (SQLException e) {
} finally {
if (stmt != null) {
try {stmt.close();}catch (SQLException ingore) {}
}
if (conn != null) {
try {conn.close();}catch (SQLException ingore) {}
}
}
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
}
Since:
Ant 1.5

Field Summary

Fields inherited from class org.apache.tools.ant.Task

target, taskName, taskType, wrapper

Fields inherited from class org.apache.tools.ant.ProjectComponent

description, location, project

Method Summary

Path
createClasspath()
Add a path to the classpath for loading the driver.
Path
getClasspath()
Gets the classpath.
protected Connection
getConnection()
Creates a new Connection as using the driver, url, userid and password specified.
protected AntClassLoader
getLoader()
Get the classloader used to create a driver.
protected static Hashtable
getLoaderMap()
Get the cache of loaders and drivers.
String
getPassword()
Gets the password.
String
getRdbms()
Gets the rdbms.
String
getUrl()
Gets the url.
String
getUserId()
Gets the userId.
String
getVersion()
Gets the version.
boolean
isAutocommit()
Gets the autocommit.
void
isCaching(boolean value)
Set the caching attribute.
protected boolean
isValidRdbms(Connection conn)
Verify we are connected to the correct RDBMS
void
setAutocommit(boolean autocommit)
Auto commit flag for database connection; optional, default false.
void
setCaching(boolean enable)
Caching loaders / driver.
void
setClasspath(Path classpath)
Sets the classpath for loading the driver.
void
setClasspathRef(Reference r)
Set the classpath for loading the driver using the classpath reference.
void
setDriver(String driver)
Class name of the JDBC driver; required.
void
setPassword(String password)
Sets the password; required.
void
setRdbms(String rdbms)
Execute task only if the lower case product name of the DB matches this
void
setUrl(String url)
Sets the database connection URL; required.
void
setUserid(String userId)
Set the user name for the connection; required.
void
setVersion(String version)
Sets the version string, execute task only if rdbms version match; optional.

Methods inherited from class org.apache.tools.ant.Task

bindToOwner, execute, getOwningTarget, getRuntimeConfigurableWrapper, getTaskName, getTaskType, getWrapper, handleErrorFlush, handleErrorOutput, handleFlush, handleInput, handleOutput, init, isInvalid, log, log, log, log, maybeConfigure, perform, reconfigure, setOwningTarget, setRuntimeConfigurableWrapper, setTaskName, setTaskType

Methods inherited from class org.apache.tools.ant.ProjectComponent

clone, getDescription, getLocation, getProject, log, log, setDescription, setLocation, setProject

Method Details

createClasspath

public Path createClasspath()
Add a path to the classpath for loading the driver.
Returns:
a path to be configured

getClasspath

public Path getClasspath()
Gets the classpath.
Returns:
Returns a Path

getConnection

protected Connection getConnection()
            throws BuildException
Creates a new Connection as using the driver, url, userid and password specified. The calling method is responsible for closing the connection.
Returns:
Connection the newly created connection.
Throws:
BuildException - if the UserId/Password/Url is not set or there is no suitable driver or the driver fails to load.

getLoader

protected AntClassLoader getLoader()
Get the classloader used to create a driver.
Returns:
the classloader

getLoaderMap

protected static Hashtable getLoaderMap()
Get the cache of loaders and drivers.
Returns:
a hashtable

getPassword

public String getPassword()
Gets the password.
Returns:
Returns a String

getRdbms

public String getRdbms()
Gets the rdbms.
Returns:
Returns a String

getUrl

public String getUrl()
Gets the url.
Returns:
Returns a String

getUserId

public String getUserId()
Gets the userId.
Returns:
Returns a String

getVersion

public String getVersion()
Gets the version.
Returns:
Returns a String

isAutocommit

public boolean isAutocommit()
Gets the autocommit.
Returns:
Returns a boolean

isCaching

public void isCaching(boolean value)
Set the caching attribute.
Parameters:
value - a boolean value

isValidRdbms

protected boolean isValidRdbms(Connection conn)
Verify we are connected to the correct RDBMS
Parameters:
conn - the jdbc connection
Returns:
true if we are connected to the correct RDBMS

setAutocommit

public void setAutocommit(boolean autocommit)
Auto commit flag for database connection; optional, default false.
Parameters:
autocommit - The autocommit to set

setCaching

public void setCaching(boolean enable)
Caching loaders / driver. This is to avoid getting an OutOfMemoryError when calling this task multiple times in a row; default: true
Parameters:
enable - a boolean value

setClasspath

public void setClasspath(Path classpath)
Sets the classpath for loading the driver.
Parameters:
classpath - The classpath to set

setClasspathRef

public void setClasspathRef(Reference r)
Set the classpath for loading the driver using the classpath reference.
Parameters:
r - a reference to a classpath

setDriver

public void setDriver(String driver)
Class name of the JDBC driver; required.
Parameters:
driver - The driver to set

setPassword

public void setPassword(String password)
Sets the password; required.
Parameters:
password - The password to set

setRdbms

public void setRdbms(String rdbms)
Execute task only if the lower case product name of the DB matches this
Parameters:
rdbms - The rdbms to set

setUrl

public void setUrl(String url)
Sets the database connection URL; required.
Parameters:
url - The url to set

setUserid

public void setUserid(String userId)
Set the user name for the connection; required.
Parameters:
userId - The userId to set

setVersion

public void setVersion(String version)
Sets the version string, execute task only if rdbms version match; optional.
Parameters:
version - The version to set