1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| public class Sparql {
public Vector Query (){
URL url;
Vector v3=new Vector();
try{
String queryString = "SELECT ?nom ?label ?comment WHERE { " +
"?nom owl:sameAs m.getName()" +
"?label owl:sameAs m.getLabel()" +
"?comment owl:sameAs m.getComment" +
" FILTER langMatches(lang(?nom), 'en') " +
" FILTER langMatches(lang(?label), 'en') " +
" FILTER langMatches(lang(?comment), 'en')}";
Query query = QueryFactory.create(queryString);
// initializing queryExecution factory with remote service.
// **this actually was the main problem I couldn't figure out.**
QueryExecution qexec = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", query);
try {
ResultSet results = qexec.execSelect();
for (; results.hasNext();) {
QuerySolution sol = results.next();
v3.addElement(sol);
System.out.println(sol.get("?nom") + "\n"+ sol.get("?label") + "\n" + sol.get("?comment"));
}
}catch(Exception e){
e.printStackTrace();
}
finally {
qexec.close();
}
}catch(Exception e){
e.printStackTrace();
}
return v3;
}
} |
Partager