在向数据库提交信息后刷新java程序。

时间:2022-10-13 09:09:57

I created a Java address book program using MySQL Database. My program has a left panel and a right panel. The left panel writes new entries to the database and the right panel displays all the database info. I'm having trouble updating the program after I've submitted new entries to the database. I have to close my program then reopen it to display the new info.

我使用MySQL数据库创建了一个Java地址簿程序。我的程序有一个左面板和一个右面板。左侧面板向数据库写入新条目,右侧面板显示所有数据库信息。在向数据库提交新条目之后,我在更新程序时遇到了麻烦。我必须关闭我的程序然后重新打开它来显示新的信息。

How can I get my address book to update itself after I click the submit button?

当我点击提交按钮后,如何让地址簿自动更新?

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.*;

@SuppressWarnings("serial")
public class Frame1 extends JFrame {

JLabel label1 = new JLabel("Address Book Input");
JLabel label2 = new JLabel("MySQL Address Book");

JButton submit = new JButton("Submit");

JTextField $first_name = new JTextField(20);
JTextField $last_name = new JTextField(20);
JTextField $phone = new JTextField(20);
JTextField $email = new JTextField(20);
JTextField $street = new JTextField(20);
JTextField $city = new JTextField(20);
JTextField $state = new JTextField(20);
JTextField $zip = new JTextField(20);

JLabel first_nameLabel = new JLabel("First Name: ");
JLabel last_nameLabel = new JLabel("Last Name: ");
JLabel phoneLabel = new JLabel("Phone: ");
JLabel emailLabel = new JLabel("Email: ");
JLabel streetLabel = new JLabel("Street: ");
JLabel cityLabel = new JLabel("City: ");
JLabel stateLabel = new JLabel("State: ");
JLabel zipLabel = new JLabel("Zip: ");

public Frame1() {
    super("1 Class Template");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(800, 480);
    setLocationRelativeTo(null);
    setVisible(true);
    initComponents();

}

public void initComponents() {
    JPanel panel = new JPanel(new GridLayout(0, 2));
    JPanel panelLeft = new JPanel(new GridBagLayout());
    JPanel panelRight = new JPanel(new BorderLayout());
    JScrollPane scrollPane = new JScrollPane();
    JPanel addressBook = new JPanel(new GridBagLayout());

    add(panel);
    panel.add(panelLeft);
    panel.add(panelRight);
    panelRight.add(scrollPane);
    scrollPane.setViewportView(addressBook);

    panelLeft.setBorder(BorderFactory.createLineBorder(Color.black));
    panelRight.setBorder(BorderFactory.createLineBorder(Color.black));

    GridBagConstraints gbc = new GridBagConstraints();

    label1.setFont(new Font(null, Font.PLAIN, 18));
    gbc.insets = new Insets(8, 8, 8, 8);
    gbc.gridwidth = 2;
    gbc.anchor = GridBagConstraints.CENTER;
    gbc.gridx = 0;
    gbc.gridy = 0;
    panelLeft.add(label1, gbc);

    gbc.insets = new Insets(4, 4, 4, 4);
    gbc.gridwidth = 1;
    gbc.anchor = GridBagConstraints.LINE_END;
    gbc.gridx = 0;
    gbc.gridy++;
    panelLeft.add(first_nameLabel, gbc);
    gbc.gridy++;
    panelLeft.add(last_nameLabel, gbc);
    gbc.gridy++;
    panelLeft.add(phoneLabel, gbc);
    gbc.gridy++;
    panelLeft.add(emailLabel, gbc);
    gbc.gridy++;
    panelLeft.add(streetLabel, gbc);
    gbc.gridy++;
    panelLeft.add(cityLabel, gbc);
    gbc.gridy++;
    panelLeft.add(stateLabel, gbc);
    gbc.gridy++;
    panelLeft.add(zipLabel, gbc);

    gbc.insets = new Insets(4, 4, 4, 4);
    gbc.gridwidth = 1;
    gbc.anchor = GridBagConstraints.LINE_START;
    gbc.gridx = 1;
    gbc.gridy = 1;
    panelLeft.add($first_name, gbc);
    gbc.gridy++;
    panelLeft.add($last_name, gbc);
    gbc.gridy++;
    panelLeft.add($phone, gbc);
    gbc.gridy++;
    panelLeft.add($email, gbc);
    gbc.gridy++;
    panelLeft.add($street, gbc);
    gbc.gridy++;
    panelLeft.add($city, gbc);
    gbc.gridy++;
    panelLeft.add($state, gbc);
    gbc.gridy++;
    panelLeft.add($zip, gbc);

    gbc.insets = new Insets(4, 4, 4, 4);
    gbc.anchor = GridBagConstraints.LINE_END;
    gbc.gridwidth = 1;
    gbc.gridx = 1;
    gbc.gridy++;
    submit.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            try {
                java.sql.Connection myConn = DriverManager.getConnection(
                        "jdbc:mysql://localhost:3306/database_name",
                        "user_name", "password");

                Statement myStmt = myConn.createStatement();
                myStmt
                        .executeUpdate("INSERT INTO address_book (first_name, last_name, phone, email, street, city, state, zip) VALUES ('"
                                + $first_name.getText()
                                + "', '"
                                + $last_name.getText()
                                + "', '"
                                + $phone.getText()
                                + "', '"
                                + $email.getText()
                                + "', '"
                                + $street.getText()
                                + "', '"
                                + $city.getText()
                                + "', '"
                                + $state.getText()
                                + "', '"
                                + $zip.getText() + "') ");

                $first_name.setText("");
                $last_name.setText("");
                $phone.setText("");
                $email.setText("");
                $street.setText("");
                $city.setText("");
                $state.setText("");
                $zip.setText("");

                JOptionPane.showConfirmDialog(null,
                        "Your Data Has been Inserted", "Result",
                        JOptionPane.DEFAULT_OPTION,
                        JOptionPane.PLAIN_MESSAGE);

            }

            catch (Exception exc) {
                exc.printStackTrace();
            }
        }
    });
    panelLeft.add(submit, gbc);

    label2.setFont(new Font(null, Font.PLAIN, 18));
    gbc.insets = new Insets(8, 8, 8, 8);
    gbc.gridwidth = 2;
    gbc.anchor = GridBagConstraints.CENTER;
    gbc.gridx = 0;
    gbc.gridy = 0;
    addressBook.add(label2, gbc);

    gbc.anchor = GridBagConstraints.LINE_START;
    gbc.gridwidth = 1;
    try {
        java.sql.Connection myConn = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/database_name", "user_name",
                "password");

        Statement myStmt = myConn.createStatement();

        ResultSet myRs = myStmt
                .executeQuery("SELECT * FROM address_book ORDER BY last_name, first_name");

        while (myRs.next()) {
            JLabel lab1 = new JLabel(myRs.getString("first_name") + " "
                    + myRs.getString("last_name"));
            JLabel lab2 = new JLabel(myRs.getString("phone"));
            JLabel lab3 = new JLabel(myRs.getString("email"));
            JLabel lab4 = new JLabel(myRs.getString("street"));
            JLabel lab5 = new JLabel(myRs.getString("city") + ", "
                    + myRs.getString("state") + " " + myRs.getString("zip"));

            gbc.insets = new Insets(4, 4, 4, 4);
            gbc.gridy++;
            addressBook.add(lab1, gbc);
            gbc.gridy++;
            addressBook.add(lab2, gbc);
            gbc.gridy++;
            addressBook.add(lab3, gbc);
            gbc.gridy++;
            addressBook.add(lab4, gbc);
            gbc.insets = new Insets(4, 4, 20, 4);
            gbc.gridy++;
            addressBook.add(lab5, gbc);

        }
    }

    catch (Exception exc) {
        exc.printStackTrace();
    }
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new Frame1();
        }
    });
}
}

2 个解决方案

#1


3  

After inserting data from your left panel, just call a function that will contain the code to set the updated the data with the new text like

在从左侧面板插入数据之后,只需调用一个函数,该函数将包含用于设置新文本的更新数据的代码。

lab1.setText($first_name);
..
..

then put $first_name to

然后把first_name美元

null

#2


0  

I found the code I was looking for. This clears the addressBook panel, then I can pull my info from the database again.

我找到了我要找的代码。这将清除addressBook面板,然后我可以再次从数据库中提取我的信息。

    addressBook.removeAll();
    addressBook.updateUI();

#1


3  

After inserting data from your left panel, just call a function that will contain the code to set the updated the data with the new text like

在从左侧面板插入数据之后,只需调用一个函数,该函数将包含用于设置新文本的更新数据的代码。

lab1.setText($first_name);
..
..

then put $first_name to

然后把first_name美元

null

#2


0  

I found the code I was looking for. This clears the addressBook panel, then I can pull my info from the database again.

我找到了我要找的代码。这将清除addressBook面板,然后我可以再次从数据库中提取我的信息。

    addressBook.removeAll();
    addressBook.updateUI();