Fork me on GitHub

Download MySQL JDBC Java Connector

Get it here and place on disk with Confluence, for example:

/opt/atlassian_confluence/lib/mysql-connector-java.jar

Create import script

Place on disk to Confluence data directory, for example:

/var/lib/confluence_data/script/sqlQuery.groovy

// (c) 2015, Konstantin Shagygin <k0ste@k0ste.ru>
// (c) 2015, Vladislav kaykovskiy <v.kaykovskiy@e2e4online.ru>

import java.sql.Driver

driver = 'jar:file:/opt/atlassian_confluence/lib/mysql-connector-java.jar!/'

def getMysqlConnection(final Map parameters) {
  URL[] urlList = new URL[1];
  urlList[0] = new URL(driver)
  URLClassLoader loader = URLClassLoader.newInstance(urlList, ClassLoader.getSystemClassLoader())

  Driver driver = (Driver) Class.forName('com.mysql.jdbc.Driver', true, loader).newInstance()
  def properties = new Properties()
  properties.setProperty('user', parameters.user)
  properties.setProperty('password', parameters.password)
  return driver.connect(parameters.url, properties)
}

Add Groovy macros

Add Bob Swift Groovy macros and import #sqlQuery.groovy

import groovy.sql.Sql

def connection = getMysqlConnection(
  url: 'jdbc:mysql://mysql.host:3306/database',
  user: 'foo',
  password: 'bar'
)
def sql = Sql.newInstance(connection)

sql.eachRow("SELECT * FROM foobar") {
  row -> out.println 'Test ' + row.foo + ', ' + row.bar + '.'
}