Using a script to read data directly from a database is another way to access information. This could be used for data migration, or for updating an IJC database in a routine manner. Accessing databases from Groovy is rather simple (see the Groovy groovy.sql.SQL class).
One aspect of this deserves attention - setting up the JDBC connection to the database. Some strange aspects of how the class loading is done means that the standard connection mechanism (Connection conn = DriverManager.getConnection(url, username, password)
) will not work. Instead, we suggest you obtain connections by creating a DataSource. Suggested connection scriptlets are shown below.
Example Derby connection:
Example MySQL connection:
Example Oracle connection:
This scriptlet is a simple example of how to read data in from a table in a database:
It is important to note that the demo scriptlet ends with closing the sql connection. This is a good practice, if not important. Also note that these databases do NOT have be IJC databases - they can be any accessible database.