Browse Source

adding example of usage. patch submitted by Nick Chalko

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@272584 13f79535-47bb-0310-9956-ffa450edef68
master
Erik Hatcher 23 years ago
parent
commit
adf4a3188c
1 changed files with 52 additions and 2 deletions
  1. +52
    -2
      src/main/org/apache/tools/ant/taskdefs/JDBCTask.java

+ 52
- 2
src/main/org/apache/tools/ant/taskdefs/JDBCTask.java View File

@@ -70,7 +70,57 @@ import java.util.Properties;

/**
* Handles JDBC configuration needed by SQL type tasks.
*
* <p>
* The following example class prints the contents of the first column of each row in TableName.
*</p>
*<code><pre>
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;
}

}

</pre></code>


* @author <a href="mailto:nick@chalko.com">Nick Chalko</a>
* @author <a href="mailto:jeff@custommonkey.org">Jeff Martin</a>
* @author <A href="mailto:gholam@xtra.co.nz">Michael McCallum</A>
@@ -437,4 +487,4 @@ public abstract class JDBCTask extends Task {
return version;
}

}
}

Loading…
Cancel
Save