java 修改数据后刷新_将信息提交到数据库后刷新Java程序

本文介绍了一个使用Java和MySQL创建的地址簿程序,当在左侧面板输入新条目并提交后,如何实现在不关闭程序的情况下更新右侧显示所有数据库信息的面板。通过在提交按钮的事件监听器中调用更新面板的函数,实现数据的即时刷新。
摘要由CSDN通过智能技术生成

我使用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();

}

});

}

}

解决方法:

从左侧面板插入数据后,只需调用一个函数即可,该函数将包含用新文本设置更新后的数据的代码,例如

lab1.setText($first_name);

..

..

然后将$first_name放入

null

标签:java,mysql

来源: https://codeday.me/bug/20191119/2038586.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值