简体   繁体   English

如何将我的sqlite数据库连接到我的java swing GUI?

[英]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. 我有一个问题,找到一种方法将我的sqlite数据库与我的java swing GUI结合使用,我使用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. 这是一个学校项目,我们正在尝试使用我们可以编辑的sqlite数据库在预约时间创建一个数据库,已经尝试了很多不同的方式来连接它们,但我刚接触java swing,因此有麻烦。

This is our GUI 这是我们的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 这是我们的sqlite数据库

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. 您打开但立即关闭Connection然后返回null。

How to you expect to use an Object if the objects value is null? 如果对象值为null,您期望如何使用Object? This is nothing special about connecting to a database, this is basic Java. 这对于连接数据库没有什么特别之处,这是基本的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. 因此,对于数据库,您将获得与数据库的连接,在数据库上执行SQL命令,然后关闭与数据库的连接。

Start by reading the Java tutorial on JDBC Basics for basic information and examples. 首先阅读JDBC基础知识 Java教程,获取基本信息和示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为我的数独游戏制作 GUI? (摇摆) - How do I make a GUI for my Sudoku game? (Swing) 从Eclipse中的sqlite数据库在JAVA GUI中加载表时如何添加表标题? - How do i add table heading while loading a table in my JAVA GUI from sqlite database in eclipse? 如何将存储在AWS S3服务器中的sqlite数据库远程连接到我的android应用 - How do I connect a sqlite DB stored in AWS S3 server to my android app remotely 如何在我的Java应用程序的用户前端使用Swing GUI或轻量级Web客户端之间做出决定? - How do I decide between a using a Swing GUI or a light-weight web client for the user front end of my Java application? 如何在Java swing中为程序添加整数输入? - How do I add an integer input for my program in java swing? 如何使用Java中的Swing使代码在GUI中工作? - How can I make my code work in a GUI using Swing in Java? 如何在 JAVA GUI JTable Swing 中插入 sqlite DB 并使用动态搜索? - How to insert sqlite DB and use dynamic search in JAVA GUI JTable Swing? Java Swing:如何构建Connect 4 GUI - Java Swing: How to build Connect 4 GUI 如何在Java应用程序中连接到mongo db? - How I can connect to mongo db in my java application? 如何在Java GUI中将消息打印到屏幕上? - How do I print messages to the screen in my Java GUI?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM