How to Access Databases Directly in Flying Logic

How to Access Databases Directly in Flying Logic

The 2.2 release of Flying Logic introduced some additional scripting features that allow direct access to any database with a JDBC driver.

You might be wondering why that would be necessary — doesn’t Python and Java already support access to databases. Well, yes and no. Yes, in that the functionality is there. No, because of Flying Logic’s internal class-loading model causes calls to Python and Java’s JDBC to fail.

There are two new scripting method that can be used to resolve this issue.

Jython, the implementation of Python incorporated into Flying Logic Pro, has a package ZxJDBC that includes the method zxJDBC.connect to open a database connection. This call is replaced by a method in the Application class in Flying Logic Pro. Here is an example for MySQL.

# You should have previously added the path to the MySQL JDBC jar
# via Application.appendClassPath method
url = "jdbc:mysql://someserver/somedb"
username = "someuser"
password = "somepassword"

driver = "com.mysql.jdbc.Driver"
# obtain a connection using the with-statment
with Application.createSQLConnection(url, username, password, driver) as conn:
with conn.cursor() as c:
# execute SQL commands

Alternately, you could use Java’s java.sql package to open a connection. This only works if the createCompatibleDriver method in the Application class is used to “wrap” a driver created from a JDBC package.

# You should have previously added the path to the MySQL JDBC jar
# via Application.appendClassPath method
from com.mysql.jdbc import Driver
from java.sql import DriverManager

# Need to create shim Driver from real Driver
realDriver = Driver()
shimDriver = Application.createCompatibleDriver( realDriver )

# register driver and open connection
DriverManager.registerDriver(shimDriver);
conn = DriverManager.getConnection("jdbc:mysql://someserver/somedb", "someuser", "somepassword")

Start taking all the information you have in your databases and create some helpful graphs!