You have been redirected from an outdated version of the article. Below is the content available on this topic. To view the old article click here.

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);
       
    });
   
}

Search results for "jdbc()"

jdbc()

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.

jdbc(url, query)
New with 3.3.1:
jdbc(url, query[, driver])

Importing data from JDBC sources

Importing data from a SQL database is possible via the jdbc() function function in the Structr scripting engine. You can execute an SQL query on a server and process or display the result in your application. The code for that is essentially the same as for the “Import from Webservices” example above.

You can provide the fully-qualified class name (FQCN) of your preferred JDBC driver as a third parameter to the jdbc() function, and Structr will use that driver to make the connection. Please note that the driver JAR is most likely not shipped with the Structr distribution, so you have to put it in the lib directory of your Structr installation manually.