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

📄 classmagdialog.java

📁 学生管理系统。使用java编程
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
				nmajorId   = Integer.parseInt(strmajorId);
				nstuNum = Integer.parseInt(strstuNum);
				nFlagYear = Integer.parseInt(strFlagYear);
			} catch (NumberFormatException e) {
				e.printStackTrace();
			}
			// 校验学生人数
			if (nstuNum > 100) {
				bisRight = false;
				JOptionPane.showMessageDialog(this,
						"您输入的学生人数超出范围。\n学生人数在(0,1000]内,请重新输入!");
			} else {
				bisRight = true;
			}
			// 校验班主任ID
			if (nteacherId > 1000) {
				bisRight = false;
				JOptionPane.showMessageDialog(this,
						"您输入的班主任ID超出范围。\n班主任ID在(0,1000]内,请重新输入!");
			} else {
				bisRight = true;
			}
			// 校验专业号
			if (nmajorId > 1000) {
				bisRight = false;
				JOptionPane.showMessageDialog(this,
						"您输入的专业号超出范围。\n专业号在(0,1000]内,请重新输入!");
			} else {
				bisRight = true;
			}
			if (bisRight) {
				// 为Class对象赋值
				newclass.setValue(nclassID, strclassName, nteacherId, nmajorId,
						nstuNum, nFlagYear);
			}
		}

		return newclass;
	}

	private void NewButtonActionPerformed(ActionEvent evt) {
		// 新增班级
		// 建立一个JDBC对象
		JdbcConnct jdbcConnection = new JdbcConnct();
		// 定义Vector变量,存储从数据库查询来的信息
		Vector vecData = new Vector();
		// 定义SQL语句
		String strSQL = "";
		// 定义int型变量,MaxClassID,最大班级号
		int MaxClassID = 0;
		String[] strAryMaxClassID = new String[1];

		// 清空文本框
		this.ClassIdTextField.setText("");
		this.ClassNameTextField.setText("");
		this.TeacherIdTextField.setText("");
		this.majorIDTextField.setText("");
		this.StuNumTextField.setText("");
		this.FlagYearTextField.setText("");

		// 新增、修改、删除按钮不可用,保存按钮可用
		this.NewButton.setEnabled(false);
		this.ModifyButton.setEnabled(false);
		this.DeleteButton.setEnabled(false);
		this.SaveButton.setEnabled(true);
		// 班级号自动增长,查询班级号,加一显示班级号
		// 查询学科信息表Course中最大学科号
		strSQL = " Select max(classId) from teach_clas_view ";
		try {
			vecData = jdbcConnection.getData(strSQL);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		for (int i = 0; i < vecData.size(); i++) {

			strAryMaxClassID = (String[]) vecData.get(i);
		}

		try {
			MaxClassID = Integer.parseInt(strAryMaxClassID[0]);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		// MaxCouID加一
		MaxClassID++;
		System.out.println(MaxClassID);
		// 课程号文本框显示,置为False
		this.ClassIdTextField.setText(String.valueOf(MaxClassID));
		this.ClassIdTextField.setEnabled(false);
	}

	private void SaveButtonActionPerformed(ActionEvent evt) {
		// 保存
		// 定义boolean型变量,是否成功保存
		boolean bisSaved = false;
		
		// 定义Course对象
		Class newclass = new Class();

		//保存用户输入信息,并返回保存是否成功,同时获得Class对象值
		bisSaved = this.saveUserInput(newclass); 
		//如果用户保存成功
		if (bisSaved) {
			JOptionPane.showMessageDialog(this, "成功保存!");
			// 用户界面增加数据
			ClassQueryTableModel.addRow(new String[] { 
					String.valueOf(newclass.getClassId()),
					newclass.getClassName(),
					String.valueOf(newclass.getTeacherId()),
					newclass.getTeacherName(),
					String.valueOf(newclass.getMajorId()),
					newclass.getMajorName(),
					String.valueOf(newclass.getStuNum()),
					String.valueOf(newclass.getFlagYear()) });

			// 新增、修改、删除可用、保存不可用
			this.NewButton.setEnabled(true);
			this.ModifyButton.setEnabled(true);
			this.DeleteButton.setEnabled(true);
			this.SaveButton.setEnabled(false);
			// 学科号自动增长,显示学科号,文本框设为不可用
			this.ClassIdTextField.setEnabled(false);			
		} else {
			JOptionPane.showMessageDialog(this, "保存失败!请重试!");
		}
		
	}

	private void DeleteButtonActionPerformed(ActionEvent evt) {
		// 建立一个JDBC对象
		JdbcConnct jdbcConnection = new JdbcConnct();
		// 定义鼠标点击行
		int nrow = 0;
		// 定义boolean型变量,用户是否点击行,初始化为False
		boolean bisRight = false;
		// 定义boolean型变量,当前记录是否与学生表关联,初始化为False
		boolean bisReferenceStu = false;
		// 定义SQL语句
		String strSQL = "";
		// 更新行数 int型 updateLine
		int updateLine = 0;
		// 定义用户选择 nuserSelect
		int nuserSelect = 0;
		// 定义变量班级号
		String strclassId = "";
		// 定义Vector变量,存储从数据库查询来的信息
		Vector vecData = new Vector();

		// 获取鼠标点击行
		nrow = this.ClassQueryTable.getSelectedRow();
		// 判断用户是否选择
		if (nrow < 0) {
			bisRight = false;
			JOptionPane.showMessageDialog(this, "您没有选择数据!在删除前请先选择一行数据!");
		} else {
			bisRight = true;
		}
		// 获取行内数据
		strclassId = String.valueOf(this.ClassQueryTable.getValueAt(nrow, 0));

		// 当前记录是否与其它表关联
		// 组合SQL语句,查询是否与student表关联
		strSQL = "select StudentId from student t where  ClassId = "
				+ strclassId;
		try {
			vecData = jdbcConnection.getData(strSQL);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if (vecData.size() > 0) {
			// 如果和学生表有关联,bisReferenceStu置为true
			bisReferenceStu = true;
			bisRight = false;
			JOptionPane.showMessageDialog(this, "该班级中有学生,无法删除班级,请重新选择.");
			
		} else {
			bisReferenceStu = false;
			bisRight = true;
		}

		if (bisRight) {
			// 删除用户选择的行数据
			nuserSelect = JOptionPane.showConfirmDialog(this, "是否确认删除?",
					"用户确认", JOptionPane.YES_NO_OPTION);
			// System.out.println("\t" + nuserSelect+"\t"+ strcourseId);
			// 如果用户选择删除
			if (nuserSelect == 0) {
				strSQL = "delete from classes where ClassId = "
						+ strclassId;
				try {
					updateLine = jdbcConnection.updateData(strSQL);
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			// 判断是否删除成功
			if (updateLine > 0) {
				// 从用户界面上删除数据
				this.ClassQueryTableModel.removeRow(nrow);

				JOptionPane.showMessageDialog(this, "成功删除!");
			} else {
				JOptionPane.showMessageDialog(this, "删除失败!请重新操作.");
			}
		}
		// 注意:若删除信息与其他表关联,则不可删除
	}
	
	/**
	 * 保存用户输入信息,并返回保存是否成功,同时更新Class对象值
	 * @param newclass Class类对象
	 * @return  boolean型,bisSaved,用户输入是否保存成功
	 */
	private boolean  saveUserInput(Class newclass){
		//保存用户输入数据
		// 建立JDBC对象
		JdbcConnct jdbcConnection = new JdbcConnct();
		// 定义SQL语句
		String strSQL = "";
		// 定义SQL语句执行返回值 int updateLine;
		int updateLine = 0;
		// 定义Vector变量,存储从数据库查询来的信息
		Vector vecData = new Vector();

		// 定义String一维数组,存储班主任名、
		String[] strAryTeacherName = new String[1];
		// 定义String一维数组,存储专业名、
		String[] strAryMajorName = new String[1];
		// 定义boolean型变量,用户操作是否正确
		boolean bisRight = false;
		// 定义boolean型变量,是否成功保存
		boolean bisSaved = false;

		// 利用对象Class 获取用户输入信息
		newclass = getUserInput(newclass);
		// 用户输入校验
		if (newclass.getClassName().equals(null)
				|| newclass.getTeacherId() > 1000 
				|| newclass.getStuNum() > 100
				|| newclass.getMajorId() >1000
				|| !String.valueOf(newclass.getFlagYear()).matches(REG_DIGFlagYear)) {
			bisRight = false;
		} else {
			bisRight = true;
		}


		// 获取教师名,若该教师不存在则给出提示
		strSQL = "select TeacherName from teacher  where TeacherId = "
				+ newclass.getTeacherId();
		// 从数据库中查询教师名
		try {
			vecData = jdbcConnection.getData(strSQL);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if (vecData.size() > 0) {
			bisRight = true;
			for (int i = 0; i < vecData.size(); i++) {
				strAryTeacherName = (String[]) vecData.get(i);
			}
			//为Class对象变量TeacherName赋值
			newclass.setTeacherName(strAryTeacherName[0]);
		} else {
			bisRight = false;
			JOptionPane.showMessageDialog(this, "工号为" + newclass.getTeacherId()
					+ "的老师不存在,请重输!");
		}
		
		// 获取专业名,若该专业不存在则给出提示
		strSQL = "select MajorName from major  where MajorId = "
				+ newclass.getMajorId();
		//如果该教师存在,从数据库中查询专业名
		if (bisRight) {
			try {
				vecData = jdbcConnection.getData(strSQL);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			if (vecData.size() > 0) {
				bisRight = true;
				for (int i = 0; i < vecData.size(); i++) {
					strAryMajorName = (String[]) vecData.get(i);
				}
				// 为Class对象变量TeacherName赋值
				newclass.setMajorName(strAryMajorName[0]);
			} else {
				bisRight = false;
				JOptionPane.showMessageDialog(this, "专业号为"
						+ newclass.getMajorId() + "的专业不存在,请重输!");
			}
		}
		//如果不与其它表关联,向数据库写入数据
		if (bisRight) {
			// 创建SQL语句
			strSQL = "insert into classes ( ClassName, TeacherId ,MajorId ,StuNum ,FlagYear) values ('"
					+ newclass.getClassName()
					+ "',"
					+ newclass.getTeacherId()
					+ ","
					+newclass.getMajorId()
					+","
					+ newclass.getStuNum()
					+ ",'"
					+ newclass.getFlagYear() + "')";
			// 执行SQL语句
			try {
				updateLine = jdbcConnection.updateData(strSQL);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			if (updateLine > 0) {
				System.out.println(updateLine);
				bisSaved  = true;
			} else {
				bisSaved = false;
			}
		}
		return bisSaved;
	}
	
	/**
	 * 更新用户输入信息,并返回更新是否成功,同时更新Class对象值
	 * @param newclass
	 * @return boolean型,bisUpdated 用户输入是否更新成功
	 */
	private boolean  updateUserInput(Class newclass){
		//保存用户输入数据
		// 建立JDBC对象
		JdbcConnct jdbcConnection = new JdbcConnct();
		// 定义SQL语句
		String strSQL = "";
		// 定义SQL语句执行返回值 int updateLine;
		int updateLine = 0;
		// 定义Vector变量,存储从数据库查询来的信息
		Vector vecData = new Vector();

		// 定义String一维数组,存储班主任名、
		String[] strAryTeacherName = new String[1];
		// 定义String一维数组,存储专业名、
		String[] strAryMajorName = new String[1];
		// 定义boolean型变量,用户操作是否正确
		boolean bisRight = false;
		// 定义boolean型变量,是否成功保存
		boolean bisUpdated = false;

		// 利用对象Class 获取用户输入信息
		newclass = getUserInput(newclass);
		//对用户输入进行校验,班级名、班主任工号、学生人数、
		if (newclass.getClassName().equals(null)
				|| newclass.getTeacherId() > 1000 
				|| newclass.getMajorId() >1000
				|| newclass.getStuNum() > 100
				||!String.valueOf(newclass.getFlagYear()).matches(REG_DIGFlagYear)) {
			bisRight = false;
		} else {
			bisRight = true;
		}


		// 获取教师名,若该教师不存在则给出提示
		strSQL = "select TeacherName from teacher  where TeacherId = "
				+ newclass.getTeacherId();
		// 从数据库中查询教师名
		try {
			vecData = jdbcConnection.getData(strSQL);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if (vecData.size() > 0) {
			bisRight = true;
			for (int i = 0; i < vecData.size(); i++) {
				strAryTeacherName = (String[]) vecData.get(i);
			}
			//为Class对象变量TeacherName赋值
			newclass.setTeacherName(strAryTeacherName[0]);
		} else {
			bisRight = false;
			JOptionPane.showMessageDialog(this, "工号为" + newclass.getTeacherId()
					+ "的老师不存在,请重输!");
		}
		
		// 获取专业名,若该专业不存在则给出提示
		strSQL = "select MajorName from major  where MajorId = "
				+ newclass.getMajorId();
		//如果教师存在,从数据库中查询专业名
		if (bisRight) {
			try {
				vecData = jdbcConnection.getData(strSQL);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			if (vecData.size() > 0) {
				bisRight = true;
				for (int i = 0; i < vecData.size(); i++) {
					strAryMajorName = (String[]) vecData.get(i);
				}
				// 为Class对象变量TeacherName赋值
				newclass.setMajorName(strAryMajorName[0]);
			} else {

⌨️ 快捷键说明

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