Search code examples
javasparqljenadbpedia

Select wiki page for any DBpedia resource


I write a simple java code to retrieve Wikipedia page for any DBpedia resource

public static void main(String[] args)throws Exception {
    String service = "http://dbpedia.org/sparql";
    String query = "SELECT ?x WHERE {"
                + "?x foaf:primaryTopic <http://dbpedia.org/resource/France>";
    QueryExecution qe = QueryExecutionFactory.sparqlService(service, query);
            try{
                ResultSet results = qe.execSelect();
                for (; results.hasNext();){
                    QuerySolution sol = (QuerySolution) results.next();
                    String answer = sol.get("?x").toString();
                    System.out.println(answer);

                }
            }
            catch(Exception e){
                System.out.println(e.getMessage());
            }
            finally{
                qe.close();
            }

but I have No results!! I've tried the sparql query and it works fine so I've no idea what's wrong SPARQL Result any suggestions?


Solution

  • Well, I forgot to put the declaration for foaf when I correct it, it works just fine. I just edit my query and add the foaf declaration as following:

    String query = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>"
                + "SELECT  ?x WHERE {" 
                    + "?x foaf:primaryTopic <http://dbpedia.org/resource/France>}";
    

    Thanks to @joshua-taylor for your point