Top Description Fields Constructors Methods
org.apache.tools.ant.taskdefs

public abstract Class JDBCTask

extends Task
Class Inheritance
Known Direct Subclasses
org.apache.tools.ant.taskdefs.SQLExec
Imports
java.lang.reflect.InvocationTargetException, java.sql.Connection, .DatabaseMetaData, .Driver, .SQLException, java.util.ArrayList, .Hashtable, .List, .Locale, .Properties, org.apache.tools.ant.AntClassLoader, .BuildException, .Project, .Task, org.apache.tools.ant.types.Path, .Reference

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 ignore) {}
            }
            if (conn != null) {
                try {conn.close();}catch (SQLException ignore) {}
            }
        }
    }
    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

}
Since
Ant 1.5

Field Summary

Modifier and TypeField and Description
private boolean
autocommit

Autocommit flag.

private boolean
private Path
private List<Property>
connectionProperties

Additional properties to put into the JDBC connection string.

private String
driver

DB driver.

private boolean
failOnConnectionError

whether the task fails when ant fails to connect to the database.

private static final int
private AntClassLoader
private static final Hashtable<String, AntClassLoader>
LOADER_MAP

Used for caching loaders / driver.

private String
password

Password

private String
rdbms

RDBMS Product needed for this SQL.

private String
url

DB url.

private String
userId

User name.

private String
version

RDBMS Version needed for this SQL.

Inherited from org.apache.tools.ant.Task:
targettaskNametaskTypewrapper

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
addConnectionProperty(Property
Property
var
)

Additional properties to put into the JDBC connection string.

public Path

Returns:

a path to be configured
createClasspath
()

Add a path to the classpath for loading the driver.

public Path

Returns:

Returns a Path
getClasspath
()

Gets the classpath.

protected Connection

Returns:

Connection the newly created connection or null if the connection failed and failOnConnectionError is false.
getConnection
()

Creates a new Connection as using the driver, url, userid and password specified.

private Driver

Returns:

Driver
getDriver
()

Gets an instance of the required driver.

protected AntClassLoader

Returns:

the classloader
getLoader
()

Get the classloader used to create a driver.

protected static Hashtable<String, AntClassLoader>

Returns:

a hashtable
getLoaderMap
()

Get the cache of loaders and drivers.

public String

Returns:

Returns a String
getPassword
()

Gets the password.

public String

Returns:

Returns a String
getRdbms
()

Gets the rdbms.

public String

Returns:

Returns a String
getUrl
()

Gets the url.

public String

Returns:

Returns a String
getUserId
()

Gets the userId.

public String

Returns:

Returns a String
getVersion
()

Gets the version.

public boolean

Returns:

Returns a boolean
isAutocommit
()

Gets the autocommit.

public void
isCaching(boolean
a boolean value
value
)

Set the caching attribute.

protected boolean

Returns:

true if we are connected to the correct RDBMS
isValidRdbms
(Connection
the jdbc connection
conn
)

Verify we are connected to the correct RDBMS

public void
setAutocommit(boolean
The autocommit to set
autocommit
)

Auto commit flag for database connection; optional, default false.

public void
setCaching(boolean
a boolean value
enable
)

Caching loaders / driver.

public void
setClasspath(Path
The classpath to set
classpath
)

Sets the classpath for loading the driver.

public void
setClasspathRef(Reference
a reference to a classpath
r
)

Set the classpath for loading the driver using the classpath reference.

public void
setDriver(String
The driver to set
driver
)

Class name of the JDBC driver; required.

public void
setFailOnConnectionError(boolean
boolean
b
)

whether the task should cause the build to fail if it cannot connect to the database.

public void
setPassword(String
The password to set
password
)

Sets the password; required.

public void
setRdbms(String
The rdbms to set
rdbms
)

Execute task only if the lower case product name of the DB matches this

public void
setUrl(String
The url to set
url
)

Sets the database connection URL; required.

public void
setUserid(String
The userId to set
userId
)

Set the user name for the connection; required.

public void
setVersion(String
The version to set
version
)

Sets the version string, execute task only if rdbms version match; optional.

Inherited from org.apache.tools.ant.Task:
bindToOwnerexecutegetOwningTargetgetRuntimeConfigurableWrappergetTaskNamegetTaskTypegetWrapperhandleErrorFlushhandleErrorOutputhandleFlushhandleInputhandleOutputinitisInvalidloglogloglogmaybeConfigureperformreconfiguresetOwningTargetsetRuntimeConfigurableWrappersetTaskNamesetTaskType

Field Detail

autocommitback to summary
private boolean autocommit

Autocommit flag. Default value is false

cachingback to summary
private boolean caching
classpathback to summary
private Path classpath
connectionPropertiesback to summary
private List<Property> connectionProperties

