How to make a custom query to the DB?

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.
2 answers


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

    public Boolean executeAction() throws Exception
        // BEGIN USER CODE

        try {
            synchronized (ExecuteSQLQuery.class) {
                if (ExecuteSQLQuery.jdbcTemplate == null) {
                    ExecuteSQLQuery.jdbcTemplate = new JdbcTemplate(this.initDataSource());
        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
    public String toString()
        return "ExecuteSQLQuery";


    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();
        if (this.password != null) {
        return dataSource;


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

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