SQLite数据库表未连接

时间:2019-02-17 22:17:58

标签: java eclipse sqlite

我正在尝试创建一个程序,该程序将在表格中生成学生信息。我已经在SQLite Studio中创建了一个数据库和一个名为data的表。我还成功地将数据库连接到了我的程序,但是,当程序运行时,出现了一个SQL错误,说该表丢失了。请指教。

import java.sql.*;
import javax.swing.*;

public class connection {

Connection conn = null;


public static Connection dbConnector() {

    try {
        Class.forName("org.sqlite.JDBC");
        Connection conn = DriverManager.getConnection("jdbc:sqlite:\\Macintosh HD⁩\\Users\\ethantang⁩\\Desktop⁩\\eclipse-workspace~⁩\\FBLA project⁩\\students.sqlite");
        JOptionPane.showMessageDialog(null, "Connected");
        return conn;

    }
    catch(Exception e) {
        JOptionPane.showMessageDialog(null, e);
        return null;
    }


}

}

import java.awt.BorderLayout;
import java.sql.*;
import javax.swing.*;
import java.awt.EventQueue;
import net.proteanit.sql.DbUtils;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import net.proteanit.sql.DbUtils;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.awt.event.ActionEvent;

public class database extends JFrame {

private JPanel contentPane;
private JTable table;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                database frame = new database();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

Connection connect = null;
/**
 * Create the frame.
 */
public database() {
    connect = connection.dbConnector();
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setBounds(6, 45, 438, 227);
    contentPane.add(scrollPane);

    table = new JTable();
    scrollPane.setViewportView(table);

    JButton btnNewButton = new JButton("Load Database");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                String query = "select * from data";
                PreparedStatement pst = connect.prepareStatement(query);
                ResultSet rs = pst.executeQuery();
                table.setModel(DbUtils.resultSetToTableModel(rs));
             }
            catch (Exception a){
                a.printStackTrace();
            }
        }
    });
    btnNewButton.setBounds(167, 6, 117, 29);
    contentPane.add(btnNewButton);
}
}

0 个答案:

没有答案