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

📄 client.java

📁 JAVA实现的酒店管理系统
💻 JAVA
字号:
package file1;

/*
 * 功能描述:所有对客户的操作的入口类
 * @Author:黄顺武
 * Create Time:---
 * Last Modified:2007-12-15
 * Modify Reason:数据库连接类DBConnection 的内部结构设计得到优化
 */
import java.sql.*;
import javax.swing.*;
import sun.jdbc.rowset.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Client extends JPanel implements ActionListener {

	private JLabel cGrade = new JLabel("客户级别:");
	private JLabel cName = new JLabel("客户名称:");
	private JLabel contaction = new JLabel("联系方式:");
	private JComboBox cGradeBox;
	private JTextField cNameTF = new JTextField(10);
	private JTextField contactionTF = new JTextField(10);
	private JPanel p1 = new JPanel();
	private JTable recordTable;
	private String[] head = { "ID", "客户名称", "联系方式", "客户级别" };
	private int headNum = 0;
	private String tableData[][] = null;
	private JScrollPane recScrollPane;
	private JPanel p3 = new JPanel();
	private JButton add = new JButton("增加记录");
	private JButton delete = new JButton("删除记录");
	private String[] cGrade_ids = null;// 所有客户级别的id
	private String[] client_ids = null;// 所有客户的ID
	CheckDigits checkDigits=null;//用来为电话号码的合法性提供方法的类对象

	public Client() {
		checkDigits=new CheckDigits();
		cNameTF.setBorder(null);
		contactionTF.setBorder(null);
		cGradeBox = new JComboBox();
		headNum = head.length;
		this.setLayout(new BorderLayout(0, 5));
		p1.setLayout(new FlowLayout(FlowLayout.CENTER, 15, 5));
		p1.add(cGrade);
		p1.add(cGradeBox);
		p1.add(cName);
		p1.add(cNameTF);
		p1.add(contaction);
		p1.add(contactionTF);
		p3.setLayout(new FlowLayout(FlowLayout.CENTER, 15, 5));
		add.setBorder(null);
		add.setBackground(Color.LIGHT_GRAY);
		delete.setBorder(null);
		delete.setBackground(Color.LIGHT_GRAY);
		p3.add(add);
		p3.add(delete);
		getAllCNames();
		doIt();
		add.addActionListener(this);
		delete.addActionListener(this);
	}

	private void getAllCNames() {//给客户级别下拉框赋值
		try {
			DBConnection con = new DBConnection();
			String query = "select ID,grade from CGrade";
			CachedRowSet crs = con.getResultSet(query);
			int count = 0;
			while (crs.next()) {
				count++;
			}
			cGrade_ids = new String[count];
			count = 0;
			crs.beforeFirst();
			while (crs.next()) {
				cGrade_ids[count++] = String.valueOf(crs.getInt(1));
				cGradeBox.addItem(crs.getString(2));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void doIt() {//从数据库中读取指定的客户记录字段并显示到表格中
		try {
			DBConnection con = new DBConnection();
			String sql = "select Client.id,cName,phone,grade from Client,CGrade where Client.cGradeID=CGrade.ID";
			CachedRowSet crs = con.getResultSet(sql);
			int count = 0;
			while (crs.next()) {
				count++;
			}
			if(count==0){
				delete.setEnabled(false);
			}else{
				delete.setEnabled(true);
			}
			crs.beforeFirst();
			tableData = new String[count][headNum];
			client_ids = new String[count];
			int row = 0;
			while (crs.next()) {
				client_ids[row] = String.valueOf(crs.getInt(1));
				tableData[row][0] = String.valueOf(crs.getInt(1));
				tableData[row][1] = crs.getString(2);
				tableData[row][2] = crs.getString(3);
				tableData[row][3] = crs.getString(4);
				row++;
			}
			recordTable = new JTable(tableData, head);
			recordTable.setRowHeight(20);
			recScrollPane = new JScrollPane(recordTable);
			this.add(p1, BorderLayout.NORTH);
			this.add(recScrollPane, BorderLayout.CENTER);
			this.add(p3, BorderLayout.SOUTH);
			this.validate();
		} catch (SQLException sqle) {
			sqle.printStackTrace();
		} catch (ClassNotFoundException cnfe) {
			cnfe.printStackTrace();
		}
		return;
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == add) {//点击"添加记录"按钮时调用的方法
			int index = cGradeBox.getSelectedIndex();
			if (index == -1) {
				JOptionPane.showMessageDialog(null, "客户级别不能为空!", "提示",
						JOptionPane.INFORMATION_MESSAGE);
				return;
			}
			String cName = cNameTF.getText().trim();
			if (cName.equals("")) {
				JOptionPane.showMessageDialog(null, "客户名称不能为空!", "提示",
						JOptionPane.INFORMATION_MESSAGE);
				return;
			}
			String contaction = contactionTF.getText().trim();
			String phone = checkDigits.doCheck(contaction);
			if (phone == null) {
				JOptionPane.showMessageDialog(null, "电话号码的所有位必须为数字!", "提示",
						JOptionPane.INFORMATION_MESSAGE);
				return;
			}
			try {
				DBConnection con = new DBConnection();
				String insert = "insert into Client values("
						+ cGrade_ids[index] + ",'" + cName + "','" + phone
						+ "')";
				con.addSql(insert);
				con.doDML();//执行添加操作
				doIt();//重新查询数据库以使刚插入到数据库中的记录的指定字段显示到显示指定客户字段数据的表格中
			} catch (SQLException sqle) {
				sqle.printStackTrace();
			} catch (ClassNotFoundException cnfe) {
				cnfe.printStackTrace();
			}

		}
		if (e.getSource() == delete) {//点击"删除记录"按钮时调用的方法
			String idGet = (String) JOptionPane.showInputDialog(null,
					"请选择要删除的客户ID!", "", JOptionPane.INFORMATION_MESSAGE, null,
					client_ids, client_ids[0]);
			if (idGet == null) {
				return;
			}
			try {
				DBConnection con = new DBConnection();
				int confirm = JOptionPane.showConfirmDialog(null, "您真的确认删除吗?",
						"", JOptionPane.YES_NO_OPTION);
				if (confirm == JOptionPane.YES_OPTION) {
					String delete = "delete from Client where id="
							+ idGet ;
					con.addSql(delete);
					con.doDML();//执行删除动作
					doIt();//重新查询数据库以使刚执行的删除动作反映到显示指定客户字段数据的表格中
				}
			} catch (SQLException sqle) {
				sqle.printStackTrace();
			} catch (ClassNotFoundException cnfe) {
				cnfe.printStackTrace();
			}
		}
	}
}

⌨️ 快捷键说明

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