Advertisement
Guest User

ApacheJenaAndFusekiExample

a guest
Nov 25th, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.29 KB | None | 0 0
  1. package main;
  2.  
  3. import java.awt.BorderLayout;
  4. import javax.swing.JButton;
  5. import javax.swing.JFrame;
  6. import javax.swing.JTable;
  7. import javax.swing.JToolBar;
  8. import javax.swing.UIManager;
  9. import javax.swing.UnsupportedLookAndFeelException;
  10. import javax.swing.table.DefaultTableModel;
  11. import org.apache.jena.query.QueryExecutionFactory;
  12. import org.apache.jena.query.QuerySolution;
  13. import org.apache.jena.query.ResultSet;
  14. import org.apache.jena.rdf.model.RDFNode;
  15. import javax.swing.JScrollPane;
  16.  
  17. public class Main {
  18.    
  19.     private JFrame frame;
  20.     private DefaultTableModel model;
  21.     private JScrollPane scrollPane;
  22.     private JTable table;
  23.  
  24.     /**
  25.      * Starts the program and runs the first methods that are needed
  26.      * @param args
  27.      */
  28.     public static void main(String[] args) {
  29.         @SuppressWarnings("unused")
  30.         Main app = new Main();
  31.     }
  32.    
  33.     /**
  34.      * Constructor
  35.      */
  36.     public Main(){
  37.         drawUI();
  38.         Go();
  39.     }
  40.  
  41.     /**
  42.      * In this method, all the data is gathered from the database and is filled into a JTable
  43.      */
  44.     private void Go() {
  45.         //The sparql query we're using to get all the data from the FUSEKI server
  46.         String query = "SELECT ?subject ?predicate ?object \n" +
  47.                        "WHERE { \n" +
  48.                        "?subject ?predicate ?object }";
  49.         //Excecute the query and convert the result to a ResultSet from Apache Jena (NOT FROM Java.sql)
  50.         ResultSet rs = QueryExecutionFactory.sparqlService( "http://localhost:3030/AnimalDataSet/", query ).execSelect();
  51.        
  52.         //While loop to get all the rows in the ResultSet
  53.         while(rs.hasNext())
  54.         {
  55.             //Moves onto the next result
  56.             QuerySolution sol = rs.nextSolution();
  57.             //Return the value of the named variable in this binding.
  58.             //A return of null indicates that the variable is not present in this solution
  59.             RDFNode object = sol.get("object");
  60.             RDFNode predicate = sol.get("predicate");
  61.             RDFNode subject = sol.get("subject");
  62.            
  63.             //Fill the table with the data
  64.             DefaultTableModel model = (DefaultTableModel) table.getModel();
  65.             model.addRow(new Object[]{subject, predicate, object});
  66.         }
  67.        
  68.     }
  69.  
  70.     /**
  71.      * Creates the window
  72.      * Not really interesting, creating a JFrame with toolbar, scrollbar and a table
  73.      */
  74.     private void drawUI(){
  75.         frame = new JFrame();
  76.         frame.setBounds(100, 100, 585, 413);
  77.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  78.        
  79.         JToolBar toolBar = new JToolBar();
  80.         toolBar.setFloatable(false);
  81.         frame.getContentPane().add(toolBar, BorderLayout.NORTH);
  82.        
  83.         JButton btnHelp = new JButton("Help");
  84.         toolBar.add(btnHelp);
  85.        
  86.         scrollPane = new JScrollPane();
  87.         frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
  88.        
  89.         //Right here you just set the table headers
  90.         String [] header={"Subject", "Predicate", "Object"};
  91.         //Creating the tablemodel with no data , 'null', and give the tablemodel the header
  92.         model = new DefaultTableModel(null, header);
  93.         table = new JTable(model);
  94.         scrollPane.setViewportView(table);
  95.  
  96.         frame.setVisible(true);
  97.        
  98.         try {
  99.             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  100.         } catch (ClassNotFoundException | InstantiationException | IllegalAccessException
  101.                 | UnsupportedLookAndFeelException e) {
  102.             // TODO Auto-generated catch block
  103.             e.printStackTrace();
  104.         }
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement