[英]sparql query code not working in jena-java
我在jena-fuseki-1.1.1中測試了這個sparql查詢,它正在工作,但是當我在jena java項目中使用時,它沒有給我output.inside,而循環沒有執行。
private static void sparqlTst() {
FileManager.get().addLocatorClassLoader(Jena_Enigma.class.getClassLoader());
Model model = FileManager.get().loadModel("http://www.heshjayasinghe.webatu.com/Enigma.RDF");
model.write(System.out, "RDF/JSON");
String queryString = "PREFIX sep: <http://www.heshjayasinghe.webatu.com/Enigma.RDF#>"
+ "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"
+ "PREFIX strg: <http://www.w3.org/2001/XMLSchema#string#>"
+ "SELECT ?first "
+ "WHERE {"
+ "?User sep:email \"heshjse@gmail.com\"."
+ "?User sep:password \"123\"."
+ "?User sep:fname ?first. "
+ "}";
Query query = QueryFactory.create(queryString);
// System.out.println(query);
QueryExecution qexec = QueryExecutionFactory.create(query, model);
try {
ResultSet results = qexec.execSelect();
System.out.println(results);
while (results.hasNext()) {
System.out.println("ok");
QuerySolution soln = results.nextSolution();
System.out.println(soln);
Literal name = soln.getLiteral("x");
System.out.println(name);
System.err.printf("X is '%s'\n", soln.getLiteral("first"));
}
} finally {
qexec.close();
}
}
正確答案
private static void sparqlTst() {
FileManager.get().addLocatorClassLoader(Jena_Enigma.class.getClassLoader());
Model model = FileManager.get().loadModel("http://www.heshjayasinghe.webatu.com/Enigma.RDF");
model.write(System.out, "RDF/JSON");
String queryString = "PREFIX sep: <http://www.heshjayasinghe.webatu.com/Enigma.RDF#>"
+ "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"
+ "PREFIX strg: <http://www.w3.org/2001/XMLSchema#string#>"
+ "SELECT ?x "
+ "WHERE {"
+ "?User sep:email \"heshjse@gmail.com\"."
+ "?User sep:password \"123\"."
+ "?User sep:fname ?x. "
+ "}";
Query query = QueryFactory.create(queryString);
// System.out.println(query);
QueryExecution qexec = QueryExecutionFactory.create(query, model);
try {
ResultSet results = qexec.execSelect();
System.out.println(results);
while (results.hasNext()) {
QuerySolution soln = results.nextSolution();
Literal name = soln.getLiteral("x");
System.out.println(name);
}
} finally {
qexec.close();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.