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
}
See database replicator (appstore) for custom queries, in java.
Normally you don't make stored procedures in a mendix db.