简体   繁体   中英

How do i connect my sqlite db to my java swing GUI?

I have a problem finding a way to combine my sqlite database with my java swing GUI, im using Sublime Text 3.

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.

This is our GUI

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){
}
 }

And this is our sqlite database

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');";

  }

}
private static Connection conn = null; 

Your Connection is null.

Connection conn = DriverManager.getConnection("jdbc:sqlite:avtaler.db"); 
conn.close();       
System.out.println("Vellykket oppkobling til databasen!"); 

return null;

You open but close the Connection immediately and then return null.

How to you expect to use an Object if the objects value is null? This is nothing special about connecting to a database, this is basic Java.

A database is like a file, you open the file, read data from the file and then close the file.

So for a database you get a connection to the database, execute SQL commands on the database and then close the connection to the database.

Start by reading the Java tutorial on JDBC Basics for basic information and examples.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM