How to retrieve a column's value in sparql
Ineed to retrieve a column's value once the query is executed. The query runs and gives me the correct answer in the Eclipse console. I need to just retrieve one of the attribute value from the row. The Complete code is stored in SemanticSearch.java. This function is called from Admin.java.
public int searchForUser(String userName, String password)
{
String prolog="PREFIX kb:<"+VUSER.getURI()+">";
System.out.println("Search for user in semantic search.java");
String queryString1=prolog
+"\n" +"SELECT interest "
+WHERE {?x kb:Uname ?username. ?x kb:Pa开发者_运维技巧ssword ?password. ?x kb:Interest ?interest. "
+"FILTER regex(?username, \"" +userName +"\" )}";
System.out.println(queryString1);
Query query=QueryFactory.create(queryString1);
QueryExecution qexec = QueryExecutionFactory.create(query, model);
ResultSet results1 = qexec.execSelect();
//System.out.println(results1);
//ResultSetFormatter.out(results1);
ResultSetFormatter.out(System.out, results1,query);
if(results1.getRowNumber()>0)
{
QuerySolution soln=results1.nextSolution();
Literal RES=soln.getLiteral("Interest");
String RES=results1.
int res=results1.getRowNumber();
System.out.println(RES);
return res;
}
else
{
return 0;
}
}
The Output is:
PREFIX kb:http://protege.stanford.edu/kb#
SELECT * WHERE {?x kb:Uname ?username. ?x kb:Password ?password. ?x kb:Interest ?interest. FILTER regex(?username, "anu" )}
| x | username | password | interest |
| kb:Anvika | "anu" | "anu" | "C language" |
After this error comes:
Feb 10, 2011 10:50:56 AM org.apache.catalina.core.StandardWrapperValve invoke SEVERE:
Servlet.service() for servlet Admin threw exception java.util.NoSuchElementException: QueryIteratorCloseable
at com.hp.hpl.jena.sparql.engine.iterator.QueryIteratorBase.nextBinding(QueryIteratorBase.java:93)
at com.hp.hpl.jena.sparql.engine.ResultSetStream.nextBinding(ResultSetStream.java:74)
at com.hp.hpl.jena.sparql.engine.ResultSetStream.nextSolution(ResultSetStream.java:91)
at semanticsearch.SemanticSearch.searchForUser(SemanticSearch.java:126)
at controller.Admin.doGet(Admin.java:84) at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)
I am not able to retrieve the value at a column. Help. Regards.
Ok so then my next suspicion would be that you've tried to iterate past the end of the iterator. Using the condition results1.getRowNumber()>0 is a very bad idea since it is always going to be > 0 as far as I can tell (though this isn't clear from the Javadoc for ARQ).
Instead I would replace the condition with results1.hasNext() and see if that fixes it
Redacted Original Answer
I suspect this is just a case of variable names being case sensitive.
Replace the line:
Literal RES=soln.getLiteral("Interest");
With the line:
Literal RES=soln.getLiteral("interest");
And I suspect it will work fine.
精彩评论