Example scripts
To homepage
Confluence

Display SQL Results from an External Database Macro
App in script

ScriptRunner For Confluence
by Adaptavist
Compatibility

Confluence (7.15 - 8.6)

ScriptRunner For Confluence (7.10.0)
Language |
groovy
import com.onresolve.scriptrunner.db.DatabaseUtil
import com.onresolve.scriptrunner.db.NoSuchDataSourceException
import groovy.xml.MarkupBuilder
/* You will replace 'petstore' with whatever you named your pool when
configuring your Resource. */
def rows
try {
rows = DatabaseUtil.withSql('petstore') { sql ->
sql.rows('select NAME, SPECIES, ID From petstore')
}
}
catch (NoSuchDataSourceException e) {
return "Data source is invalid: ${e.message}"
}
def writer = new StringWriter()
/* Notice our use of Groovy’s MarkupBuilder here. This is a helper class for creating XML or HTML markup.
It’s very important to use the MarkupBuilder here to ensure your HTML is safe. */
def builder = new MarkupBuilder(writer)
builder.table('class': 'aui') {
tr {
rows.first().keySet().each { key ->
th {
mkp.yield(key)
}
}
}
rows.each { columns ->
tr {
columns.each { Map.Entry cell ->
td {
if (cell.value) {
mkp.yield(cell.value)
}
}
}
}
}
}
writer.toString()
Having an issue with this script?
Report it here