# How to use the java api to make a simple query

**URL:** https://community.stardog.com/t/how-to-use-the-java-api-to-make-a-simple-query/421
**Category:** Support
**Created:** [June 22, 2017, 12:57pm UTC](https://community.stardog.com/t/how-to-use-the-java-api-to-make-a-simple-query/421 "2017-06-22T12:57:20Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Orval](https://avatars.discourse-cdn.com/v4/letter/o/ac8455/32.png) [@Orval](https://community.stardog.com/u/Orval)
#### Post date: [June 22, 2017, 12:57pm UTC](https://community.stardog.com/t/how-to-use-the-java-api-to-make-a-simple-query/421/1 "2017-06-22T12:57:20Z")

</div>

I’m using java to query Stardog and I can’t find a way to use a `Getter` to have the same results than the query below :

```
select ?s ?p ?o where{
    ?s ?p ?o.
    ?s a ex:MyClass
}

```

I tried the code below but, as expected, it only retrieves the triples having the predicate RDF.TYPE

```
connection.get()
          .object(iri(ex, "MyClass"))
          .statements();

```

So how can I do that in java without using a query string ?

---

<div class="post-metadata">

### Author: ![evren](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/evren/32/1515_2.png) [@evren](https://community.stardog.com/u/evren)
#### Post date: [June 22, 2017, 1:08pm UTC](https://community.stardog.com/t/how-to-use-the-java-api-to-make-a-simple-query/421/2 "2017-06-22T13:08:38Z")

</div>

`Getter` API can only execute queries equivalent to a single triple pattern. If you are not using a SPARQL query, you would need to loop over the results of the call you show and make another call for each subject to retrieve their triples. Needless to say this is very inefficient and a bad idea in general.

A better alternative is write a query template and set the parameter(s) programmatically:

```auto
connection
          .select(query)
          .parameter("type", iri(ex, "MyClass"))
          .execute();

```

where `query` looks like

```auto
select ?s ?p ?o where{
    ?s ?p ?o.
    ?s a ?type
}

```

Best,  
Evren

---

<div class="post-metadata">

### Author: ![doggyfood](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/doggyfood/32/106_2.png) [@doggyfood](https://community.stardog.com/u/doggyfood)
#### Post date: [June 22, 2017, 2:03pm UTC](https://community.stardog.com/t/how-to-use-the-java-api-to-make-a-simple-query/421/3 "2017-06-22T14:03:30Z")

</div>

Agree.

What I do is to use a

`final static String QUERY_GETTER = "select ?s ?p ?o....";`

and then use `.parameter()` to configure the values. Note that they have to be inside the `WHERE` clause, otherwise you need to use `BIND`.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/2X/e/ed66b48a616f505106a4acde3c9cee7e6da9bc67.svg) [@system](https://community.stardog.com/u/system)
#### Post date: [July 6, 2017, 2:04pm UTC](https://community.stardog.com/t/how-to-use-the-java-api-to-make-a-simple-query/421/4 "2017-07-06T14:04:55Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
