# Connecting to the SQL database from a module

**URL:** <https://forum.inductiveautomation.com/t/connecting-to-the-sql-database-from-a-module/10916>\
**Category:** Module Development\
**Created:** [December 14, 2015, 7:14pm UTC](https://forum.inductiveautomation.com/t/connecting-to-the-sql-database-from-a-module/10916 "2015-12-14T19:14:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pmclean2](https://avatars.discourse-cdn.com/v4/letter/p/9de0a6/32.png) [@pmclean2](https://forum.inductiveautomation.com/u/pmclean2)\
**Post date:** [December 14, 2015, 7:14pm UTC](https://forum.inductiveautomation.com/t/connecting-to-the-sql-database-from-a-module/10916/1 "2015-12-14T19:14:50Z")

</div>

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();
		}
	}
}
```

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [December 14, 2015, 7:54pm UTC](https://forum.inductiveautomation.com/t/connecting-to-the-sql-database-from-a-module/10916/2 "2015-12-14T19:54:38Z")

</div>

What doesn’t work? How does it fail?
