⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 demopersonmanager.java

📁 一个JDBC应用的例子
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import java.sql.*;
import java.util.ArrayList;

// 描述:演示程序
public class DemoPersonManager {
	public static void main(String[] args) {
		// 初始化主画框,调整其位置和宽度,使得显示出来的按钮更漂亮
		MainFrame.init("欢迎进入Java语言的GUI世界", MainFrame.getWidth()+500, 
				MainFrame.getHeight(), MainFrame.getStartX()-250, 
				MainFrame.getStartY()+50, "system");
		try {
			PersonManager manager = new PersonManager();
			manager.createManagerPane(MainFrame.getContentPane()); 
		} catch (Exception exc) {
			exc.printStackTrace();
			JOptionPane.showMessageDialog(MainFrame.getMainFrame(), 
					exc.getMessage(), "警告", JOptionPane.WARNING_MESSAGE);
		}
		// 启动主画框,并进行演示
		MainFrame.start();
	}
}

// 描述:管理表Person的管理器
// 作者:周晓聪
// 版本:1.0
// 日期:2003.09.12
class PersonManager extends DatabaseManager implements ActionListener {
	private Person person = null;

	// 功能:构造方法,主要是初始化person,调用它的open()方法建立与数据库的连接
	public PersonManager() throws Exception {
		person = new Person();
		person.open();
	}
	// 功能:创建用于管理人员信息的面板
	public void createManagerPane(Container ground) throws Exception {
		ArrayList data = person.findAll();
		DatabaseTableColumn[] columns = person.getColumns();
		String keyField = person.getKeyField();
		model = new DatabaseTableModel(data, columns, keyField, true);
		createManagerPane(ground, model);
		addOperationButton("添加(A)", 'I', "添加新的人员信息", "APPEND", this); 
		addOperationButton("删除(D)", 'D', "删除当前人员信息", "DELETE", this); 
		addOperationButton("退出(X)", 'X', "退出对人员信息管理", "EXIT", this); 
	}
	// 功能:当用户对管理器中的表格进行编辑时,调用person的update()方法修改数据库中相应内容
	public void updateFieldValue(Object keyId, String modifyField, Object newValue) {
		try {
			person.update((String)keyId, modifyField, newValue);
		} catch (Exception exc) {
			exc.printStackTrace();
			JOptionPane.showMessageDialog(MainFrame.getMainFrame(), 
					exc.getMessage(), "警告", JOptionPane.WARNING_MESSAGE);
		}
	}
	// 功能:关闭数据库连接及关闭管理器界面
	public void close() throws Exception {
		person.close();
		super.close();
	}
	// 功能:创建一对话框,让用户输入人员信息,以插入该人员信息
	public PersonRecord addPerson() {
		final PersonRecord record = new PersonRecord();
		record.id = null;
		// 创建一模态对话框
		final JDialog dialog = new JDialog(MainFrame.getMainFrame(), 
				"添加新的人员信息", true);
		dialog.setLocation(new Point(MainFrame.getStartX()+50, 
				MainFrame.getStartY()));
		JPanel contentPane = (JPanel)dialog.getContentPane();
		JPanel inputTextFieldPane = new JPanel();
		// 创建输入人员信息的标签与文本字段,这里使用了最简单的界面形式
		inputTextFieldPane.setLayout(new GridLayout(6, 2, 10, 10));
		final JTextField idField = new JTextField(10);
		addInputTextField(inputTextFieldPane, "人员代码(C)", 'C', idField);
		final JTextField nameField = new JTextField(10);
		addInputTextField(inputTextFieldPane, "人员姓名(N)", 'N', nameField);
		final JTextField sexField = new JTextField(10);
		addInputTextField(inputTextFieldPane, "性别(S)", 'S', sexField);
		final JTextField birthdayField = new JTextField(10);
		addInputTextField(inputTextFieldPane, "出生日期(B)", 'B', birthdayField);
		final JTextField addressField = new JTextField(30);
		addInputTextField(inputTextFieldPane, "家庭住址(A)", 'A', addressField);
		final JTextField resumeField = new JTextField(30);
		addInputTextField(inputTextFieldPane, "个人简历(R)", 'R', resumeField);
		contentPane.add(inputTextFieldPane, BorderLayout.CENTER);
		// 使用一个局部类来监听用户按下确定或取消按钮的事件
		class LocalActionListener implements ActionListener {
			public void actionPerformed(ActionEvent evt) {
				String command = ((JButton)evt.getSource()).getActionCommand();
				if (command.equals("OK")) {
					// 如果用户按下了确认按钮,则将用户输入的值保存到record
					record.id = idField.getText();
					record.name = nameField.getText();
					record.sex = (sexField.getText().equals("女"))? true : false;
					record.birthday = new Date(92, 12, 11);		
					record.address = addressField.getText();
					record.resume = resumeField.getText();
				}
				dialog.dispose();
			}
		}
		JPanel controlPane = new JPanel();
		LocalActionListener listener = new LocalActionListener();
		JButton button = new JButton("确认");
		button.setActionCommand("OK");
		button.addActionListener(listener);
		controlPane.add(button);
		button = new JButton("取消");
		button.setActionCommand("CANCEL");
		button.addActionListener(listener);
		controlPane.add(button);
		contentPane.add(controlPane, BorderLayout.SOUTH);
		dialog.pack();
		dialog.setVisible(true);
		if (record.id == null) return null;
		else return record;
	}
	// 功能:创建用于输入人员信息的标签与文本字段
	private void addInputTextField(Container place, String msg, char mnemonic, 
			JTextField field) {
		JLabel label = new JLabel(msg);
		label.setDisplayedMnemonic(mnemonic);
		label.setLabelFor(field);
		place.add(label);
		place.add(field);
	}
	// 功能:监听在管理器界面上添加的插入、删除按钮,完成对人员信息的更新
	public void actionPerformed(ActionEvent evt) {
		String command = ((JButton)evt.getSource()).getActionCommand();
		if (command.equals("APPEND")) {
			// 插入人员信息,调用方法addPerson()让用户输入人员信息
			PersonRecord personRecord = addPerson();
			if (personRecord == null) return;
			try {
				// 调用Person的add()方法将用户输入的人员信息插入的数据表
				person.add(personRecord);
			} catch (Exception exc) {
				exc.printStackTrace();
				JOptionPane.showMessageDialog(MainFrame.getMainFrame(), 
						exc.getMessage(), "警告", JOptionPane.WARNING_MESSAGE);
				return;
			}
			Object[] rowData = person.recordToArray(personRecord);
			// 调用DatabaseManager的addRow()方法将用户输入人员信息插入到Swing表格
			addRow(rowData);
		} else if (command.equals("DELETE")) {
			String personId = (String)getSelectionRowKey();
			try {
				// 调用Person的remove()方法在数据表中删除当前选中的人员信息
				person.remove(personId);
			} catch (Exception exc) {
				exc.printStackTrace();
				JOptionPane.showMessageDialog(MainFrame.getMainFrame(), 
						exc.getMessage(), "警告", JOptionPane.WARNING_MESSAGE);
			}
			// 调用DatabaseManager的removeRow()方法在Swing表格中删除当前行
			removeRow(personId);
		} else {
			// 准备退出程序,在退出时调用Person的close()方法关闭数据库连接,调用
			// DatabaseManager的close()方法清除界面
			try {
				close();
			} catch (Exception exc) { 
				JOptionPane.showMessageDialog(MainFrame.getMainFrame(), 
				exc.getMessage(), "警告", JOptionPane.WARNING_MESSAGE);
				exc.printStackTrace();
			}
			System.exit(1);
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -