Package org.apache.tools.ant.taskdefs
Class JDBCTask
- java.lang.Object
- 
- org.apache.tools.ant.ProjectComponent
- 
- org.apache.tools.ant.Task
- 
- org.apache.tools.ant.taskdefs.JDBCTask
 
 
 
- 
- All Implemented Interfaces:
- java.lang.Cloneable
 - Direct Known 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 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 org.apache.tools.ant.ProjectComponentdescription, location, project
 
- 
 - 
Constructor SummaryConstructors Constructor Description JDBCTask()
 - 
Method SummaryModifier and Type Method Description voidaddConnectionProperty(Property var)Additional properties to put into the JDBC connection string.PathcreateClasspath()Add a path to the classpath for loading the driver.PathgetClasspath()Gets the classpath.protected java.sql.ConnectiongetConnection()Creates a new Connection as using the driver, url, userid and password specified.protected AntClassLoadergetLoader()Get the classloader used to create a driver.protected static java.util.Hashtable<java.lang.String,AntClassLoader>getLoaderMap()Get the cache of loaders and drivers.java.lang.StringgetPassword()Gets the password.java.lang.StringgetRdbms()Gets the rdbms.java.lang.StringgetUrl()Gets the url.java.lang.StringgetUserId()Gets the userId.java.lang.StringgetVersion()Gets the version.booleanisAutocommit()Gets the autocommit.voidisCaching(boolean value)Set the caching attribute.protected booleanisValidRdbms(java.sql.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.voidsetClasspathRef(Reference r)Set the classpath for loading the driver using the classpath reference.voidsetDriver(java.lang.String driver)Class 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(java.lang.String password)Sets the password; required.voidsetRdbms(java.lang.String rdbms)Execute task only if the lower case product name of the DB matches thisvoidsetUrl(java.lang.String url)Sets the database connection URL; required.voidsetUserid(java.lang.String userId)Set the user name for the connection; required.voidsetVersion(java.lang.String version)Sets the version string, execute task only if rdbms version match; optional.- 
Methods inherited from class org.apache.tools.ant.TaskbindToOwner, 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.ProjectComponentclone, getDescription, getLocation, getProject, setDescription, setLocation, setProject
 
- 
 
- 
- 
- 
Method Detail- 
setClasspathpublic void setClasspath(Path classpath) Sets the classpath for loading the driver.- Parameters:
- classpath- The classpath to set
 
 - 
setCachingpublic 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- booleanvalue
 
 - 
createClasspathpublic Path createClasspath() Add a path to the classpath for loading the driver.- Returns:
- a path to be configured
 
 - 
setClasspathRefpublic void setClasspathRef(Reference r) Set the classpath for loading the driver using the classpath reference.- Parameters:
- r- a reference to a classpath
 
 - 
setDriverpublic void setDriver(java.lang.String driver) Class name of the JDBC driver; required.- Parameters:
- driver- The driver to set
 
 - 
setUrlpublic void setUrl(java.lang.String url) Sets the database connection URL; required.- Parameters:
- url- The url to set
 
 - 
setPasswordpublic void setPassword(java.lang.String password) Sets the password; required.- Parameters:
- password- The password to set
 
 - 
setAutocommitpublic void setAutocommit(boolean autocommit) Auto commit flag for database connection; optional, default false.- Parameters:
- autocommit- The autocommit to set
 
 - 
setRdbmspublic void setRdbms(java.lang.String rdbms) Execute task only if the lower case product name of the DB matches this- Parameters:
- rdbms- The rdbms to set
 
 - 
setVersionpublic void setVersion(java.lang.String version) Sets the version string, execute task only if rdbms version match; optional.- Parameters:
- version- The version to set
 
 - 
setFailOnConnectionErrorpublic 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
 
 - 
isValidRdbmsprotected boolean isValidRdbms(java.sql.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
 
 - 
getLoaderMapprotected static java.util.Hashtable<java.lang.String,AntClassLoader> getLoaderMap() Get the cache of loaders and drivers.- Returns:
- a hashtable
 
 - 
getLoaderprotected AntClassLoader getLoader() Get the classloader used to create a driver.- Returns:
- the classloader
 
 - 
addConnectionPropertypublic void addConnectionProperty(Property var) Additional properties to put into the JDBC connection string.- Parameters:
- var- Property
- Since:
- Ant 1.8.0
 
 - 
getConnectionprotected java.sql.Connection getConnection() throws BuildExceptionCreates 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.
 
 - 
isCachingpublic void isCaching(boolean value) Set the caching attribute.- Parameters:
- value- a- booleanvalue
 
 - 
getClasspathpublic Path getClasspath() Gets the classpath.- Returns:
- Returns a Path
 
 - 
isAutocommitpublic boolean isAutocommit() Gets the autocommit.- Returns:
- Returns a boolean
 
 - 
getUrlpublic java.lang.String getUrl() Gets the url.- Returns:
- Returns a String
 
 - 
getUserIdpublic java.lang.String getUserId() Gets the userId.- Returns:
- Returns a String
 
 - 
setUseridpublic void setUserid(java.lang.String userId) Set the user name for the connection; required.- Parameters:
- userId- The userId to set
 
 - 
getPasswordpublic java.lang.String getPassword() Gets the password.- Returns:
- Returns a String
 
 - 
getRdbmspublic java.lang.String getRdbms() Gets the rdbms.- Returns:
- Returns a String
 
 - 
getVersionpublic java.lang.String getVersion() Gets the version.- Returns:
- Returns a String
 
 
- 
 
-