我无法找到一种方法,可以使用Sublime Text 3将sqlite数据库与java swing GUI结合起来。
这是针对一个学校项目,我们正在尝试使用可编辑的sqlite数据库在约会时间内建立数据库,并尝试了多种不同的方式将它们连接起来,但对于java swing来说是新手,因此遇到了很多麻烦。
这是我们的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){
}
}
这是我们的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');";
}
}
答案 0 :(得分:0)
private static Connection conn = null;
您的连接为空。
Connection conn = DriverManager.getConnection("jdbc:sqlite:avtaler.db");
conn.close();
System.out.println("Vellykket oppkobling til databasen!");
return null;
您打开但立即关闭连接,然后返回null。
如果objects值为null,您如何期望使用Object?连接数据库没有什么特别的,这是基本的Java。
数据库就像一个文件,您打开文件,从文件中读取数据,然后关闭文件。
因此对于数据库,您将获得与数据库的连接,在数据库上执行SQL命令,然后关闭与数据库的连接。
首先阅读JDBC Basics上的Java教程以获取基本信息和示例。