2013-10-12 107 views
0

如果我想通過Netbeans在Sesame存儲庫中執行SPARQL SELECT查詢,我使用以下代碼(並在綁定集中獲得三個值)。我怎樣才能做一個只返回一個布爾值的ASK查詢?如何執行ASK查詢?

TupleQuery tupleQuery = con.prepareTupleQuery(QueryLanguage.SPARQL, queryString); 
TupleQueryResult result = tupleQuery.evaluate(); 
List<String> bindingNames = result.getBindingNames(); 

try { 
    while (result.hasNext()) {  
     BindingSet bindingSet = result.next(); 
     Value firstValue1 = bindingSet.getValue(bindingNames.get(0)); 
     Value firstValue2 = bindingSet.getValue(bindingNames.get(1)); 
     Value firstValue3 = bindingSet.getValue(bindingNames.get(2)); 
    } 
} 
+0

代碼還要求,在http://answers.semanticweb.com/questions/24793/how-to-perfom-sparql-ask-query-in-sesame-回答repository-through-netbeans –

回答

3

假設你conRepositoryConnection,你會使用的一些prepareBooleanQuery(…)版本。這將返回一個BooleanQueryevaluate()方法返回一個布爾值。 Modying你提出會離開你

BooleanQuery booleanQuery = con.prepareBoleanQuery(QueryLanguage.SPARQL, queryString); 
boolean result = booleanQuery.evaluate(); 
+0

謝謝。它解決了我的問題。這裏是代碼: \t BooleanQuery bq = con.prepareBooleanQuery(QueryLanguage.SPARQL,queryString); try { \t boolean b = bq.evaluate(); \t System.out.println(b); \t} {終於 \t} – Vijender

+0

@vijendra如果你不'catch'ing什麼,你不是在'finally'塊做什麼,爲什麼要使用'嘗試{...}終於{...}'在所有? –