我使用MySQL数据库创建了一个Java通讯录程序。我的程序有左侧面板和右侧面板。左侧面板将新条目写入数据库,右侧面板显示所有数据库信息。在我向数据库提交新条目后,我无法更新程序。我必须关闭我的程序,然后重新打开它以显示新信息。
单击提交按钮后,如何让我的地址簿自动更新?
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();
}
});
}
}
答案 0 :(得分:3)
从左侧面板插入数据后,只需调用一个包含代码的函数,用新文本设置更新的数据,如
lab1.setText($first_name);
..
..
然后将$first_name
放到
null