Class JDBCTask
java.lang.Object
org.apache.tools.ant.ProjectComponent
org.apache.tools.ant.Task
org.apache.tools.ant.taskdefs.JDBCTask
- All Implemented Interfaces:
Cloneable
- Direct Known Subclasses:
SQLExec
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
Fields inherited from class ProjectComponent
description, location, project -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAdditional properties to put into the JDBC connection string.Add a path to the classpath for loading the driver.Gets the classpath.protected ConnectionCreates a new Connection as using the driver, url, userid and password specified.protected AntClassLoaderGet the classloader used to create a driver.protected static Hashtable<String, AntClassLoader> Get the cache of loaders and drivers.Gets the password.getRdbms()Gets the rdbms.getUrl()Gets the url.Gets the userId.Gets the version.booleanGets the autocommit.voidisCaching(boolean value) Set the caching attribute.protected booleanisValidRdbms(Connection conn) Verify we are connected to the correct RDBMSvoidsetAutocommit(boolean autocommit) Auto commit flag for database connection; optional, default false.voidsetCaching(boolean enable) Caching loaders / driver.voidsetClasspath(Path classpath) Sets the classpath for loading the driver.voidSet the classpath for loading the driver using the classpath reference.voidClass name of the JDBC driver; required.voidsetFailOnConnectionError(boolean b) whether the task should cause the build to fail if it cannot connect to the database.voidsetPassword(String password) Sets the password; required.voidExecute task only if the lower case product name of the DB matches thisvoidSets the database connection URL; required.voidSet the user name for the connection; required.voidsetVersion(String version) Sets the version string, execute task only if rdbms version match; optional.Methods inherited from class 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, setTaskTypeModifier and TypeMethodDescriptionfinal voidbindToOwner(Task owner) Bind a task to another; use this when configuring a newly created task to do work on behalf of another.voidexecute()Called by the project to let the task do its work.Returns the container target of this task.Returns the wrapper used for runtime configuration.Returns the name to use in logging messages.Return the type of task.protected RuntimeConfigurableReturn the runtime configurable structure for this task.protected voidhandleErrorFlush(String output) Handles an error line by logging it with the WARN priority.protected voidhandleErrorOutput(String output) Handles an error output by logging it with the WARN priority.protected voidhandleFlush(String output) Handles output by logging it with the INFO priority.protected inthandleInput(byte[] buffer, int offset, int length) Handle an input request by this task.protected voidhandleOutput(String output) Handles output by logging it with the INFO priority.voidinit()Called by the project to let the task initialize properly.protected final booleanHas this task been marked invalid?voidLogs a message with the default (INFO) priority.voidLogs a message with the given priority.voidLogs a message with the given priority.voidLogs a message with the given priority.voidConfigures this task - if it hasn't been done already.final voidperform()Performs this task if it's still valid, or gets a replacement version and performs that otherwise.voidForce the task to be reconfigured from its RuntimeConfigurable.voidsetOwningTarget(Target target) Sets the target container of this task.voidSets the wrapper to be used for runtime configuration.voidsetTaskName(String name) Sets the name to use in logging messages.voidsetTaskType(String type) Sets the name with which the task has been invoked.Methods inherited from class ProjectComponent
clone, getDescription, getLocation, getProject, setDescription, setLocation, setProjectModifier and TypeMethodDescriptionclone()Returns the description of the current action.Returns the file/location where this task was defined.Returns the project to which this component belongs.voidsetDescription(String desc) Sets a description of the current action.voidsetLocation(Location location) Sets the file/location where this task was defined.voidsetProject(Project project) Sets the project object of this component.
-
Constructor Details
-
JDBCTask
public JDBCTask()
-
-
Method Details
-
setClasspath
Sets the classpath for loading the driver.- Parameters:
classpath- The classpath 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- abooleanvalue
-
createClasspath
Add a path to the classpath for loading the driver.- Returns:
- a path to be configured
-
setClasspathRef
Set the classpath for loading the driver using the classpath reference.- Parameters:
r- a reference to a classpath
-
setDriver
Class name of the JDBC driver; required.- Parameters:
driver- The driver to set
-
setUrl
Sets the database connection URL; required.- Parameters:
url- The url to set
-
setPassword
Sets the password; required.- Parameters:
password- The password to set
-
setAutocommit
public void setAutocommit(boolean autocommit) Auto commit flag for database connection; optional, default false.- Parameters:
autocommit- The autocommit to set
-
setRdbms
Execute task only if the lower case product name of the DB matches this- Parameters:
rdbms- The rdbms to set
-
setVersion
Sets the version string, execute task only if rdbms version match; optional.- Parameters:
version- The version to set
-
setFailOnConnectionError
public void setFailOnConnectionError(boolean b) whether the task should cause the build to fail if it cannot connect to the database.- Parameters:
b- boolean- Since:
- Ant 1.8.0
-
isValidRdbms
Verify we are connected to the correct RDBMS- Parameters:
conn- the jdbc connection- Returns:
- true if we are connected to the correct RDBMS
-
getLoaderMap
Get the cache of loaders and drivers.- Returns:
- a hashtable
-
getLoader
Get the classloader used to create a driver.- Returns:
- the classloader
-
addConnectionProperty
Additional properties to put into the JDBC connection string.- Parameters:
var- Property- Since:
- Ant 1.8.0
-
getConnection
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 or null if the connection failed and failOnConnectionError is false.
- Throws:
BuildException- if the UserId/Password/Url is not set or there is no suitable driver or the driver fails to load.
-
isCaching
public void isCaching(boolean value) Set the caching attribute.- Parameters:
value- abooleanvalue
-
getClasspath
-
isAutocommit
public boolean isAutocommit()Gets the autocommit.- Returns:
- Returns a boolean
-
getUrl
-
getUserId
-
setUserid
Set the user name for the connection; required.- Parameters:
userId- The userId to set
-
getPassword
-
getRdbms
-
getVersion
-