// Prefix mapping just helps serialization
query.getPrefixMapping().setNsPrefix("dc" , DC.getURI()) ;
query.serialize(new IndentedWriter(System.out,true)) ;
System.out.println() ;
QueryExecution qexec = QueryExecutionFactory.create(query, model) ;
try {
// Assumption: it's a SELECT query.
ResultSet rs = qexec.execSelect() ;
// The order of results is undefined.
System.out.println("Titles: ") ;
for ( ; rs.hasNext() ; )
{
QuerySolution rb = rs.nextSolution() ;
// Get title - variable names do not include the '?' (or '$')
RDFNode x = rb.get("title") ;
// Check the type of the result value
if ( x instanceof Literal )
{
Literal titleStr = (Literal)x ;
System.out.println(" "+titleStr) ;
}
else
System.out.println("Strange - not a literal: "+x) ;
}
}
finally
{
// QueryExecution objects should be closed to free any system resources
qexec.close() ;
}
}