Connecting to the SQL database from a module

Hi,

I tried to connect my module to the MySQL database in Ignition using the example found in the SDK developer guide. But it seems that the “context” variable is not the same as the GatewayContext. Can you provide a complete example on how to access the database from a module please. My current code below.

Thanks,

Pierre McLean

package com.lafargeholcim.lh_indapps.gateway;

import com.lafargeholcim.lh_indapps.gateway.QMCScriptHandler;
import com.inductiveautomation.ignition.common.licensing.LicenseState;
import com.inductiveautomation.ignition.common.script.ScriptManager;
import com.inductiveautomation.ignition.gateway.model.AbstractGatewayModuleHook;
import com.inductiveautomation.ignition.gateway.model.GatewayContext;

import com.inductiveautomation.ignition.gateway.datasource.*;
import com.inductiveautomation.ignition.gateway.localdb.hsql.HSQLDBManager;

public class GatewayHook extends AbstractGatewayModuleHook {

@Override
public void setup(GatewayContext context) {
	
	SRConnection con;
	int maxVal;
	
	try{
	con = context.getDatasourceManager().getConnection("minecad");
	//con.runPrepQuery("SELECT * FROM cont_bench WHERE ID = ?", 1);
	maxVal= (Integer) con.runScalarQuery("SELECT max(ID) FROM cont_bench");
	}finally{
		if(con!=null){
			con.close();
		}
	}
}

What doesn’t work? How does it fail?