It took some copying and pasting to see what exactly was going on. I would suggest that you add new lines to your query for readability. The query you are using is:
PREFIX dbres: <http://dbpedia.org/resource/> SELECT * WHERE { dbres:??? <http://www.w3.org/2000/01/rdf-schema
where ??? replaced by the contents of the entity string. You do absolutely no input check here to ensure that the entity value is legal for the insert. Based on your question, it looks like entity contains William_H._Miller_(writer) , so you get a request:
PREFIX dbres: <http://dbpedia.org/resource/> SELECT * WHERE { dbres:William_H._Miller_(writer) <http://www.w3.org/2000/01/rdf-schema
You can paste this into the DBpedia public endpoint and you will get a similar parsing error message:
Virtuoso 37000 Error SP030: SPARQL compiler, line 6: syntax error at 'writer' before ')' SPARQL query: define sql:big-data-const 0 #output-format:text/html define sql:signal-void-variables 1 define input:default-graph-uri <http://dbpedia.org> PREFIX dbres: <http://dbpedia.org/resource/> SELECT * WHERE { dbres:William_H._Miller_(writer) <http://www.w3.org/2000/01/rdf-schema#label> ?o FILTER (langMatches(lang(?o),"en")) }
Better than hitting the DBpedia endpoint with bad queries, you can also use the SPARQL query validation mechanism that reports for this query:
Syntax error: Lexical error in row 4, column 34. Found: ")" (41), after: "writer"
In Jena, you can use ParameterizedSparqlString to avoid such problems. Here is your example, redesigned to use a parameterized string:
import com.hp.hpl.jena.query.ParameterizedSparqlString; public class PSSExample { public static void main( String[] args ) {
Output:
PREFIX dbres: <http://dbpedia.org/resource/> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema
You can run this query on a public endpoint and get the expected results . Please note that if you use an entity that does not need special escaping, for example,
final String entity = "George_Washington";
then the query output will use the prefix form:
PREFIX dbres: <http://dbpedia.org/resource/> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema
This is very convenient because you do not need to check if your suffix, i.e. entity , any characters that need to be escaped; Jena takes care of this for you.