I tried getting RDF results of a query through JAVA. I used the
jdbc example occurring in the Virtuoso documentation, but Literals with
xml:lang specification return as java String.
The drivers I tested:
* recent virtjdbc3.jar (ftp'd just now)
OpenLink Virtuoso(TM) Driver for JDBC(TM) Version 3.x [Build 3.35]
* The virtjdbc3.jar that came with the 6.0 windows binary installation:
OpenLink Virtuoso(TM) Driver for JDBC(TM) Version 3.x [Build
3.27/06.00.3039]
The virtuoso version is 6.0.0-pre2.3118-threads as of Apr 23 2009
(downloaded from virtuoso site)
My query is:
"sparql SELECT ?l ?u where {?u skos:prefLabel
?l.FILTER(langmatches(lang(?l),\"nl\"))} LIMIT 10";
I get results:
"Populaire" genres en vormen
<http://www.oclc.org/nl/kar/Mp>
I did have to specify:-Djdbc.drivers=virtuoso.jdbc3.Driver in java as
otherwise
the driver was not found. The
Object o = ((VirtuosoResultSet)rs).getObject(i);
returns a java String for Literals, apparently even if the literal has
an xml:lang attribute.
Am I missing something?
Any advice? Should I use the higher level interfaces to java?
=================
The code:
import java.sql.*;
import virtuoso.jdbc3.*;
public class TestVirtuosoRDFBare {
public static void main(String[] args) throws SQLException {
String connectionURL =
"jdbc:virtuoso://localhost:1111/UID=demo/PWD=demo";
Connection connection = DriverManager.getConnection(connectionURL);
Statement stmt = connection.createStatement();
String query = "sparql SELECT ?l ?u where {?u skos:prefLabel
?l.FILTER(langmatches(lang(?l),\"nl\"))} LIMIT 10";
boolean more = stmt.execute(query);
ResultSetMetaData data = stmt.getResultSet().getMetaData();
while (more) {
ResultSet rs = stmt.getResultSet();
while(rs.next()){
for(int i = 1;i <= data.getColumnCount();i++) {
String s = rs.getString(i);
Object o = ((VirtuosoResultSet)rs).getObject(i);
if (o instanceof VirtuosoExtendedString) // String
representing an IRI
{
VirtuosoExtendedString vs =
(VirtuosoExtendedString) o;
if (vs.iriType == VirtuosoExtendedString.IRI)
System.out.println ("<" + vs.str +">");
else if (vs.iriType == VirtuosoExtendedString.BNODE)
System.out.println ("<" + vs.str +">");
}
else if (o instanceof VirtuosoRdfBox) // Typed literal
{
VirtuosoRdfBox rb = (VirtuosoRdfBox) o;
System.out.println (rb.rb_box + " lang=" +
rb.getLang() + " type=" + rb.getType());
}
else if(stmt.getResultSet().wasNull())
System.out.println("NULL");
else {
System.out.println(s);
}
}
}
more = stmt.getMoreResults();
}
connection.close();
}
}