Additional properties to put into the JDBC connection string.

Since
Ant 1.8.0
driverback to summary
private String driver

DB driver.

failOnConnectionErrorback to summary
private boolean failOnConnectionError

whether the task fails when ant fails to connect to the database.

Since
Ant 1.8.0
HASH_TABLE_SIZEback to summary
private static final int HASH_TABLE_SIZE
loaderback to summary
private AntClassLoader loader
LOADER_MAPback to summary
private static final Hashtable<String, AntClassLoader> LOADER_MAP

Used for caching loaders / driver. This is to avoid getting an OutOfMemoryError when calling this task multiple times in a row.

passwordback to summary
private String password

Password

rdbmsback to summary
private String rdbms

RDBMS Product needed for this SQL.

urlback to summary
private String url

DB url.

userIdback to summary
private String userId

User name.

versionback to summary
private String version

RDBMS Version needed for this SQL.

Constructor Detail

JDBCTaskback to summary
public JDBCTask()

Method Detail

addConnectionPropertyback to summary
public void addConnectionProperty(Property var)

Additional properties to put into the JDBC connection string.

Parameters
var:Property

Property

Since
Ant 1.8.0
createClasspathback to summary
public Path createClasspath()

Add a path to the classpath for loading the driver.

Returns:Path

a path to be configured

getClasspathback to summary
public Path getClasspath()

Gets the classpath.

Returns:Path

Returns a Path

getConnectionback to summary
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

Connection the newly created connection or null if the connection failed and failOnConnectionError is false.

Exceptions
BuildException:
if the UserId/Password/Url is not set or there is no suitable driver or the driver fails to load.
getDriverback to summary
private Driver getDriver() throws BuildException

Gets an instance of the required driver. Uses the ant class loader and the optionally the provided classpath.

Returns:Driver

Driver

Exceptions
BuildException:
if something goes wrong
getLoaderback to summary
protected AntClassLoader getLoader()

Get the classloader used to create a driver.

Returns:AntClassLoader

the classloader

getLoaderMapback to summary
protected static Hashtable<String, AntClassLoader> getLoaderMap()

Get the cache of loaders and drivers.

Returns:Hashtable<String, AntClassLoader>

a hashtable

getPasswordback to summary
public String getPassword()

Gets the password.

Returns:String

Returns a String

getRdbmsback to summary
public String getRdbms()

Gets the rdbms.

Returns:String

Returns a String

getUrlback to summary
public String getUrl()

Gets the url.

Returns:String

Returns a String

getUserIdback to summary
public String getUserId()

Gets the userId.

Returns:String

Returns a String

getVersionback to summary
public String getVersion()

Gets the version.

Returns:String

Returns a String

isAutocommitback to summary
public boolean isAutocommit()

Gets the autocommit.

Returns:boolean

Returns a boolean

isCachingback to summary
public void isCaching(boolean value)

Set the caching attribute.

Parameters
value:boolean

a boolean value

isValidRdbmsback to summary
protected boolean isValidRdbms(Connection conn)

Verify we are connected to the correct RDBMS

Parameters
conn:Connection

the jdbc connection

Returns:boolean

true if we are connected to the correct RDBMS

setAutocommitback to summary
public void setAutocommit(boolean autocommit)

Auto commit flag for database connection; optional, default false.

Parameters
autocommit:boolean

The autocommit to set

setCachingback to summary
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:boolean

a boolean value

setClasspathback to summary
public void setClasspath(Path classpath)

Sets the classpath for loading the driver.

Parameters
classpath:Path

The classpath to set

setClasspathRefback to summary
public void setClasspathRef(Reference r)

Set the classpath for loading the driver using the classpath reference.

Parameters
r:Reference

a reference to a classpath

setDriverback to summary
public void setDriver(String driver)

Class name of the JDBC driver; required.

Parameters
driver:String

The driver to set

setFailOnConnectionErrorback to summary
public void setFailOnConnectionError(boolean b)

whether the task should cause the build to fail if it cannot connect to the database.

Parameters
b:boolean

boolean

Since
Ant 1.8.0
setPasswordback to summary
public void setPassword(String password)

Sets the password; required.

Parameters
password:String

The password to set

setRdbmsback to summary
public void setRdbms(String rdbms)

Execute task only if the lower case product name of the DB matches this

Parameters
rdbms:String

The rdbms to set

setUrlback to summary
public void setUrl(String url)

Sets the database connection URL; required.

Parameters
url:String

The url to set

setUseridback to summary
public void setUserid(String userId)

Set the user name for the connection; required.

Parameters
userId:String

The userId to set

setVersionback to summary
public void setVersion(String version)

Sets the version string, execute task only if rdbms version match; optional.

Parameters
version:String

The version to set