|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--org.apache.tools.ant.ProjectComponent | +--org.apache.tools.ant.Task | +--org.apache.tools.ant.taskdefs.JDBCTask
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 ingore) {}
}
if (conn != null) {
try {conn.close();}catch (SQLException ingore) {}
}
}
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
}
Field Summary |
Fields inherited from class org.apache.tools.ant.Task |
description, location, target, taskName, taskType, wrapper |
Fields inherited from class org.apache.tools.ant.ProjectComponent |
project |
Constructor Summary | |
JDBCTask()
|
Method Summary | |
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()
|
protected static java.util.Hashtable |
getLoaderMap()
|
java.lang.String |
getPassword()
Gets the password. |
java.lang.String |
getRdbms()
Gets the rdbms. |
java.lang.String |
getUrl()
Gets the url. |
java.lang.String |
getUserId()
Gets the userId. |
java.lang.String |
getVersion()
Gets the version. |
boolean |
isAutocommit()
Gets the autocommit. |
void |
isCaching(boolean value)
|
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(java.lang.String driver)
Class name of the JDBC driver; required. |
void |
setPassword(java.lang.String password)
Sets the password; required. |
void |
setRdbms(java.lang.String rdbms)
Execute task only if the lower case product name of the DB matches this |
void |
setUrl(java.lang.String url)
Sets the database connection URL; required. |
void |
setUserid(java.lang.String userId)
Set the user name for the connection; required. |
void |
setVersion(java.lang.String version)
Sets the version string, execute task only if rdbms version match; optional. |
Methods inherited from class org.apache.tools.ant.Task |
execute, getDescription, getLocation, getOwningTarget, getRuntimeConfigurableWrapper, getTaskName, getTaskType, getWrapper, handleErrorFlush, handleErrorOutput, handleFlush, handleInput, handleOutput, init, isInvalid, log, log, maybeConfigure, perform, reconfigure, setDescription, setLocation, setOwningTarget, setRuntimeConfigurableWrapper, setTaskName, setTaskType |
Methods inherited from class org.apache.tools.ant.ProjectComponent |
getProject, setProject |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public JDBCTask()
Method Detail |
public void setClasspath(Path classpath)
classpath
- The classpath to setpublic void setCaching(boolean enable)
enable
- public Path createClasspath()
public void setClasspathRef(Reference r)
public void setDriver(java.lang.String driver)
driver
- The driver to setpublic void setUrl(java.lang.String url)
url
- The url to setpublic void setPassword(java.lang.String password)
password
- The password to setpublic void setAutocommit(boolean autocommit)
autocommit
- The autocommit to setpublic void setRdbms(java.lang.String rdbms)
rdbms
- The rdbms to setpublic void setVersion(java.lang.String version)
version
- The version to setprotected boolean isValidRdbms(java.sql.Connection conn)
protected static java.util.Hashtable getLoaderMap()
protected AntClassLoader getLoader()
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.public void isCaching(boolean value)
public Path getClasspath()
public boolean isAutocommit()
public java.lang.String getUrl()
public java.lang.String getUserId()
public void setUserid(java.lang.String userId)
userId
- The userId to setpublic java.lang.String getPassword()
public java.lang.String getRdbms()
public java.lang.String getVersion()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |