jdbc()

Usage

jdbc(url, query)

New with 3.3.1:

 jdbc(url, query[, driver])

Description
Fetches data from a JDBC source. Since version 3.3.1, you can define the driver class by setting the full qualified class path in the driver parameter.

The default for the driver parameter is com.mysql.jdbc.Driver.

Note
Make sure the driver is available in a JAR file in Structr’s lib directory. The JAR can be found here: https://www.oracle.com/technetwork/database/application-development/jdbc/downloads/index.html

Examples

${jdbc("jdbc:mysql://localhost:3306",  "SELECT * from Test")}

New with 3.3.1:

You can now define the exact driver class:

${jdbc("jdbc:oracle:thin:user/password@myhost:1521:orcl", "SELECT * from Test", "oracle.jdbc.driver.OracleDriver")}

The following example shows how to use the jdbc() function to import data from an Oracle database:

{ // JavaScript Context
   
    let rows = Structr.jdbc('jdbc:oracle:thin:test/test@localhost:1521:orcl', 'SELECT * FROM test.emails');
   
   
    rows.forEach(function(row) {
       
        Structr.log('Fetched row from Oracle database: ', row);
       
        let fromPerson = Structr.getOrCreate('Person', 'name', row.from_address);
        let toPerson   = Structr.getOrCreate('Person', 'name', row.to_address);
       
        let message = Structr.getOrCreate('EMailMessage',
            'content',   row.email_body,
            'sender',    fromPerson,
            'recipient', toPerson,
            'sentDate',  Structr.parseDate(row.email_date, 'yyyy-MM-dd')
        );
       
        Structr.log('Found existing or created new EMailMessage node: ', message);
       
    });
   
}
About this article
Last change 2019-07-03
Topics