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

📄 majormagdialog.java

📁 学生管理系统。使用java编程
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	}

	/**
	 * 获得专业介绍
	 */
	private String getMajorInfo() {
		String majorInfo = this.majInfoTextField.getText().toString().trim();
		if (majorInfo.equals("")) {
			return "";
		} else
			return majorInfo;
	}

	/**
	 * 添加方法
	 * 
	 * @param evt
	 */
	private void saveButtonActionPerformed(ActionEvent evt) {
		Vector vecData = new Vector();

		String majorName = this.getMajorName();
		String majorInfo = this.getMajorInfo();

		if (!majorName.equals("")) {
			try {
				Connection conn = null;
				PreparedStatement stmt = null;

				conn = JdbcConnct.fetchConnection();
				// System.out.println(conn);

				String sql = "insert into  major(majorName,majorInfo)"
						+ "values(" + "'" + getMajorName() + "'" + "," + "'"
						+ getMajorInfo() + "'" + ")";
				stmt = conn.prepareStatement(sql);
				int updateLine = stmt.executeUpdate(sql);
				vecData.addElement(getMajorId() - 1);
				vecData.addElement(getMajorName());
				vecData.addElement(getMajorInfo());
				if (updateLine > 0) {

					majorTableModel.addRow(vecData);
					JOptionPane.showMessageDialog(this, "添加专业成功!!\n");
					// 新增、删除、修改按钮可用、保存按钮不可用
					this.newButton.enable(true);
					this.deleteButton.enable(true);
					this.modifyButton.enable(true);

					this.saveButton.enable(false);
				}

			} catch (Exception e) {
				// System.out.println(getMajorId());
				JOptionPane.showMessageDialog(this, "输入错误,请重新输入");
				e.printStackTrace();
			}
		} else
			JOptionPane.showMessageDialog(this, "专业名不可为空");
	}

	/**
	 * 获得查询条件
	 */
	private long getCommentMajorId() {
		try {
			long majorId = Long.parseLong(this.commentTextField.getText());
			return majorId;
		} catch (NumberFormatException e) {
			// JOptionPane.showMessageDialog(this, "专业号必须为数字");
			// return 0;
		}
		return 0;

		// return getMajorId();
	}

	private String getCommentMajorName() {
		String majorName = this.commentTextField.getText().toString();
		return majorName;
	}

	/*
	 * public String getInput() { String input =
	 * this.commentTextField.getText().trim(); // if(input.equals("")) // return
	 * null; // else{
	 * if(this.querComboBox.getSelectedItem().toString().equals("专业号")) { }
	 * if(this.querComboBox.getSelectedItem().toString().equals("专业名")) { return
	 * input; } return input; // } }
	 */

	/**
	 * 查询方法
	 */
	private void querButtonActionPerformed(ActionEvent evt) throws Exception {
		String choose = this.querComboBox.getSelectedItem().toString().trim();
		// 建立一个JDBC对象
		JdbcConnct jdbcConnection = new JdbcConnct();
		Vector vecData = new Vector();
		String sql = null;

		if (choose.equals("全部")) {
			sql = "select * from  major order by majorid asc";
		}

		if (choose.equals("专业号")) {
			sql = "select * from  major where majorId ="
					+ getCommentMajorId();

		}
		if (choose.equals("专业名")) {
			sql = "select * from  major where majorname =" + "'%"
					+ getCommentMajorName() + "%'";
		}

		majorTableModel = new DefaultTableModel(row, column);
		this.majorTable.setModel(majorTableModel);
		majorTable.getTableHeader().setBounds(0, 0, 469, -273);
		// this.CourseQueryTable.setRowMargin(20);
		// 获得数据
		vecData = jdbcConnection.getData(sql);
		// 依次为各行插入数据

		if (vecData.size() == 0) {
			JOptionPane.showMessageDialog(this, "没有你想要查询的结果!");
		} else {
			for (int i = 0; i < vecData.size(); i++) {
				majorTableModel.addRow((String[]) vecData.get(i));
			}
		}

	}

	/**
	 * 删除专业,如果专业和其它表关联则不能删除
	 * 
	 * @param evt
	 */
	private void deleteButtonActionPerformed(ActionEvent evt) {
		// 建立一个JDBC对象
		JdbcConnct jdbcConnection = new JdbcConnct();
		// 定义鼠标点击行
		int nrow = 0;
		// 定义boolean型变量,用户是否点击行,初始化为False
		boolean bisRight = false;

		// 定义SQL语句
		String strSQL = "";
		// 更新行数 int型 updateLine
		int updateLine = 0;
		// 定义用户选择 nuserSelect 是否确认删除
		int nuserSelect = 0;
		// 定义专业号
		String strmajorId = "";

		// 获取鼠标点击行
		nrow = this.majorTable.getSelectedRow();
		// 判断用户是否选择
		if (nrow < 0) {
			bisRight = false;
			JOptionPane.showMessageDialog(this, "您没有选择数据!在删除前请先选择一行数据!");
		} else {
			bisRight = true;
			// 获得专业号
			strmajorId = (String) this.majorTable.getValueAt(nrow, 0);
			// 当前记录是否与其它表关联,有关联则不能删除
			bisRight = !this.isReferenceOtherTable(strmajorId);
		}

		// 如果用户选中专业,且和其它表无关联则可删除该专业
		if (bisRight) {
			try {
				String sql = "delete from major where majorid = " + strmajorId;
				updateLine = jdbcConnection.updateData(sql);
			} catch (Exception e) {
				e.printStackTrace();
				JOptionPane.showMessageDialog(this, "从数据库中删除失败!");

			}
			// 若删除成功则移除专业表中数据,否则提示“删除失败”
			if (updateLine > 0) {
				// 清除专业表中的行
				majorTableModel.removeRow(nrow);
				// 提示删除成功
				JOptionPane.showMessageDialog(this, "删除成功!!");
				// 清除文本框中显示内容
				this.majorIdTextField.setText("");
				this.majorNameTextField.setText("");
				this.majInfoTextField.setText("");
			} else {
				// 提示删除失败
				JOptionPane.showMessageDialog(this, "从数据库中删除失败!");
			}
		}

	}

	/**
	 * 判断该专业是否与班级表、专业非配表关联
	 * 
	 * @param strmajorId
	 * @return
	 */
	private boolean isReferenceOtherTable(String strmajorId) {
		// 定义boolean型变量,当前记录是否与班级表关联,初始化为False
		boolean bisReferenceClass = false;
		// 定义boolean型变量,当前记录是否与学习心理评论表关联,初始化为False
		boolean bisReferenceComment = false;
		// 错误显示语句
		String strErrorView = "";
		// 定义SQL语句
		String strSQL = "";
		// 定义Vector变量,存储从数据库查询来的信息
		Vector vecData = new Vector();

		// 建立一个JDBC对象
		JdbcConnct jdbcConnection = new JdbcConnct();

		// 当前记录是否与其它表关联
		// 组合SQL语句,查询是否与Classes表关联
		strSQL = "select * from  Classes t where  majorid = " + strmajorId;
		try {
			vecData = jdbcConnection.getData(strSQL);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if (vecData.size() > 0) {
			// 如果和学生表有关联,bisReferenceStu置为true
			bisReferenceClass = true;
			strErrorView += "该该专业存在班级,无法删除专业,请重新选择.\n";

		} else {
			bisReferenceClass = false;
		}

		// 组合SQL语句,查询是否与MajorCourse表关联,SQL语句需改成专业信息分配表
		strSQL = "select * from  coumajrmatch cm where  cm.majorid = "
				+ strmajorId;
		// 若果该专业与班级表不关联,则继续判断是否与专业科目分配表关联

		try {
			vecData = jdbcConnection.getData(strSQL);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if (vecData.size() > 0) {
			// 如果和专业课程分配表有关联,bisReferenceMajorCourse置为true
			bisReferenceComment = true;
			strErrorView = "该该专业中有科目,无法删除专业,请重新选择.\n";

		} else {
			bisReferenceComment = false;
		}
		// 如果任何一个表关联,则不能删除
		if (bisReferenceComment || bisReferenceClass) {
			bisReferenceClass = true;
			// 显示错误提示
			JOptionPane.showMessageDialog(this, strErrorView);

		} else {
			bisReferenceClass = false;
		}

		return bisReferenceClass;
	}

	/**
	 * 修改
	 */
	private void modifyButtonactionPerformed(ActionEvent evt) {

		// 建立一个JDBC对象
		JdbcConnct jdbcConnection = new JdbcConnct();
		// 定义鼠标点击行
		int nrow = 0;
		// 定义boolean型变量,用户是否点击行,初始化为False
		boolean bisRight = false;
		// 定义SQL语句
		String strSQL = "";
		// 更新行数 int型 updateLine
		int updateLine = 0;

		// 获取鼠标点击行
		nrow = this.majorTable.getSelectedRow();
		// 判断用户是否选择
		if (nrow < 0) {
			bisRight = false;
			JOptionPane.showMessageDialog(this, "您没有选择数据!在修改前请选择一条数据!");
		} else {
			bisRight = true;
		}

		// 修改数据
		if (bisRight) {

			strSQL = "update  major set majorname = '" + getMajorName()
					+ "',majorInfo=" + "'" + getMajorInfo() + "'"
					+ "where majorid = "
					+ this.majorTableModel.getValueAt(nrow, 0);

			// 更新数据库
			try {
				updateLine = jdbcConnection.updateData(strSQL);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			// 更新成功
			if (updateLine > 0) {
				// 更新用户界面数据
				this.majorTableModel.setValueAt(getMajorName(), nrow, 1);
				this.majorTableModel.setValueAt(getMajorInfo(), nrow, 2);
				JOptionPane.showMessageDialog(this, "修改成功!\n");
			} else {
				JOptionPane.showMessageDialog(this, "修改失败!请重新修改!");
			}
			// }
		}

		int row = this.majorTable.getSelectedRow();

		if (row != -1) {
			String majorId = (String) this.majorTable.getValueAt(row, 0);
			String majorName = (String) this.majorTable.getValueAt(row, 1);
			String majorInfo = (String) this.majorTable.getValueAt(row, 2);

			this.majorIdTextField.setText(majorId);
			this.majorNameTextField.setText(majorName);
			this.majInfoTextField.setText(majorInfo);
			// System.out.println(majorId);

		} else {
			JOptionPane.showMessageDialog(this, "没有选定行");
		}

	}

	private void commentTextFieldMouseClicked(MouseEvent evt) {
		if (this.commentTextField.getText().equals("请输入查询条件")) {
			this.commentTextField.setText("");
		}
	}

	/**
	 * 鼠标点击事件
	 * 
	 * @param evt
	 */
	private void majorTableMouseClicked(MouseEvent evt) {
		// 定义鼠标点击行
		int row = 0;

		// 获取鼠标点击行
		row = this.majorTable.getSelectedRow();
		// 获取行内数据
		String majorId = String.valueOf(this.majorTable.getValueAt(row, 0));
		String majorName = String.valueOf(this.majorTable.getValueAt(row, 1));
		String majorInfo = String.valueOf(this.majorTable.getValueAt(row, 2));

		// 显示鼠标点击数据
		this.majorIdTextField.setText(majorId);
		this.majorNameTextField.setText(majorName);
		this.majInfoTextField.setText(majorInfo);

		// 文本框设为不可编辑
		this.majorIdTextField.enable(false);

	}

	private void newButtonActionPerformed(ActionEvent evt) {
		// 专业号
		long LmajorId = 0;

		// 获得专业号
		try {
			LmajorId = this.getMajorId();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// 新增、删除、修改按钮不可用、保存按钮可用
		this.newButton.enable(false);
		this.deleteButton.enable(false);
		this.modifyButton.enable(false);

		this.saveButton.enable(true);
		// 清空界面数据
		this.majorIdTextField.setText(String.valueOf(LmajorId));
		this.majorNameTextField.setText("");
		this.majInfoTextField.setText("");
		// 专业号不可编辑
		this.majorIdTextField.enable(false);

	}

	private void exitButtonActionPerformed(ActionEvent evt) {
		this.dispose();
	}

}

⌨️ 快捷键说明

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