如何将Java类连接到GUI?

时间:2016-02-20 21:29:01

标签: java mysql database user-interface windowbuilder

我正在设计一个可以通过GUI访问的数据库,我有三个类Main,Database和GUI,当我运行main时出现错误,GUI关闭,然后是一条简短的错误消息,我无法读取,不知道这是哪里出错,因为我认为这可能是一些问题。谢谢你的帮助:)

这是我的主要课程

public class MainApplication {

@SuppressWarnings("unused")
public static void main(String[] args) {

    VideoStoreGUI window = new VideoStoreGUI();
} 

}

我的数据库类:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

import javax.swing.JOptionPane;
@SuppressWarnings("unused")
public class DataBase {

static Connection con = null;
static Statement stmt = null;
static ResultSet rs = null;
static Scanner in = new Scanner(System.in);



public void close_connection() {
    try
    {
        con.close();
        System.out.println("Database Connections Succesully Closed.");
    } 
    catch (SQLException sqle)
    {
        System.out.println("Error: failed to close the database");
    }
}

public static void addMember(int member_id, String name, String address) // Adding a Member to the Database.
{
    try {
        String str = "INSERT INTO members (member_id, name, address) values(" + member_id + ", '" + name + "', '"
                + address + "');";
        int rows = stmt.executeUpdate(str);

        System.out.println("Success in adding member");

    } catch (SQLException sqle) {
        JOptionPane.showMessageDialog(null, "Error: Could not add member");
    }
}


public static void initialize_database() {
    try {
        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/videostore";
        con = DriverManager.getConnection(url, "root", "admin");
        stmt = con.createStatement();
    } catch (Exception e) {
        System.out.println("Error: Failed to connect to database\n" + e.getMessage());
    }
}

public DataBase()
{
    initialize_database();      
}

}

和我的GUI类:

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

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

import javax.swing.JTabbedPane;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JMenuBar;

public class VideoStoreGUI extends JFrame {
private JFrame frame;

DataBase my_database;
private JPanel contentPane;
private JTextField textMemberID;
private JTextField textMemberName;
private JTextField textMemberAddress;

/**
 * Create the frame.
 */
public VideoStoreGUI() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 400);

    JMenuBar menuBar = new JMenuBar();
    setJMenuBar(menuBar);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
    tabbedPane.setBounds(10, 31, 232, 240);
    contentPane.add(tabbedPane);

    JPanel panel = new JPanel();
    tabbedPane.addTab("Members", null, panel, null);
    panel.setLayout(null);

    JLabel labelMemberID = new JLabel("Members ID");
    labelMemberID.setBounds(10, 11, 85, 14);
    panel.add(labelMemberID);

    JLabel labelMemberName = new JLabel("Members Name");
    labelMemberName.setBounds(10, 36, 85, 14);
    panel.add(labelMemberName);

    JLabel labelMemberAddress = new JLabel("Members Address");
    labelMemberAddress.setBounds(10, 61, 85, 14);
    panel.add(labelMemberAddress);

    textMemberID = new JTextField();
    textMemberID.setBounds(131, 8, 86, 20);
    panel.add(textMemberID);
    textMemberID.setColumns(10);

    textMemberName = new JTextField();
    textMemberName.setColumns(10);
    textMemberName.setBounds(131, 33, 86, 20);
    panel.add(textMemberName);

    textMemberAddress = new JTextField();
    textMemberAddress.setColumns(10);
    textMemberAddress.setBounds(131, 58, 86, 20);
    panel.add(textMemberAddress);

    JButton buttonAddMember = new JButton("Add Member");
    buttonAddMember.setBounds(10, 86, 102, 23);
    panel.add(buttonAddMember);

    JButton buttonRemoveMember = new JButton("Add Member");
    buttonRemoveMember.setBounds(115, 86, 102, 23);
    panel.add(buttonRemoveMember);

    JButton buttonSearchMember = new JButton("Add Member");
    buttonSearchMember.setBounds(66, 120, 102, 23);
    panel.add(buttonSearchMember);

    JPanel panel_1 = new JPanel();
    tabbedPane.addTab("Products", null, panel_1, null);
}

}

2 个答案:

答案 0 :(得分:0)

我只看到一个可以弹出窗口的地方。如果您愿意,可以保留它,但添加系统输出并确保您的消息:

catch (SQLException sqle) {
        System.out.writeln("Exception:"+sqle);
        JOptionPane.showMessageDialog(null, "Error: Could not add member");
    }

根据此消息的位置,我认为你有错误的sql或数据库。我建议你打印你的sql和它的结果。

答案 1 :(得分:0)

这对我不起作用的主要原因是因为我从GUI中删除了setVisable(true)并忘记将其添加回方法,然后我收到了错误" AWT-EventQueue-0&# 34;这与设置数据库连接的方式有关,删除并添加JDBC驱动程序再次解决了这个问题。