Discussion:
Paramerized SPARQL Query
rana abaalkhail
2017-03-21 19:43:02 UTC
Permalink
Hi

I want to code a parameterized SPARQL Query in Java Jena where one of the triple in query be injected

so in the code below I need to inject value as a string the pass to the class


However, the SPARQL query is correct so when I replace "value" with the class name I got the right result

I tried two code non of them worked No result or run time error


the first code:



package ontology;







import org.apache.jena.iri.impl.Main;



import com.hp.hpl.jena.query.ParameterizedSparqlString;

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.QuerySolution;

import com.hp.hpl.jena.query.ResultSet;

import com.hp.hpl.jena.rdf.model.Model;

import com.hp.hpl.jena.util.FileManager;



public class SPARQL {









public static void sparqlTest( String str)

{



FileManager.get().addLocatorClassLoader(Main.class.getClassLoader());

Model model=FileManager.get().loadModel("ASO.owl");







String queryString=



"PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>"+

"PREFIX rdf:< http://www.w3.org/1999/02/22-rdf-syntax-ns#>"+

"PREFIX HASO:< http://www.semanticweb.org/rabaa006/ontologies/2014/4/HASO#>"+





"SELECT ?x "+

"WHERE"+

" {?x rdfs:subClassOf HASO:Affective_State}";





ParameterizedSparqlString queryStr = new ParameterizedSparqlString(queryString);

queryStr.setLiteral("value", str);



Query query=QueryFactory.create(queryStr.toString());



QueryExecution qexec = QueryExecutionFactory.create(query,model);





try {



ResultSet results = qexec.execSelect();





while ( results.hasNext()){



QuerySolution soln = results.nextSolution();

String strg=soln.getResource("?x").toString();

//System.out.println(strg);

String number = strg.substring(strg.lastIndexOf("#") + 1);

System.out.println(number);



}}

finally{



qexec.close();}





}



}



The Second code:


package ontology;







import org.apache.jena.iri.impl.Main;



import com.hp.hpl.jena.query.ParameterizedSparqlString;

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.QuerySolution;

import com.hp.hpl.jena.query.ResultSet;

import com.hp.hpl.jena.rdf.model.Model;

import com.hp.hpl.jena.util.FileManager;



public class SPARQL {









public static void sparqlTest( String str)

{



FileManager.get().addLocatorClassLoader(Main.class.getClassLoader());

Model model=FileManager.get().loadModel("ASO.owl");







ParameterizedSparqlString pss = new ParameterizedSparqlString();

pss.setCommandText (

"PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>"+

"PREFIX rdf:< http://www.w3.org/1999/02/22-rdf-syntax-ns#>"+

"PREFIX HASO:< http://www.semanticweb.org/rabaa006/ontologies/2014/4/HASO#>"+





"SELECT ?x "+

"WHERE"+

" {?x rdfs:subClassOf HASO:valuee}");

pss.setLiteral("value", str);







Query query=QueryFactory.create(pss.toString());





QueryExecution qexec = QueryExecutionFactory.create(query,model);





try {



ResultSet results = qexec.execSelect();





while ( results.hasNext()){



QuerySolution soln = results.nextSolution();

String strg=soln.getResource("?x").toString();

//System.out.println(strg);

String number = strg.substring(strg.lastIndexOf("#") + 1);

System.out.println(number);



}}

finally{



qexec.close();}





}



}

Loading...