Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main;
- import java.awt.BorderLayout;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JTable;
- import javax.swing.JToolBar;
- import javax.swing.UIManager;
- import javax.swing.UnsupportedLookAndFeelException;
- import javax.swing.table.DefaultTableModel;
- import org.apache.jena.query.QueryExecutionFactory;
- import org.apache.jena.query.QuerySolution;
- import org.apache.jena.query.ResultSet;
- import org.apache.jena.rdf.model.RDFNode;
- import javax.swing.JScrollPane;
- public class Main {
- private JFrame frame;
- private DefaultTableModel model;
- private JScrollPane scrollPane;
- private JTable table;
- /**
- * Starts the program and runs the first methods that are needed
- * @param args
- */
- public static void main(String[] args) {
- @SuppressWarnings("unused")
- Main app = new Main();
- }
- /**
- * Constructor
- */
- public Main(){
- drawUI();
- Go();
- }
- /**
- * In this method, all the data is gathered from the database and is filled into a JTable
- */
- private void Go() {
- //The sparql query we're using to get all the data from the FUSEKI server
- String query = "SELECT ?subject ?predicate ?object \n" +
- "WHERE { \n" +
- "?subject ?predicate ?object }";
- //Excecute the query and convert the result to a ResultSet from Apache Jena (NOT FROM Java.sql)
- ResultSet rs = QueryExecutionFactory.sparqlService( "http://localhost:3030/AnimalDataSet/", query ).execSelect();
- //While loop to get all the rows in the ResultSet
- while(rs.hasNext())
- {
- //Moves onto the next result
- QuerySolution sol = rs.nextSolution();
- //Return the value of the named variable in this binding.
- //A return of null indicates that the variable is not present in this solution
- RDFNode object = sol.get("object");
- RDFNode predicate = sol.get("predicate");
- RDFNode subject = sol.get("subject");
- //Fill the table with the data
- DefaultTableModel model = (DefaultTableModel) table.getModel();
- model.addRow(new Object[]{subject, predicate, object});
- }
- }
- /**
- * Creates the window
- * Not really interesting, creating a JFrame with toolbar, scrollbar and a table
- */
- private void drawUI(){
- frame = new JFrame();
- frame.setBounds(100, 100, 585, 413);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- JToolBar toolBar = new JToolBar();
- toolBar.setFloatable(false);
- frame.getContentPane().add(toolBar, BorderLayout.NORTH);
- JButton btnHelp = new JButton("Help");
- toolBar.add(btnHelp);
- scrollPane = new JScrollPane();
- frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
- //Right here you just set the table headers
- String [] header={"Subject", "Predicate", "Object"};
- //Creating the tablemodel with no data , 'null', and give the tablemodel the header
- model = new DefaultTableModel(null, header);
- table = new JTable(model);
- scrollPane.setViewportView(table);
- frame.setVisible(true);
- try {
- UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
- } catch (ClassNotFoundException | InstantiationException | IllegalAccessException
- | UnsupportedLookAndFeelException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement