简体   繁体   中英

How to connect Java classes to GUI?

I'm designing a database that can be accessed through a GUI, I have three classes Main, Database and GUI, When I run the main I get an error and the GUI closes followed by a brief error message which I cannot read, not sure where this is going wrong as i believe it can be a number of issues. Thanks for all your help :)

Here is my main class

public class MainApplication {

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

    VideoStoreGUI window = new VideoStoreGUI();
} 

}

My Database Class:

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();      
}

}

and my GUI class:

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);
}

}

I see only one place where you can get popup. You can keep it if you like but add system output and be sure about your message:

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

Based on place of this message I think you have wrong sql or database. I would suggest that you print your sql and results of it too.

这对我不起作用的主要原因是因为我从GUI中删除了setVisable(true),却忘记了将其重新添加到方法中,然后接收到错误“ AWT-EventQueue-0”,这与我建立数据库连接的方式,删除并添加JDBC驱动程序再次解决了此问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM