0

I'm working on an Apache Jena project. I've got a Fuseki server running on my localhost. I want to create a Java Program for my Fuseki server, that shows all the data in the triplestore in a JTable. I just have no idea how to parse the result from my query into a JTable.

My code sofar: (left out the part where the window, table, frame etc is created)

private void Go() {
    String query = "SELECT ?subject ?predicate ?object \n" +
                   "WHERE { \n" +
                   "?subject ?predicate ?object }";
    Query sparqlQuery = QueryFactory.create(query, Syntax.syntaxARQ) ;
    QueryEngineHTTP httpQuery = new QueryEngineHTTP("http://localhost:3030/AnimalDataSet/", sparqlQuery);

    ResultSet results = httpQuery.execSelect();
    System.out.println(ResultSetFormatter.asText(results));
    while (results.hasNext()) {
        QuerySolution solution = results.next();
    }
    httpQuery.close();
}

The sysout prints this, which is the correct data:

-------------------------------------------------------------------------------------------------------------------------------------
| subject                    | predicate                                         | object                                           |
=====================================================================================================================================
| <urn:animals:data>         | <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> | <http://www.w3.org/1999/02/22-rdf-syntax-ns#Seq> |
| <urn:animals:data>         | <http://www.w3.org/1999/02/22-rdf-syntax-ns#_1>   | <urn:animals:lion>                               |
| <urn:animals:data>         | <http://www.w3.org/1999/02/22-rdf-syntax-ns#_2>   | <urn:animals:tarantula>                          |
| <urn:animals:data>         | <http://www.w3.org/1999/02/22-rdf-syntax-ns#_3>   | <urn:animals:hippopotamus>                       |
| <urn:animals:lion>         | <http://www.some-ficticious-zoo.com/rdf#name>     | "Lion"                                           |
| <urn:animals:lion>         | <http://www.some-ficticious-zoo.com/rdf#species>  | "Panthera leo"                                   |
| <urn:animals:lion>         | <http://www.some-ficticious-zoo.com/rdf#class>    | "Mammal"                                         |
| <urn:animals:tarantula>    | <http://www.some-ficticious-zoo.com/rdf#name>     | "Tarantula"                                      |
| <urn:animals:tarantula>    | <http://www.some-ficticious-zoo.com/rdf#species>  | "Avicularia avicularia"                          |
| <urn:animals:tarantula>    | <http://www.some-ficticious-zoo.com/rdf#class>    | "Arachnid"                                       |
| <urn:animals:hippopotamus> | <http://www.some-ficticious-zoo.com/rdf#name>     | "Hippopotamus"                                   |
| <urn:animals:hippopotamus> | <http://www.some-ficticious-zoo.com/rdf#species>  | "Hippopotamus amphibius"                         |
| <urn:animals:hippopotamus> | <http://www.some-ficticious-zoo.com/rdf#class>    | "Mammal"                                         |
-------------------------------------------------------------------------------------------------------------------------------------

I really hope someone here knows how to parse the data from the query into a JTbale :D

Thanks in advance!

pnuts
  • 58,317
  • 11
  • 87
  • 139
  • A `JTable` is a UI control which you populate with data from some source. In this case you have a Jena `ResultSet` where each `QuerySolution` is a row in the query results so you just need to convert the data appropriately. I'd start with the official [How to Use Tables](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html) tutorial if you aren't already familiar with `JTable` – RobV Nov 25 '15 at 09:40

1 Answers1

1

I've done some further research and finally found the solution! It's quite easy actually.

You just simply change the while loop like this:

while(rs.hasNext())
    {
        QuerySolution sol = rs.nextSolution();
        RDFNode object = sol.get("object"); 
        RDFNode predicate = sol.get("predicate"); 
        RDFNode subject = sol.get("subject");

        DefaultTableModel model = (DefaultTableModel) table.getModel();
        model.addRow(new Object[]{subject, predicate, object});
    }

And that works fine for me!

For everyone who's interested, i've puplished my version as it is now to pastebin which has comments: The link to the full (current) version of my project