MySQL Connection

The SPARX sample code explains how to connect to a local .eap file, but what your model is in a remote database? This sample code demonstrates how to connect to a MySQL database, but other database connection strings are similar:

package tech.otter.EAUtil;

import java.util.Map;

public class MySQLConnectionBuilder {
    private String DBName = "";
    private String DBPass = "";
    private String DBUser = "";
    private String DBSource = "";
    private String DBSchema = "";

    private int lazyLoad = 1;
    private boolean persistSecurityInfo = true;

    /**
     * Handy for environment variables.
     * @param settings
     * @return
     */
    public MySQLConnectionBuilder load(Map<String, String> settings) {
        this.DBName = settings.get("DB_NAME");
        this.DBPass = settings.get("DB_PASS");
        this.DBUser = settings.get("DB_USER");
        this.DBSource = settings.get("DB_SOURCE");
        this.DBSchema = settings.get("DB_SCHEMA");
        return this;
    }

    @Override
    public String toString() {
        return new StringBuilder()
                .append(DBName)
                .append(" --- DBType=0;")
                .append("Connect=Provider=MSDASQL.1;")
                .append("Password=" + DBPass + ";")
                .append("Persist Security Info=" + (persistSecurityInfo ? "True" : "False") + ";")
                .append("User ID=" + DBUser + ";")
                .append("Data Source=" + DBSource + ";")
                .append("Initial Catalog=" + DBSchema + ";")
                .append("LazyLoad=" + lazyLoad + ";")
                .toString();
    }

    public void setLazyLoad(boolean lazyLoad) {
        if(lazyLoad) {
            this.lazyLoad = 1;
        } else {
            this.lazyLoad = 0;
        }
    }
    public void setPersistSecurityInfo(boolean persistSecurityInfo) {
        this.persistSecurityInfo = persistSecurityInfo;
    }
    // Imagine other setters here...
}
MySQL Connection Builder

This string is used the same way you’d use a file path to connect:

public class Test {

    public static void main(String[] args) {
        String connection = new MySQLConnectionBuilder().load(System.getenv()).toString();

        log("===== Start =====");
        Repository r = new org.sparx.Repository();
        if(!r.OpenFile(connection)) {
            System.err.println("Connection failed: " + r.GetLastError().toString());
            return;
        }
        // You can do things with your repository here...
}
Simple Example