DB connection from Java action

0
Hello, How can I get db connection reference from java action, or I have to manually connect to the DB using DB URL, and credential? Thanks
asked
3 answers
1

First, import the database replication module from the appstore (MxModelReflection is required first) into your project. Then, specify a Java action with a DatabaseReplication.Database as one of the input parameters, I have a String Query as the second parameter. Then, in Java i used the following to get a connection:

// BEGIN USER CODE

ObjectBaseDBSettings dbSettings = new ObjectBaseDBSettings( this.getContext(), this.DatabaseParameter1.getMendixObject() );

DBReplicationSettings settings = new DBReplicationSettings(this.getContext(), dbSettings, Table.getType(), null);

Statement statement = (new DatabaseConnector( settings.getDbSettings(), settings.getLanguage(), settings.getErrorHandler())).connect( );

statement.executeQuery( Query );

// Use this if the RS is interesting for you
//ResultSet rs = statement.executeQuery( Query );

return true;

// END USER CODE

Make sure you set the correct imports. Mine look like this:

import com.mendix.systemwideinterfaces.core.UserAction; import com.mendix.systemwideinterfaces.core.IMendixObject; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; import java.sql.ResultSet; import java.sql.Types; import oracle.jdbc.OracleTypes; import replication.MessageOptions.Language; import databasereplication.implementation.DBReplicationSettings; import databasereplication.implementation.DatabaseConnector; import databasereplication.implementation.ObjectBaseDBSettings; import databasereplication.proxies.Database; import databasereplication.proxies.Table;

Before using the Java action, make sure you have a Database connection set up from the application and pass the Database connection information to the Java action.

Hope this helps, good luck!

answered
0

See the java action of the database replication (appstore) for a sample.

answered
0

This is outdated info, but still seems to come up sometimes. Better ways to achieve this are documented here: https://docs.mendix.com/howto/extensibility/howto-datastorage-api/

answered