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

📄 house.java

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

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

public class House extends JPanel implements ActionListener {

	private JLabel hGrade = new JLabel("客房级别:");
	private JLabel hNo = new JLabel("客房编号:");
	private JLabel state = new JLabel("是否可用:");
	private JLabel useable = new JLabel("开始可用时间:");
	private JComboBox hGradeBox;
	private JTextField hNoTF = new JTextField(10);
	private JComboBox stateBox = new JComboBox(new String[] { "是", "否" });
	private JTextField useableTF = new JTextField(10);
	private JPanel p1 = new JPanel();
	private JTable recordTable = new JTable();
	private String[] head = { "客房级别", "客房编号", "单价", "是否可用", "开始可用时间" };
	private int headNum = 0;
	private String tableData[][] = null;
	private JScrollPane recScrollPane;
	private JPanel p3 = new JPanel();
	private GetDate getD = null;
	private JButton add = new JButton("增加记录");
	private JButton delete = new JButton("删除记录");
	private String[] hNoS = null;

	public House() {
		getD = new GetDate();
		hNoTF.setBorder(null);
		useableTF.setBorder(null);
		hGradeBox = new JComboBox();
		headNum = head.length;
		this.setLayout(new BorderLayout(0, 5));
		p1.setLayout(new FlowLayout(FlowLayout.CENTER, 15, 5));
		p1.add(hGrade);
		p1.add(hGradeBox);
		p1.add(hNo);
		p1.add(hNoTF);
		p1.add(state);
		p1.add(stateBox);
		p1.add(useable);
		p1.add(useableTF);
		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 grade from HGrade";
			CachedRowSet crs = con.getResultSet(query);
			int count = 0;
			while (crs.next()) {
				count++;
			}
			count = 0;
			crs.beforeFirst();
			while (crs.next()) {
				hGradeBox.addItem(crs.getString(1).trim());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void doIt() {// 返回1表示没有记录或查询出错,返回0表示非前面两种情况
		try {
			DBConnection con = new DBConnection();
			String sql = "select hGrade,HouseNo,sPrice,state,beginUseable from House,HGrade where hGrade=grade";
			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];
			hNoS = new String[count];
			count = 0;
			int row = 0;
			while (crs.next()) {
				tableData[row][0] = crs.getString(1).trim();
				hNoS[count] = crs.getString(2).trim();
				tableData[row][1] = crs.getString(2).trim();
				tableData[row][2] = String.valueOf(crs.getFloat(3));
				tableData[row][3] = crs.getString(4).trim();
				tableData[row][4] = crs.getString(5).trim().replace(
						"00:00:000", "");
				count++;
				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();
		}
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == add) {
			int index = hGradeBox.getSelectedIndex();
			if (index == -1) {
				JOptionPane.showMessageDialog(null, "客房级别不能为空!", "提示",
						JOptionPane.INFORMATION_MESSAGE);
				return;
			}
			String hGrade = (String) hGradeBox.getSelectedItem();
			String hNo = hNoTF.getText().trim();
			if (hNo.equals("")) {
				JOptionPane.showMessageDialog(null, "客房编号不能为空!", "提示",
						JOptionPane.INFORMATION_MESSAGE);
				return;
			}
			int stateIndex = stateBox.getSelectedIndex();
			if (stateIndex == -1 || stateIndex == 1) {
				JOptionPane.showMessageDialog(null, "客房状态不能为空且必须选择'是'!", "提示",
						JOptionPane.INFORMATION_MESSAGE);
				return;
			}
			String state = (String) stateBox.getSelectedItem();
			String beginUseable = getD.getDate(useableTF.getText().trim());
			if (beginUseable == null) {
				JOptionPane.showMessageDialog(null, "开始可用时间必须为如2007-12-09的格式!",
						"提示", JOptionPane.INFORMATION_MESSAGE);
				return;
			}
			String query = "select* from House where HouseNo='" + hNo + "'";
			CachedRowSet crs = null;
			try {
				DBConnection con = new DBConnection();
				crs = con.getResultSet(query);
				if (crs.next()) {
					JOptionPane.showMessageDialog(null, "该记录已经存在!", "提示",
							JOptionPane.INFORMATION_MESSAGE);
					return;
				}
				String insert = "insert into House values('" + hGrade + "','"
						+ hNo + "','" + state + "','" + beginUseable + "')";
				con.addSql(insert);
				con.doDML();
				doIt();
			} catch (SQLException sqle) {
				sqle.printStackTrace();
			} catch (ClassNotFoundException cnfe) {
				cnfe.printStackTrace();
			}
		}
		if (e.getSource() == delete) {
			String hNoGet = (String) JOptionPane.showInputDialog(null,
					"请选择要删除的客房编号!", "", JOptionPane.INFORMATION_MESSAGE, null,
					hNoS, hNoS[0]);
			if (hNoGet == null) {
				return;
			}
			String query = "select* from House where HouseNo='" + hNoGet + "'";
			CachedRowSet crs = null;
			try {
				DBConnection con = new DBConnection();
				crs = con.getResultSet(query);
				if (!crs.next()) {
					JOptionPane.showMessageDialog(null, "该记录不存在!", "提示",
							JOptionPane.INFORMATION_MESSAGE);
					return;
				}
				int confirm = JOptionPane.showConfirmDialog(null, "您真的确认删除吗?",
						"", JOptionPane.YES_NO_OPTION);
				if (confirm == -1 || confirm == JOptionPane.NO_OPTION) {
					return;
				}
				String delete = "delete from House where HouseNo='" + hNoGet
						+ "'";
				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 + -