# Running Sparql queries against SCB Graph

The following example executes a Sparql querry against an SCB Graph using the Jena Façade.

Comment: Be sure that the Graph "http://localhost/" exists and contains the appropriate triples for the query. If you need information how to store or retrieve triples using the Smart Content Binding read the tutorial exploring and aggregating RDF data with SCB.

    import org.apache.clerezza.rdf.core.MGraph;
    import org.apache.clerezza.rdf.core.UriRef;
    import org.apache.clerezza.rdf.core.access.TcManager;
    import org.apache.clerezza.rdf.jena.facade.JenaGraph;

    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.rdf.model.ModelFactory;
    import com.hp.hpl.jena.rdf.model.RDFNode;

    public class SparqlDemo {

    	public static void main(String... args) throws Exception {

    		TcManager tcManager = TcManager.getInstance();
    		UriRef mGraphName = new UriRef("http://localhost/");
    		String itemUri = "http://dannyayers.com/2004/05/15/rasqal-rdf-query-library/";
    		MGraph mGraph = tcManager.getMGraph(mGraphName);
    		JenaGraph jenaGraph = new JenaGraph(mGraph);
    		Model model = ModelFactory.createModelForGraph(jenaGraph);

    		String queryString = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>"
    				+ "PREFIX rss: <http://purl.org/rss/1.0/> "
    				+ "PREFIX dc: <http://purl.org/dc/elements/1.1/> "
    				+ "SELECT  ?p ?o WHERE {"
    				+ " <" + itemUri + "> ?p ?o " + " }";
    		Query query = QueryFactory.create(queryString);
    		QueryExecution qexec = QueryExecutionFactory.create(query, model);
    		int i = 1;
    		try {
    			ResultSet results = qexec.execSelect();
    			for (; results.hasNext();) {
    				QuerySolution soln = results.nextSolution();
    				RDFNode p = soln.get("p");
    				RDFNode o = soln.get("o"); // Get a result variable by name.
    				System.out.println(i++ + "." + p + ", " + o);
    			}
    		} finally {
    			qexec.close();
    		}

    	}
    }