Example of Execute_SQL_Query with parameters
Created: 2012-03-16 14:12:35
Last updated: 2012-03-16 14:16:18
This workflow connects to a mysql on localhost, queries the "world" database which is one of the test databases on MySQL 5.5, queries it about which countries have Dutch as a language, and emits answers in both Lists and in xml. This workflow is intended as an example to help you see the syntax for the various inputs to this local service beanshell. see: http://www.mygrid.org.uk/dev/wiki/display/taverna/Execute+SQL+Query for instructions on jars, dependencies etc. The jar used here is: mysql-connector-java-5.1.18-bin.jar To run this workflow example, you'll also need * to have root access to a mysql database server on your machine on the default port. If your database is elsewhere or on another port, edit the url_value accordingly. * Have a database on there called "world" - this is one of the test databases which comes bundled with MySQL 5.5. By default, it has access by root user. Note that the beanshell PutEachElementInAList is not necessary for the workflow to run with a single input value (say "Dutch". But if you want to add more languages to the list of queries (Say, "Dutch", "Japanese", then it is necessary, because each set of parameters needs to be in its own list.
Preview
Run
Run this Workflow in the Taverna Workbench...
Workflow Components
Authors (1)
Titles (1)
Example of Excecute_SQL_Query with parameters |
Descriptions (1)
This workflow connects to a mysql on localhost, queries the "world" database which is one of the test databases on MySQL 5.5, queries it about which countries have Dutch as a language, and emits answers in both Lists and in xml.
This workflow is intended as an example to help you see the syntax for the various inputs to this local service beanshell.
see: http://www.mygrid.org.uk/dev/wiki/display/taverna/Execute+SQL+Query
for instructions on jars, dependencies etc.
The jar used here is:
mysql-connector-java-5.1.18-bin.jar
To run this workflow example, you'll also need
* to have root access to a mysql database server on your machine on the default port. If your database is elsewhere or on another port, edit the url_value accordingly.
* Have a database on there called "world" - this is one of the test databases which comes bundled with MySQL 5.5.
By default, it has access by root user.
Note that the beanshell PutEachElementInAList is not necessary for the workflow to run with a single input value (say "Dutch". But if you want to add more languages to the list of queries (Say, "Dutch", "Japanese", then it is necessary, because each set of parameters needs to be in its own list. |
Dependencies (0)
Inputs (2)
Name |
Description |
params |
|
rootpassword |
|
Processors (7)
Name |
Type |
Description |
Execute_SQL_Query |
localworker |
Script
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import javax.sql.rowset.WebRowSet;
import com.sun.rowset.WebRowSetImpl;
if ((driver == void) || (driver == null) || driver.equals("")) {
throw new RuntimeException("The driver must be specified");
}
if ((url == void) || (url == null) || url.equals("")) {
throw new RuntimeException("The url must be specified");
}
boolean provideXmlBoolean = ((provideXml != void) && (provideXml != null) && Boolean.valueOf(provideXml));
if ((params == void) || (params == null)) {
params = new ArrayList();
}
if ((sql == void) || (sql == null) || sql.equals("")) {
throw new RuntimeException("The sql must be specified");
}
Class c = Thread.currentThread().getContextClassLoader().loadClass(driver);
if (c == null) {
throw new RuntimeException("Class " + driver + " not found");
}
Driver d = c.newInstance();
if (d == null) {
throw new RuntimeException("Could not create instance of driver");
}
Properties p = new Properties();
if ((userid == void) || (userid == null)) {
userid = "";
}
p.setProperty("user", userid);
if ((password == void) || (password == null)) {
password = "";
}
p.setProperty("password", password);
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = d.connect(url, p);
ps = con.prepareStatement(sql);
int paramSize = params.size();
for (int i = 0; i < paramSize; i++) {
ps.setObject(i + 1, params.get(i));
}
rs = ps.executeQuery();
if (provideXmlBoolean) {
WebRowSet webrs = new WebRowSetImpl();
StringWriter sw = new StringWriter();
webrs.writeXml(rs, sw);
xmlresults = sw.toString();
} else {
xmlresults = "";
}
try {
rs.beforeFirst();
} catch (SQLException e) {
// redo the query
rs = ps.executeQuery();
}
ResultSetMetaData rsmd = rs.getMetaData();
int numCols = rsmd.getColumnCount();
resultList = new ArrayList();
// put the results into the results list.
while (rs.next()) {
List row = new ArrayList(numCols);
for (int i = 0; i < numCols; i++) {
String str = rs.getString(i + 1);
row.add(str == null ? "null" : str);
}
resultList.add(row);
}
}
finally {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (con != null) {
con.close();
}
}
|
url_value |
stringconstant |
Valuejdbc:mysql://localhost:3306/world |
userid_value |
stringconstant |
Valueroot |
provideXml_value |
stringconstant |
Valuetrue |
sql_value |
stringconstant |
Valueselect country.Name, countrylanguage.language FROM country, countrylanguage WHERE country.code=countrylanguage.CountryCode AND countrylanguage.Language=?; |
PutEachElementInAList |
beanshell |
Scriptoutputlist=new ArrayList();
for (i = inputlist.iterator(); i.hasNext();) {
ArrayList listtoadd=new ArrayList();
listtoadd.add(i.next());
outputlist.add(listtoadd);
}
|
driver_value |
stringconstant |
Valuecom.mysql.jdbc.Driver |
Beanshells (1)
Name |
Description |
Inputs |
Outputs |
PutEachElementInAList |
|
inputlist
|
outputlist
|
Outputs (2)
Name |
Description |
Execute_SQL_Query_resultList |
|
xmlresults |
|
Datalinks (10)
Source |
Sink |
url_value:value |
Execute_SQL_Query:url |
userid_value:value |
Execute_SQL_Query:userid |
provideXml_value:value |
Execute_SQL_Query:provideXml |
sql_value:value |
Execute_SQL_Query:sql |
PutEachElementInAList:outputlist |
Execute_SQL_Query:params |
rootpassword |
Execute_SQL_Query:password |
driver_value:value |
Execute_SQL_Query:driver |
params |
PutEachElementInAList:inputlist |
Execute_SQL_Query:resultList |
Execute_SQL_Query_resultList |
Execute_SQL_Query:xmlresults |
xmlresults |
Uploader
License
All versions of this Workflow are
licensed under:
Version 1
(of 1)
Credits (1)
(People/Groups)
Attributions (0)
(Workflows/Files)
None
Shared with Groups (0)
None
Featured In Packs (0)
None
Log in to add to one of your Packs
Attributed By (0)
(Workflows/Files)
None
Favourited By (1)
Statistics
Other workflows that use similar services
(0)
There are no workflows in myExperiment that use similar services to this Workflow.
Comments (0)
No comments yet
Log in to make a comment