The JDBCTask.java file in the ant source tree under /src/main/org/apache/tools/ant/taskdefs has a sample sql task in it's comments section. The task logs the values returned from a sql select.

I put the code in below. It would be trivial to create your own ant task based on this one and change the code to set ant properties instead of writing to the log.

Properties are set from task code like this...
getProject().setUserProperty("task.classpath", "mypath");

There's a really great doc on creating ant tasks at...
http://ant.apache.org/manual/index.html

Scott


/**
* 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;
}

}


David McTavish wrote:
I don't believe the sql task is meant for retrieving records so much as
running inserts and updates. You can redirect the output to a file, which
may be the only way you can retrieve these values.
d.


-----Original Message-----
From: ALIA, David [mailto:[EMAIL PROTECTED]]
Sent: Thursday, January 30, 2003 10:59 AM
To: [EMAIL PROTECTED]
Subject: Multiple values in sql task with Ant


Hi. I've searched through archives but I wasn't able to find correct answer
to this question...
I'm trying to get multiple fields values from a sql task into several
variables. For example,

<sql ...>

SELECT field1, field2, field3 FROM table WHERE condition

</sql>
And then manipulate properties like ${field1}, ${field2}... I can also grab these values from a java task, but the question subsists :
how do I put these values into variables ? Thanks a lot for your help David

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to