# How to retrieve BDM data from groovy classes of your Rest API extension

**URL:** <https://community.ofelia.com/t/how-to-retrieve-bdm-data-from-groovy-classes-of-your-rest-api-extension/12063>\
**Category:** Blog\
**Created:** [15 May 2019 18:42 UTC](https://community.ofelia.com/t/how-to-retrieve-bdm-data-from-groovy-classes-of-your-rest-api-extension/12063 "2019-05-15T18:42:44Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://europe1.discourse-cdn.com/flex013/uploads/community_bonitasoft/original/2X/6/6b7b18c0478f2645369e82e009b65e3b97f2cc12.svg) [@system](https://community.ofelia.com/u/system)\
**Post date:** [15 May 2019 18:42 UTC](https://community.ofelia.com/t/how-to-retrieve-bdm-data-from-groovy-classes-of-your-rest-api-extension/12063/1 "2019-05-15T18:42:44Z")

</div>

If you ever wondered how to retrieve data from a Rest API extension this article is for you.

When developing a Rest API extension, if you need to access data from your Database, these two options can be available to you :

1. Using DAO and one of the queries available in your BDM;
2. Using a SQL Query.

##### 1. Using DAO :

As a first step, to be able to import your BDM objects you will need to add the following dependencies in your “pom.xml” file :

```
	<dependency>
	<groupId>com.company.model</groupId>
	<artifactId>bdm-client</artifactId>
	<version>1.0.0</version>
	<scope>provided</scope>
	</dependency>

	<dependency>
	<groupId>com.company.model</groupId>
	<artifactId>bdm-dao</artifactId>
	<version>1.0.0</version>
	<scope>provided</scope>
	</dependency>`

```

For the first option, here’s an example of code that you can use (in index.groovy for example) to call one of the queries available in your BDM :

```
	def veloDAO = context.apiClient.getDAO(VeloDAO.class);

	List<Velo> listeVelos = veloDAO.myQuery(0, 100)

```

Here we have already created a Business object called “Velo” and a custom query in the BDM (with no parameters). The list “listeVelos” will contain the results of your custom query.

##### 2. Using a SQL Query :

For the second option, here’s an another example of code to use (in index.groovy for example) in order to call a SQL query directly from your groovy class :

```
	private DataSource getDataSourceConnection(String sqlDataSourceName) {

	try {
		final Context ctx = new InitialContext();
		final DataSource dataSource = (DataSource) ctx.lookup(sqlDataSourceName);
		return dataSource;

	} catch (NamingException e) {
		// Handle your exception
	}

	return null;
}

```

Here you need to get your datasource (using your datasource name). Once you have your object Datasource, you can open a connection to the database and run your sql query as this example :

```
	Connection con = null;
	PreparedStatement pstmt = null;
	ResultSet results = null;

	final DataSource dataSource = getDataSourceConnection(sqlDataSourceName);

	if (dataSource != null) {
		con = dataSource.getConnection();

		String sqlRequest = "SELECT * FROM VELO";

		pstmt = con.prepareStatement(sqlRequest);
		results = pstmt.executeQuery();
	}

```

The result of your SQL query will be stored in a varialble of ResultSet type. After, you can access to all the fields contained inside.

Here’s an example of a Groovy class having these code examples :

> **[ExampleGetBDMFromRestAPI.groovy](https://drive.google.com/file/d/1mpMGbZ8liTyuF2wtqs2jSZ63YiRFQ6Xs/edit)**
>
> Google Drive file.

Also, you can find here an example of a project with the same examples (Bonita 7.8.4 Subscription only) :

> **[ProjectGetBDMFromRestAPI.bos](https://drive.google.com/file/d/1l5t4wq3VYXD2t8RYyRaFQewnuyv7sVoM/edit)**
>
> Google Drive file.

Here’s also the link to test your page in local : [http://localhost:8080/bonita/API/extension/resourceName](http://localhost:8080/bonita/API/extension/resourceName)

I hope this little article helped you.
