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;
}
}
| Modifier and Type | Field and Description |
|---|---|
| private boolean | autocommit
Autocommit flag. |
| private boolean | |
| private Path | |
| private List | 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 | 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. |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public void | addConnectionProperty(Property
Property var)Additional properties to put into the JDBC connection string. |
| public Path | Returns: a path to be configuredAdd a path to the classpath for loading the driver. |
| public Path | |
| protected Connection | Returns: Connection the newly created connection or null if the connection failed and failOnConnectionError is false.Creates a new Connection as using the driver, url, userid and password specified. |
| private Driver | |
| protected AntClassLoader | |
| protected static Hashtable | |
| public String | |
| public String | |
| public String | |
| public String | |
| public String | |
| public boolean | |
| public void | |
| protected boolean | Returns: true if we are connected to the correct RDBMSthe 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 | |
| public void | |
| public void | setClasspathRef(Reference
a reference to a classpath r)Set the classpath for loading the driver using the classpath reference. |
| public void | |
| public void | setFailOnConnectionError(boolean
boolean b)whether the task should cause the build to fail if it cannot connect to the database. |
| public void | |
| public void | |
| public void | |
| public void | |
| public void | setVersion(String
The version to set version)Sets the version string, execute task only if rdbms version match; optional. |
| autocommit | back to summary |
|---|---|
| private boolean autocommit Autocommit flag. Default value is false | |
| caching | back to summary |
|---|---|
| private boolean caching | |
| classpath | back to summary |
|---|---|
| private Path classpath | |
| connectionProperties | back to summary |
|---|---|
| private List<Property> connectionProperties Additional properties to put into the JDBC connection string.
| |
| driver | back to summary |
|---|---|
| private String driver DB driver. | |
| failOnConnectionError | back to summary |
|---|---|
| private boolean failOnConnectionError whether the task fails when ant fails to connect to the database.
| |
| HASH_TABLE_SIZE | back to summary |
|---|---|
| private static final int HASH_TABLE_SIZE | |
| loader | back to summary |
|---|---|
| private AntClassLoader loader | |
| LOADER_MAP | back 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. | |
| password | back to summary |
|---|---|
| private String password Password | |
| rdbms | back to summary |
|---|---|
| private String rdbms RDBMS Product needed for this SQL. | |
| url | back to summary |
|---|---|
| private String url DB url. | |
| userId | back to summary |
|---|---|
| private String userId User name. | |
| version | back to summary |
|---|---|
| private String version RDBMS Version needed for this SQL. | |
| JDBCTask | back to summary |
|---|---|
| public JDBCTask() | |
| addConnectionProperty | back to summary |
|---|---|
| public void addConnectionProperty(Property var) Additional properties to put into the JDBC connection string.
| |
| createClasspath | back to summary |
|---|---|
| public Path createClasspath() Add a path to the classpath for loading the driver.
| |
| getClasspath | back to summary |
|---|---|
| public Path getClasspath() Gets the classpath.
| |
| getConnection | back 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.
| |
| getDriver | back 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.
| |
| getLoader | back to summary |
|---|---|
| protected AntClassLoader getLoader() Get the classloader used to create a driver.
| |
| getLoaderMap | back to summary |
|---|---|
| protected static Hashtable Get the cache of loaders and drivers.
| |
| getPassword | back to summary |
|---|---|
| public String getPassword() Gets the password.
| |
| getRdbms | back to summary |
|---|---|
| public String getRdbms() Gets the rdbms.
| |
| getUrl | back to summary |
|---|---|
| public String getUrl() Gets the url.
| |
| getUserId | back to summary |
|---|---|
| public String getUserId() Gets the userId.
| |
| getVersion | back to summary |
|---|---|
| public String getVersion() Gets the version.
| |
| isAutocommit | back to summary |
|---|---|
| public boolean isAutocommit() Gets the autocommit.
| |
| isCaching | back to summary |
|---|---|
| public void isCaching(boolean value) Set the caching attribute.
| |
| isValidRdbms | back to summary |
|---|---|
| protected boolean isValidRdbms(Connection conn) Verify we are connected to the correct RDBMS
| |
| setAutocommit | back to summary |
|---|---|
| public void setAutocommit(boolean autocommit) Auto commit flag for database connection; optional, default false.
| |
| setCaching | back 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
| |
| setClasspath | back to summary |
|---|---|
| public void setClasspath(Path classpath) Sets the classpath for loading the driver.
| |
| setClasspathRef | back to summary |
|---|---|
| public void setClasspathRef(Reference r) Set the classpath for loading the driver using the classpath reference.
| |
| setDriver | back to summary |
|---|---|
| public void setDriver(String driver) Class name of the JDBC driver; required.
| |
| setFailOnConnectionError | back to summary |
|---|---|
| public void setFailOnConnectionError(boolean b) whether the task should cause the build to fail if it cannot connect to the database.
| |
| setPassword | back to summary |
|---|---|
| public void setPassword(String password) Sets the password; required.
| |
| setRdbms | back to summary |
|---|---|
| public void setRdbms(String rdbms) Execute task only if the lower case product name of the DB matches this
| |
| setUrl | back to summary |
|---|---|
| public void setUrl(String url) Sets the database connection URL; required.
| |
| setUserid | back to summary |
|---|---|
| public void setUserid(String userId) Set the user name for the connection; required.
| |
| setVersion | back to summary |
|---|---|
| public void setVersion(String version) Sets the version string, execute task only if rdbms version match; optional.
| |