How to make a custom query to the DB?

0
How can I make a query to the DB, in the Modeler? I know that, for charts, I can use OQL. If I'm not making a chart, can I make a query to the DB, using SQL syntax (I don't want to make a SELECT, I want to call a stored procedure I created in the Mendix DB)? I'm using SQL Server as my DB, so I'm not sure if I can make custom Java. Does the Modeler supports custom SQL queries? Reply: the query has to be made via Java code.
asked
2 answers
2

Louis,

You can use following java code to execute a query to the database. We use this one in a mendix 2.5 project, so you should check the apidocs if this is working in Mendix 3 or 4.

// This file was generated by Mendix Business Modeler 2.5.
//
// WARNING: Only the following code will be retained when actions are regenerated:
// - the import list
// - the code between BEGIN USER CODE and END USER CODE
// - the code between BEGIN EXTRA CODE and END EXTRA CODE
// Other code you write will be lost the next time you deploy the project.
// Special characters, e.g., é, ö, à, etc. are supported in comments.

package importmodule.actions;

import com.mendix.systemwideinterfaces.core.UserAction;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.apache.commons.dbcp.BasicDataSource;
import utils.logging.Logger;

/**
 * Execute a SQL query without a result set to the database
 */
public class ExecuteSQLQuery extends UserAction<Boolean>
{
    private logging.proxies.Process process;
    private String jdbcDriver;
    private String jdbcUrl;
    private String username;
    private String password;
    private String query;

    public ExecuteSQLQuery(String process, String jdbcDriver, String jdbcUrl, String username, String password, String query)
    {
        super();
        this.process = process == null ? null : logging.proxies.Process.valueOf(process);
        this.jdbcDriver = jdbcDriver;
        this.jdbcUrl = jdbcUrl;
        this.username = username;
        this.password = password;
        this.query = query;
    }

    @Override
    public Boolean executeAction() throws Exception
    {
        // BEGIN USER CODE

        try {
            this.validateParams();
            synchronized (ExecuteSQLQuery.class) {
                if (ExecuteSQLQuery.jdbcTemplate == null) {
                    ExecuteSQLQuery.jdbcTemplate = new JdbcTemplate(this.initDataSource());
                }
            }
            ExecuteSQLQuery.jdbcTemplate.execute(this.query);
        }
        catch (final Exception ex) {
            new Logger(this.process).error("Uitvoeren SQL query mislukt door optreden exceptie.", ex);
            throw ex;
        }

        return Boolean.TRUE;

        // END USER CODE
    }

    /**
     * Returns a string representation of this action
     */
    @Override
    public String toString()
    {
        return "ExecuteSQLQuery";
    }

    // BEGIN EXTRA CODE

    private static JdbcTemplate jdbcTemplate = null;

    private void validateParams()
    {
        if (this.jdbcDriver == null) {
            throw new IllegalArgumentException("Parameter 'jdbcDriver' cannot be null.");
        }
        if (this.jdbcUrl == null) {
            throw new IllegalArgumentException("Parameter 'jdbcUrl' cannot be null.");
        }
        if (this.username == null) {
            throw new IllegalArgumentException("Parameter 'username' cannot be null.");
        }
        if (this.query == null) {
            throw new IllegalArgumentException("Parameter 'query' cannot be null.");
        }
        if ("null".equalsIgnoreCase(this.password)) {
            this.password = null;
        }
    }

    private DataSource initDataSource()
    {
        final BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName(this.jdbcDriver);
        dataSource.setUrl(this.jdbcUrl);
        dataSource.setUsername(this.username);
        if (this.password != null) {
            dataSource.setPassword(this.password);
        }
        return dataSource;
    }

    // END EXTRA CODE
}
answered
1

See database replicator (appstore) for custom queries, in java.

Normally you don't make stored procedures in a mendix db.

answered