0

I am trying to query an open RDF repository which is loaded with a turtle file. When I am selecting all with a query - "SELECT ?s WHERE { ?s ?p ?o } "; then everything is working fine but when I am using a little complex query then its not working. I am attaching the code for the query portion -

private static void queryingRDF(Repository repo) {
    try{
        RepositoryConnection con = repo.getConnection();
        try{
            String queryString = "SELECT ?s  WHERE { ?s uml:lineOfBusiness cp:lobEQUITIES .}" ;
              TupleQuery tupleQuery = con.prepareTupleQuery(QueryLanguage.SPARQL, queryString);

              TupleQueryResult result = tupleQuery.evaluate();
              try {
                   while(result.hasNext()){
                        BindingSet bindingSet = result.next();
                        Value valueOfX = bindingSet.getValue("s");
                        //Value valueOfY = bindingSet.getValue("p");
                        //Value valueOfZ = bindingSet.getValue("o");
                        //System.out.println(valueOfX + "   " + valueOfY + "   " + valueOfZ);
                        System.out.println(valueOfX) ;
                   }

              }

              finally {
                  result.close();
              }
        }
        finally{
            con.close();
        }
    }
    catch(OpenRDFException e){
        System.out.println("Query error");
    }

}

This is continuously getting into the exception section and throwing the error - "Query Error". Whats is going wrong?

Som Sarkar
  • 289
  • 1
  • 5
  • 24
  • Just a tip: in debugging this kind of problem, don't just ignore the actual exception. Instead, inspect its stacktrace, for example by printing it out: `e.printStackTrace();`, or at the very least look at the exception message: `System.out.println(e.getMessage());`. The precise message and stacktrace often contain helpful information about what exactly went wrong. – Jeen Broekstra May 16 '14 at 16:45

1 Answers1

2

It looks like you don't define the uml or cp prefixes. I'm looking for a statement like this in your querystring:

PREFIX uml: <http://example.com/uml>

or a call to RepositoryConnection::setNamespace like this:

con.setNamespace("uml", "http://example.com/uml");
Paul Butcher
  • 6,902
  • 28
  • 39
  • 1
    The call to `setNamespace` won't solve the problem. The SPARQL query needs to be syntactically complete, which means the prefix declaration needs to be added to the query. – Jeen Broekstra May 16 '14 at 16:42
  • Thanks @JeenBroekstra. I've not used this Java library before, and I assumed that this was a way to set namespaces outside of a the actual query. – Paul Butcher May 20 '14 at 09:04