public abstract class JDBCTask extends Task
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; } }
Constructor and Description |
---|
JDBCTask() |
Modifier and Type | Method and Description |
---|---|
void |
addConnectionProperty(Property var)
Additional properties to put into the JDBC connection string.
|
Path |
createClasspath()
Add a path to the classpath for loading the driver.
|
Path |
getClasspath()
Gets the classpath.
|
protected java.sql.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<String,AntClassLoader> |
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(java.sql.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 |
setFailOnConnectionError(boolean b)
whether the task should cause the build to fail if it cannot
connect to the database.
|
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.
|
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
clone, getDescription, getLocation, getProject, setDescription, setLocation, setProject
public void addConnectionProperty(Property var)
public Path createClasspath()
public Path getClasspath()
protected java.sql.Connection getConnection() throws BuildException
BuildException
- if the UserId/Password/Url is not set or there
is no suitable driver or the driver fails to load.protected AntClassLoader getLoader()
protected static Hashtable<String,AntClassLoader> getLoaderMap()
public String getPassword()
public String getRdbms()
public String getUrl()
public String getUserId()
public String getVersion()
public boolean isAutocommit()
public void isCaching(boolean value)
value
- a boolean
valueprotected boolean isValidRdbms(java.sql.Connection conn)
conn
- the jdbc connectionpublic void setAutocommit(boolean autocommit)
autocommit
- The autocommit to setpublic void setCaching(boolean enable)
enable
- a boolean
valuepublic void setClasspath(Path classpath)
classpath
- The classpath to setpublic void setClasspathRef(Reference r)
r
- a reference to a classpathpublic void setDriver(String driver)
driver
- The driver to setpublic void setFailOnConnectionError(boolean b)
public void setPassword(String password)
password
- The password to setpublic void setRdbms(String rdbms)
rdbms
- The rdbms to setpublic void setUrl(String url)
url
- The url to setpublic void setUserid(String userId)
userId
- The userId to setpublic void setVersion(String version)
version
- The version to set