title: parameterized SPARQL Query
body: <p>I want to code a parameterized SPARQL Query in Java Jena where one of the triple in query be injected </p>
  
  <p>so in the code below I need to inject value as a string the pass to the class </p>
  
  <p>However, the SPARQL query is correct so when I replace "value" with the class name I got the right result</p>
  
  <p>I tried two code non of them worked No result  or run time error </p>
  
  <p>the first code:</p>
  
  <pre><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:&lt;http://www.w3.org/2000/01/rdf-schema#&gt;"+ 
                  "PREFIX rdf:&lt; http://www.w3.org/1999/02/22-rdf-syntax-ns#&gt;"+
                  "PREFIX HASO:&lt; http://www.semanticweb.org/rabaa006/ontologies/2014/4/HASO#&gt;"+
  
                   "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();}
  
  }
  }
  </code></pre>
  
  <p>The Second code:</p>
  
  <pre><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:&lt;http://www.w3.org/2000/01/rdf-schema#&gt;"+ 
  
                         "PREFIX rdf:&lt; http://www.w3.org/1999/02/22-rdf-syntax-ns#&gt;"+
  
                         "PREFIX HASO:&lt; http://www.semanticweb.org/rabaa006/ontologies/2014/4/HASO#&gt;"+
  
  
                   "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();}
   }
  }
  </code></pre>