title: How do i connect my sqlite db to my java swing GUI?
body: <p>I have a problem finding a way to combine my sqlite database with my java swing GUI, im using Sublime Text 3.</p>
  
  <p>This is for a school project where we are trying to make a database over appointment times using a sqlite database we can edit, have been trying alot of different ways to connect them but im new to java swing so having abit of trouble.</p>
  
  <p>This is our GUI</p>
  
  <pre class="lang-java prettyprint-override"><code>import javax.swing.*;
  import java.awt.*;
  import java.awt.event.*;
  import java.sql.*;
  
  public class Timeliste extends JFrame implements ActionListener {
      JButton leggtil, slett, endre;
      JTextField time;
      JTextArea Oversikt;
      JLabel avtaler;
      static JTable data;
      private Statement stmt;
      private ResultSet rs;
  public static void main(String[] args) throws Exception {
          Timeliste vindu = new Timeliste();
          vindu.setTitle("Time registrering");
          vindu.setDefaultCloseOperation(EXIT_ON_CLOSE);
          vindu.setSize(260,170);
          vindu.setResizable(false);
          vindu.setLocationRelativeTo(null);
          vindu.opprettGUI();
          vindu.pack();
          vindu.setVisible(true);
          getConnection();
  
    }
  public static Connection getConnection() throws Exception {
          Connection conn = DriverManager.getConnection("jdbc:sqlite:avtaler.db"); 
          conn.close();       
          System.out.println("Vellykket oppkobling til databasen!"); 
  
      return null;
  
  
  } 
      public void opprettGUI() {
      setLayout( new FlowLayout());
      add( new JLabel("Registrer timer her!") );
      leggtil = new JButton("Legg til");
      add(leggtil);
      slett = new JButton("Slett");
      add(slett);
      endre = new JButton("Endre");
      add(endre);
      time = new JTextField(8);
      add(time);
      setTitle("Avtale registrering");
      setMinimumSize(new Dimension(200,300));
      add( new JTable(10, 5));
  
  
      }
  
  
      public void actionPerformed(ActionEvent e){
  }
   }
  </code></pre>
  
  <p>And this is our sqlite database</p>
  
  <pre class="lang-java prettyprint-override"><code>import static javax.swing.JOptionPane.*; 
  import java.sql.*; 
  
  public class Avtaler {
  
    private static String url = "jdbc:sqlite:avtaler.db"; 
    private static Connection conn = null; 
  
    public static void main(String[] args) {
      String utTxt = "";
      kobleOpp();  // Kontakter databasen 
  
      try {
        Statement stmt = conn.createStatement();
        // Opprette databasen gjøres først!
        String sql = sqlNyDB(); // Spørring def i hjelpemetode
        stmt.executeUpdate(sql);
        utTxt = "Databasen er opprettet - ok!" + "\n"; 
  
  
        // Lister ut alle personer i databasen
        // String sql = "select * from Person;";      
        sql = "select * from Avtale order by Dato;";
        ResultSet rs   = stmt.executeQuery(sql); 
  
        while (rs.next()) {
          int nr           = rs.getInt("Nr");
          String dato   = rs.getString("Dato");
          String sted = rs.getString("Sted");
          String beskrivelse = rs.getString("Beskrivelse");
          utTxt += nr + ", " + dato+ " (" + sted + ") - " + beskrivelse + "\n";
        } 
  
      }
      catch (Exception e) {  
        utTxt = "Databasespørring feilet!";
      } 
  
      showMessageDialog(null, utTxt);
      kobleNed();
    } 
  
  
    // Kobler opp til databasen.
    private static void kobleOpp() {
      try { 
        conn = DriverManager.getConnection(url);  
      } 
      catch (SQLException e) {
        System.out.println( "Oppkobling til databasen " + url + " feilet." + "\n" + e.toString() );
      }
    } 
  
    // Lukker forbindelsen til databasen.
    private static void kobleNed() {
      try {
        conn.close();
      }
      catch (SQLException e) { }
    } 
  
    private static String sqlNyDB() {
      return       "drop table if exists Avtale; create table Avtale(Nr integer primary key, Dato varchar(50), Sted varchar(50), Beskrivelse varchar(50) );"
    + "insert into Avtale values ( 1, '2019-09-09 09:00:00', 'Oslo', 'Gruppearbeid');"
    + "insert into Avtale values ( 2, '2019-07-07 07:00:00', 'Fredrikstad', 'Signere kontrakt');"
    + "insert into Avtale values ( 3, '2019-12-05 11:30:00', 'Bø', 'Pub med gutta');"
    + "insert into Avtale values ( 4, '2019-06-09 07:45:00', 'Oslo', 'Gruppearbeid');"
    + "insert into Avtale values ( 5, '2019-08-11 12:00:00', 'Bergen', 'Basketball trening');";
  
    }
  
  }
  </code></pre>