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 ingore) {}
}
if (conn != null) {
try {conn.close();}catch (SQLException ingore) {}
}
}
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
}
Attribute | Description | Type | Required? |
url | Sets the database connection URL; required. | String | ? |
classpath | Sets the classpath for loading the driver. | Path | ? |
rdbms | Execute task only if the lower case product name of the DB matches this | String | ? |
autocommit | Auto commit flag for database connection; optional, default false. | boolean | ? |
userid | Set the user name for the connection; required. | String | ? |
version | Sets the version string, execute task only if rdbms version match; optional. | String | ? |
classpathref | Set the classpath for loading the driver using the classpath reference. | Reference | ? |
caching | Caching loaders / driver. This is to avoid getting an OutOfMemoryError when calling this task multiple times in a row; default: true | boolean | ? |
password | Sets the password; required. | String | ? |
driver | Class name of the JDBC driver; required. | String | ? |
<sometask>
<somepath>
<pathelement location="/path/to/file.jar" />
<pathelement path="/path/to/file2.jar:/path/to/class2;/path/to/class3" />
<pathelement location="/path/to/file3.jar" />
<pathelement location="/path/to/file4.jar" />
</somepath>
</sometask>
The object implemention sometask
must provide a method called
createSomepath
which returns an instance of Path
.
Nested path definitions are handled by the Path object and must be labeled
pathelement
.
The path element takes a parameter path
which will be parsed
and split into single elements. It will usually be used
to define a path from an environment variable.
Attribute | Description | Type | Required |
refid |
Makes this instance in effect a reference to another Path instance.
You must not set another attribute or nest elements inside this element if you make it a reference. |
Reference | ? |
path | Parses a path definition and creates single PathElements. | String | ? |