2011-03-11 81 views
1

我的代碼:如何產生是我爲sparql執行寫的代碼是否正確?

package sample; 

import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.InputStream; 

import com.hp.hpl.jena.query.Query; 
import com.hp.hpl.jena.query.QueryExecution; 
import com.hp.hpl.jena.query.QueryExecutionFactory; 
import com.hp.hpl.jena.query.QueryFactory; 
import com.hp.hpl.jena.query.ResultSet; 
import com.hp.hpl.jena.query.ResultSetFormatter; 
import com.hp.hpl.jena.rdf.model.Model; 
import com.hp.hpl.jena.rdf.model.ModelFactory; 

public class QueryTest { 

public static void main(String[] args) throws IOException { 
    InputStream in = new FileInputStream(new File("foaf-ijd.rdf")); 

    Model model = ModelFactory.createMemModelMaker().createDefaultModel(); 

    model.read(in, null); 
    in.close(); 

    String queryString = "SELECT ?x WHERE (?x, <http://www.w3.org/2001/vcard-rdf/3.0#FN>, 'John Smith')"; 

    Query query = QueryFactory.create(queryString); 

    QueryExecution qe = QueryExecutionFactory.create(query, model); 
    ResultSet results = qe.execSelect(); 

    ResultSetFormatter.out(System.out, results, query); 

    qe.close(); 

} 

} 

錯誤

Exception in thread "main" java.lang.NoSuchMethodError: org.slf4j.Logger.trace(Ljava/lang/String;)V 
    at com.hp.hpl.jena.sparql.lib.SystemUtils.chooseClassLoader(SystemUtils.java:23) 
    at com.hp.hpl.jena.sparql.lib.Metadata.init(Metadata.java:45) 
    at com.hp.hpl.jena.sparql.lib.Metadata.get(Metadata.java:75) 
    at com.hp.hpl.jena.query.ARQ.<clinit>(ARQ.java:253) 
    at com.hp.hpl.jena.query.Query.<clinit>(Query.java:54) 
    at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:71) 
    at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:43) 
    at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:31) 
    at sample.QueryTest.main(QueryTest.java:29) 

*

回答

2

你的代碼看起來OK,你得到來自於耶拿使用的庫中的一個錯誤。您使用哪個版本的耶拿?你有沒有嘗試過最新版本?您是否確保Jena下載的lib/目錄中的所有.jar位於您的CLASSPATH上?如果是這樣,是否檢查過以確保您的CLASSPATH上沒有多個衝突版本的Jena(或slf4j-*.jar)?

1

SPARQL查詢是不對的,你應該使用 '{',而不是 '(' 和沒有逗號:

String queryString = "SELECT ?x WHERE { ?x <http://www.w3.org/2001/vcard-rdf/3.0#FN> \"John Smith\"}"; 
相關問題