单击按钮时不显示SQLite数据库

时间:2016-05-18 20:59:57

标签: java database eclipse sqlite jtable

晚上好,

所以我一直在使用SQLite创建一个本地数据库。 (作为Firefox的一部分插件)。它使用三个类:

databaseConnection
Login
Display

和两个表(在数据库MyFilms下面)称为:

Users
MyFilms

databaseConnection用于简单地连接到我的SQLite数据库MyFilms,它可以正常工作。

第二个类Login,从我的数据库访问我的Users表,以便它可以访问登录并继续我的名为Display的GUI。

Display将加载以下GUI,其中包含一个按钮,单击此按钮时,应将数据库表MyFilms中的数据加载到JTable中:

enter image description here

然而,当我点击按钮时,没有任何东西加载......它甚至看起来像JTable不存在,但当我检查我的代码时它是挑衅的!

我认为问题可能存在的代码是:

JButton btnLoadData = new JButton("Load Data");
btnLoadData.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        try{
            String query ="Select * from MyFilms";
            PreparedStatement pst = connection.prepareStatement(query);
            ResultSet rs = pst.executeQuery();
            table.setModel(DbUtils.resultSetToTableModel(rs));
        }
        catch (Exception anException){
            anException.printStackTrace();
        }
    }
});

知道为什么数据库没有显示在这里?请在下面找到每个班级的代码:

的DatabaseConnection:

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

public class databaseConnection {

    Connection conn = null; 

    public static Connection dbConnector(){
        try{
            Class.forName("org.sqlite.JDBC");
            Connection conn = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\Joe\\workspace\\MyFilms.sqlite");
            JOptionPane.showMessageDialog(null, "Connection Successful!");
            return conn;
        }
        catch(Exception anException)
        {
            JOptionPane.showMessageDialog(null, anException);
            return null;
        }

        }
}

登录

import java.awt.EventQueue;
import javax.swing.JFrame;
import java.sql.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Login {

    private JFrame frmTest;

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

    Connection connection = null;
    private JTextField textFieldUN;
    private JPasswordField passwordField;

    /**
     * Create the application.
     */
    public Login() {
        initialize();
        connection = databaseConnection.dbConnector();
    }

    /**
     * Initialise the contents of the frame.
     */
    private void initialize() {
        frmTest = new JFrame();
        frmTest.setTitle("Database Login");
        frmTest.setBounds(100, 100, 345, 184);
        frmTest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmTest.getContentPane().setLayout(null);

        JLabel lblNewLabel = new JLabel("Username:");
        lblNewLabel.setBounds(34, 37, 64, 14);
        frmTest.getContentPane().add(lblNewLabel);

        JLabel lblNewLabel_1 = new JLabel("Password:");
        lblNewLabel_1.setBounds(34, 66, 64, 14);
        frmTest.getContentPane().add(lblNewLabel_1);

        textFieldUN = new JTextField();
        textFieldUN.setBounds(126, 34, 151, 20);
        frmTest.getContentPane().add(textFieldUN);
        textFieldUN.setColumns(10);

        JButton btnLogin = new JButton("Login");
        frmTest.getRootPane().setDefaultButton(btnLogin);
        btnLogin.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                try{
                    String query = "Select * from Users where username=? and password=?";
                    PreparedStatement pst = connection.prepareStatement(query);
                    pst.setString(1, textFieldUN.getText());
                    pst.setString(2, passwordField.getText());

                    ResultSet rs = pst.executeQuery();
                    int count = 0;
                    while(rs.next()){
                        count = count +1;
                    }
                    if (count == 1){
                        frmTest.dispose();
                        Display GUI = new Display();
                        GUI.setVisible(true);
                    }

                    else if (count > 1){
                        JOptionPane.showMessageDialog(null, "Duplicate Username and Password");
                    }
                    else{
                        JOptionPane.showMessageDialog(null, "Username or Password is not correct - Please try again..");
                    }
                    rs.close();
                    pst.close();
                }       
                catch (Exception anException){
                    JOptionPane.showMessageDialog(null, anException);
                }

            }

        });
        btnLogin.setBounds(126, 111, 89, 23);
        frmTest.getContentPane().add(btnLogin);

        passwordField = new JPasswordField();
        passwordField.setBounds(126, 64, 151, 17);
        frmTest.getContentPane().add(passwordField);
    }
}

显示:

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import net.proteanit.sql.DbUtils;

import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.*;

public class Display 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 {
                    Display frame = new Display();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    Connection connection = null;
    /**
     * Create the frame.
     */
    public Display() {
        connection = databaseConnection.dbConnector();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 711, 443);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JButton btnLoadData = new JButton("Load Data");
        btnLoadData.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                try{
                    String query ="Select * from MyFilms";
                    PreparedStatement pst = connection.prepareStatement(query);
                    ResultSet rs = pst.executeQuery();
                    table.setModel(DbUtils.resultSetToTableModel(rs));
                }
                catch (Exception anException){
                    anException.printStackTrace();
                }
            }
        });
        btnLoadData.setBounds(596, 11, 89, 23);
        contentPane.add(btnLoadData);

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(684, 45, -675, 349);
        contentPane.add(scrollPane);

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

任何帮助将不胜感激!干杯..

但是,当我在Eclipse中运行它时,我根本没有收到任何错误。它运行正常,只是不显示JTable中的数据库。

1 个答案:

答案 0 :(得分:2)

请你用以下内容替换你的Display.java:

Display.java

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;

import net.proteanit.sql.DbUtils;

public class Display extends JFrame {
    private static final long serialVersionUID = 1L;
    private JPanel contentPane;
    private JTable table;

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

    Connection connection = null;

    /**
     * Create the frame.
     */
    public Display() {
        connection = databaseConnection.dbConnector();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 711, 443);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new BorderLayout());

        JButton btnLoadData = new JButton("Load Data");
        btnLoadData.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                try {
                    String query = "Select * from MyFilms";
                    PreparedStatement pst = connection.prepareStatement(query);
                    ResultSet rs = pst.executeQuery();
                    table.setModel(DbUtils.resultSetToTableModel(rs));
                } catch (Exception anException) {
                    anException.printStackTrace();
                }
            }
        });
        // btnLoadData.setBounds(596, 11, 89, 23);
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new BorderLayout());
        buttonPanel.add(btnLoadData, BorderLayout.EAST);

        contentPane.add(buttonPanel, BorderLayout.NORTH);

        JScrollPane scrollPane = new JScrollPane();
        // scrollPane.setBounds(684, 45, -675, 349);

        table = new JTable();
        scrollPane.setViewportView(table);
        contentPane.add(scrollPane, BorderLayout.CENTER);
    }
}

并查看结果?

您的代码几乎没有任何变化。他们是:

1)您首先将scrollPane添加到contentPane,然后将table添加到scrollPane。我改变了订单。

2)你使用了绝对布局。我用BorderLayout改变了它。

我系统上显示的结果是:

enter image description here

希望这有帮助!