/
How to create a form in Confluence that displays data from database using Scripting for Confluence
How to create a form in Confluence that displays data from database using Scripting for Confluence
This article explains how to create a form in Confluence that displays data from the database using a single app - Scripting for Confluence.
Instructions
- On a new Confluence page, add Groovy macro of Scripting for the Confluence app.
Add the below given groovy script inside the groovy macro:
/** * Run a simple SQL query using a direct JDBC connection * Assumes: Script Plugin for Confluence 4.x or above * Assumes: Confluence 4.1 or above * Requires: mySQL jdbc driver in <Confluence server install>/lib directory * Wiki markup: * {groovy:output=wiki|script=#https://bitbucket.org/bob_swift/examples/raw/tip/confluence/groovy/sqlQuery.groovy} * {groovy} * * @param dbServer - TCP address of mySQL DB server assumed to be on the default port: 3306 */ import java.sql.Driver import groovy.sql.Sql def connection = getConnection( location: '/opt/atlassian/confluence/lib/', // Confluence installation lib directory or use an absolute directory path url: "jdbc:mysql://your-server-url:50274/confluence", // example using mySql driver: 'com.mysql.jdbc.Driver', user: 'confuser', password: '****', ) def sql = Sql.newInstance(connection) def searchtext = request.getParameter("searchtext") def form = sprintf(""" <form class="aui" method="get" action="%s"> <input type="hidden" name="searchTel" value="1"> <input type="text" name="searchtext" value="%s"> <input class="button submit" type="submit" value="Submit" id="comment-search-button"> </form> """, contentEntityObject.getUrlPath(), searchtext==null ? "" : searchtext) out.println form if (searchtext != null) { out.println(sprintf("Suche nach \"%s\"", searchtext)) out.println("""<table class=\"wrapped confluenceTable tablesorter tablesorter-default\" role=\"grid\"><thead> <tr role=\"row\"> <th class=\"confluenceTh\">id</th> </tr> </thead><tbody>""") sql.execute( [nbstr: searchtext, name: '%'+searchtext+'%'], "SELECT * FROM big01 WHERE id =:nbstr", { _, result -> result.each { row -> out.println( sprintf( """<tr role=\"row\"> <td class=\"confluenceTd\">%s</td> </tr>""", row.id ) ) } } ) out.println("</tbody></table>"); } /** * Get a direct JDBC connection from the connection parameters (see above) */ def getConnection(final Map parameters) { def classLoader = getClassLoader(new File(parameters.location)) // get class loader with necessary DB driver jar files Driver driver = (Driver) Class.forName(parameters.driver, true, classLoader).newInstance() def properties = new Properties() // login parameters properties.setProperty('user', parameters.user) properties.setProperty('password', parameters.password) return driver.connect(parameters.url, properties) } /** * Get a class loader with jars loaded from the directory provided */ def getClassLoader(final File directory) { URLClassLoader loader = null; String[] fileList = directory.list(new FilterByExtension("jar")); // get all jars in directory if (fileList.length > 0) { URL[] urlList = new URL[fileList.length]; for (int i = 0; i < fileList.length; i++) { File jarFile = new File(directory, fileList[i]); urlList[i] = new URL("jar:file:" + (jarFile.getAbsolutePath().charAt(0) == '/' ? "" : '/') + jarFile.getAbsolutePath() + "!/"); // out.println("urlList: " + urlList[i].toString()); } loader = URLClassLoader.newInstance(urlList, ClassLoader.getSystemClassLoader()); } return loader; } /** * Filter file list by extension. */ public class FilterByExtension implements FilenameFilter { String extension; public FilterByExtension(final String extension) { this.extension = "." + extension; } @Override public boolean accept(File dir, String name) { return name.endsWith(extension); } }
Make changes to the URL, user, password according to your database.
- The above script provides users with a form (as shown in the screenshot below) that asks for user input to provide the ID value and pulls all the rows from big01 table from the database for that particular user input:
- Publish the page.
- Test the script by giving an input value.
- The output from the user form looks as given below:
The above script is tested for MySQL database on Confluence v7.4.1.
, multiple selections available,
Related content
How to input data in a form and update database using the Run Self-Service Reports for Confluence app
How to input data in a form and update database using the Run Self-Service Reports for Confluence app
More like this
How to use SQL Query in the Run macro with scripting capabilities to display data in the Confluence page
How to use SQL Query in the Run macro with scripting capabilities to display data in the Confluence page
More like this
How to use SQL Query in the Run macro to display data in the Confluence page
How to use SQL Query in the Run macro to display data in the Confluence page
More like this
SQL for Confluence datasource configuration
SQL for Confluence datasource configuration
More like this
How to use SQL for Confluence with Run Self Service Reports for dynamic content
How to use SQL for Confluence with Run Self Service Reports for dynamic content
More like this
How to input data into a form and retrieve the results from database
How to input data into a form and retrieve the results from database
More like this