Discussion:
Use variable in SPARQL query
tina sani
2016-11-01 16:01:42 UTC
Permalink
Hello.

How can I use a Java variable in the SPARQL query.
?emp rdf:type ?SomeVariable

I want rdf:type of Managers and Technicals while these two values(Managers
and Technicals) are in my Java variable: employee.

So I need something like:

?emp rdf:type "employee"

Regards
lookman sanni
2016-11-01 16:13:13 UTC
Permalink
Hi Tina,

You can have your query stored in a composed java string (e.g. String query
= "Select blablabla where { ?emp rdf:type " + someJavaVariable + "}"

Hope that helps
Post by tina sani
Hello.
How can I use a Java variable in the SPARQL query.
?emp rdf:type ?SomeVariable
I want rdf:type of Managers and Technicals while these two values(Managers
and Technicals) are in my Java variable: employee.
?emp rdf:type "employee"
Regards
Martynas Jusevičius
2016-11-01 16:16:48 UTC
Permalink
A safer way would be to use ParameterizedSparqlString:
https://jena.apache.org/documentation/javadoc/arq/org/apache/jena/query/ParameterizedSparqlString.html
Post by lookman sanni
Hi Tina,
You can have your query stored in a composed java string (e.g. String query
= "Select blablabla where { ?emp rdf:type " + someJavaVariable + "}"
Hope that helps
Post by tina sani
Hello.
How can I use a Java variable in the SPARQL query.
?emp rdf:type ?SomeVariable
I want rdf:type of Managers and Technicals while these two values(Managers
and Technicals) are in my Java variable: employee.
?emp rdf:type "employee"
Regards
tina sani
2016-11-01 16:54:02 UTC
Permalink
Will simply putting a variable inside a query work?

{?emp rdf:type "+employee+"}";
Post by lookman sanni
Hi Tina,
You can have your query stored in a composed java string (e.g. String query
= "Select blablabla where { ?emp rdf:type " + someJavaVariable + "}"
Hope that helps
Post by tina sani
Hello.
How can I use a Java variable in the SPARQL query.
?emp rdf:type ?SomeVariable
I want rdf:type of Managers and Technicals while these two
values(Managers
Post by tina sani
and Technicals) are in my Java variable: employee.
?emp rdf:type "employee"
Regards
Andy Seaborne
2016-11-01 17:21:18 UTC
Permalink
Try and see what happens.

It won't work but you can the see the results SPARQL syntax error.

Andy
Post by tina sani
Will simply putting a variable inside a query work?
{?emp rdf:type "+employee+"}";
Post by lookman sanni
Hi Tina,
You can have your query stored in a composed java string (e.g. String query
= "Select blablabla where { ?emp rdf:type " + someJavaVariable + "}"
Hope that helps
Post by tina sani
Hello.
How can I use a Java variable in the SPARQL query.
?emp rdf:type ?SomeVariable
I want rdf:type of Managers and Technicals while these two
values(Managers
Post by tina sani
and Technicals) are in my Java variable: employee.
?emp rdf:type "employee"
Regards
Claude Warren
2016-11-01 19:34:17 UTC
Permalink
You can do the substitution quite easily using the querybuilder package.
It will let you build a query like

?emp rdf:type ?empType

and then bind ?empType to your java variable value.

Claude
Post by Andy Seaborne
Try and see what happens.
It won't work but you can the see the results SPARQL syntax error.
Andy
Post by tina sani
Will simply putting a variable inside a query work?
{?emp rdf:type "+employee+"}";
Hi Tina,
Post by lookman sanni
You can have your query stored in a composed java string (e.g. String query
= "Select blablabla where { ?emp rdf:type " + someJavaVariable + "}"
Hope that helps
Hello.
Post by tina sani
How can I use a Java variable in the SPARQL query.
?emp rdf:type ?SomeVariable
I want rdf:type of Managers and Technicals while these two
values(Managers
Post by tina sani
and Technicals) are in my Java variable: employee.
?emp rdf:type "employee"
Regards
--
I like: Like Like - The likeliest place on the web
<http://like-like.xenei.com>
LinkedIn: http://www.linkedin.com/in/claudewarren
Lorenz B.
2016-11-02 08:48:26 UTC
Permalink
It should be clear that String concatenation calls the toString() method
of your object. We don't know how this is implemented in your code though.
Post by tina sani
Will simply putting a variable inside a query work?
{?emp rdf:type "+employee+"}";
Post by lookman sanni
Hi Tina,
You can have your query stored in a composed java string (e.g. String query
= "Select blablabla where { ?emp rdf:type " + someJavaVariable + "}"
Hope that helps
Post by tina sani
Hello.
How can I use a Java variable in the SPARQL query.
?emp rdf:type ?SomeVariable
I want rdf:type of Managers and Technicals while these two
values(Managers
Post by tina sani
and Technicals) are in my Java variable: employee.
?emp rdf:type "employee"
Regards
--
Lorenz BÃŒhmann
AKSW group, University of Leipzig
Group: http://aksw.org - semantic web research center
tina sani
2016-11-02 11:00:44 UTC
Permalink
Lorenz, this does not work for me:

Select * where{?emp rdf:type "+employee+"}";

employee variable is : String employee; have values Manager or Technical
depend on user input.

On Wed, Nov 2, 2016 at 10:48 AM, Lorenz B. <
Post by Lorenz B.
It should be clear that String concatenation calls the toString() method
of your object. We don't know how this is implemented in your code though.
Post by tina sani
Will simply putting a variable inside a query work?
{?emp rdf:type "+employee+"}";
Post by lookman sanni
Hi Tina,
You can have your query stored in a composed java string (e.g. String
query
Post by tina sani
Post by lookman sanni
= "Select blablabla where { ?emp rdf:type " + someJavaVariable + "}"
Hope that helps
Post by tina sani
Hello.
How can I use a Java variable in the SPARQL query.
?emp rdf:type ?SomeVariable
I want rdf:type of Managers and Technicals while these two
values(Managers
Post by tina sani
and Technicals) are in my Java variable: employee.
?emp rdf:type "employee"
Regards
--
Lorenz BÃŒhmann
AKSW group, University of Leipzig
Group: http://aksw.org - semantic web research center
Lorenz B.
2016-11-02 11:02:53 UTC
Permalink
WHAT is a concrete value of the variable 'employee'?

And, full URIs have to be put between '<' and '>'
Post by tina sani
Select * where{?emp rdf:type "+employee+"}";
employee variable is : String employee; have values Manager or Technical
depend on user input.
On Wed, Nov 2, 2016 at 10:48 AM, Lorenz B. <
Post by Lorenz B.
It should be clear that String concatenation calls the toString() method
of your object. We don't know how this is implemented in your code though.
Post by tina sani
Will simply putting a variable inside a query work?
{?emp rdf:type "+employee+"}";
Post by lookman sanni
Hi Tina,
You can have your query stored in a composed java string (e.g. String
query
Post by tina sani
Post by lookman sanni
= "Select blablabla where { ?emp rdf:type " + someJavaVariable + "}"
Hope that helps
Post by tina sani
Hello.
How can I use a Java variable in the SPARQL query.
?emp rdf:type ?SomeVariable
I want rdf:type of Managers and Technicals while these two
values(Managers
Post by tina sani
and Technicals) are in my Java variable: employee.
?emp rdf:type "employee"
Regards
--
Lorenz BÃŒhmann
AKSW group, University of Leipzig
Group: http://aksw.org - semantic web research center
--
Lorenz BÃŒhmann
AKSW group, University of Leipzig
Group: http://aksw.org - semantic web research center
tina sani
2016-11-02 14:05:40 UTC
Permalink
The value of employee is either Manager or Technical. When user enter
Manager, I want the query to show instances of Manager class and when
Technical, it will show instances of Technical class.
And yes, I have used all the prefixes with full URI in my query.

On Wed, Nov 2, 2016 at 1:02 PM, Lorenz B. <
Post by Lorenz B.
WHAT is a concrete value of the variable 'employee'?
And, full URIs have to be put between '<' and '>'
Post by tina sani
Select * where{?emp rdf:type "+employee+"}";
employee variable is : String employee; have values Manager or Technical
depend on user input.
On Wed, Nov 2, 2016 at 10:48 AM, Lorenz B. <
Post by Lorenz B.
It should be clear that String concatenation calls the toString() method
of your object. We don't know how this is implemented in your code
though.
Post by tina sani
Post by Lorenz B.
Post by tina sani
Will simply putting a variable inside a query work?
{?emp rdf:type "+employee+"}";
Post by lookman sanni
Hi Tina,
You can have your query stored in a composed java string (e.g. String
query
Post by tina sani
Post by lookman sanni
= "Select blablabla where { ?emp rdf:type " + someJavaVariable + "}"
Hope that helps
Post by tina sani
Hello.
How can I use a Java variable in the SPARQL query.
?emp rdf:type ?SomeVariable
I want rdf:type of Managers and Technicals while these two
values(Managers
Post by tina sani
and Technicals) are in my Java variable: employee.
?emp rdf:type "employee"
Regards
--
Lorenz BÃŒhmann
AKSW group, University of Leipzig
Group: http://aksw.org - semantic web research center
--
Lorenz BÃŒhmann
AKSW group, University of Leipzig
Group: http://aksw.org - semantic web research center
Lorenz Buehmann
2016-11-02 21:24:30 UTC
Permalink
Post by tina sani
The value of employee is either Manager or Technical. When user enter
Manager, I want the query to show instances of Manager class and when
Technical, it will show instances of Technical class.
That is not a useful answer to my response.Please learn to provide
detailed information. What is the concrete value?

Is it the String "Manager"? Like

String employee = "Manager";

If so, your SPARQL query expects the URI of a class, do you understand
what I mean?

"Manager" is not a URI. And as I said, if you have the URI, you need to
put this in between '<' and '>'
Post by tina sani
And yes, I have used all the prefixes with full URI in my query.
I don't understand what you're saying here. Either you use full URIs or
you use prefixed URIs and add the prefixes in the beginning of the
SPARQL query.
Post by tina sani
On Wed, Nov 2, 2016 at 1:02 PM, Lorenz B. <
Post by Lorenz B.
WHAT is a concrete value of the variable 'employee'?
And, full URIs have to be put between '<' and '>'
Post by tina sani
Select * where{?emp rdf:type "+employee+"}";
employee variable is : String employee; have values Manager or Technical
depend on user input.
On Wed, Nov 2, 2016 at 10:48 AM, Lorenz B. <
Post by Lorenz B.
It should be clear that String concatenation calls the toString() method
of your object. We don't know how this is implemented in your code
though.
Post by tina sani
Post by Lorenz B.
Post by tina sani
Will simply putting a variable inside a query work?
{?emp rdf:type "+employee+"}";
Post by lookman sanni
Hi Tina,
You can have your query stored in a composed java string (e.g. String
query
Post by tina sani
Post by lookman sanni
= "Select blablabla where { ?emp rdf:type " + someJavaVariable + "}"
Hope that helps
Post by tina sani
Hello.
How can I use a Java variable in the SPARQL query.
?emp rdf:type ?SomeVariable
I want rdf:type of Managers and Technicals while these two
values(Managers
Post by tina sani
and Technicals) are in my Java variable: employee.
?emp rdf:type "employee"
Regards
--
Lorenz Bühmann
AKSW group, University of Leipzig
Group: http://aksw.org - semantic web research center
--
Lorenz Bühmann
AKSW group, University of Leipzig
Group: http://aksw.org - semantic web research center
Loading...