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

📄 stumatchclassdialog.java

📁 学生管理系统。使用java编程
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
		//如果班级人数未满		if(bisRight){
			//获得所选学生信息			bisRight = this.getMouseClickData(student, this.StuInfoTable);			
		}
		
		//如果成功得到学生信息,则更新学生的班级		if(bisRight){
			student.setClassId(strClassId);
			bisRight = this.updateStuInfo(student, strClassId);			
		}
		// 如果更新成功则更新界面信息		if (bisRight) {
			// 未选学生人数减一			nUnselectedStuNum--;
			// 移动学生信息			// 从学生信息表中删除信息			this.removeTableData(this.StuInfoTable, this.StuInfoTableModel);
			// 向班级学生信息表中添加信息			this.addTableData(student, this.SelectStuTableModel);
			//更新未选学生人数和已选学生人数			this.unselectedStuNumLabel1.setText(String.valueOf(nUnselectedStuNum));
			this.selectedStuNumLabel.setText(String.valueOf(nselectedStuNum));
		}
		
	}
	/**
	 * 从班级删除学生	 * @param evt
	 */
	private void DeleteButtonActionPerformed(ActionEvent evt) {
		//从班级删除学生		//班级已选学生人数		String strselectedStuNum = "";
		int    nselectedStuNum   = 0;

		//未选学生人数		String strUnselectedStuNum = "";
		int    nUnselectedStuNum   = 0;

		// 定义boolean型变量,是否成功删除学生		boolean bisRight = false;
		//建立学生对象		Student student = new Student();
		
		//获取班级已选学生人数、班级人数、未选学生人数,
		strselectedStuNum    = this.selectedStuNumLabel.getText();
		strUnselectedStuNum  = this.unselectedStuNumLabel1.getText();
		
		//数值转换		try{
			nselectedStuNum = Integer.parseInt(strselectedStuNum);
			nUnselectedStuNum = Integer.parseInt(strUnselectedStuNum);	
			
		}catch(NumberFormatException e){
			e.printStackTrace();
		}
		//已选学生人数减一		nselectedStuNum--;
		//校验班级人数是否大于等于零		if(nselectedStuNum >=  0){
			bisRight  = true; 
		}else{
			bisRight  = false;
			 JOptionPane.showMessageDialog(this, "班级已空,无法再从该班级删除学生!");
		}
		//如果班级人数未满		if(bisRight){
			//获得所选学生信息			bisRight = this.getMouseClickData(student, this.SelectStuTable);			
		}
		
		//如果成功得到学生信息,则更新学生的班级		if(bisRight){
			student.setClassId(null);
			bisRight = this.updateStuInfo(student, null);			
		}
		// 如果更新成功则更新界面信息		if (bisRight) {
			// 未选学生人数减一			nUnselectedStuNum++;
			// 移动学生信息			// 从班级学生信息表中删除信息			this.removeTableData(this.SelectStuTable,this.SelectStuTableModel);
			// 向学生信息表中添加信息			this.addTableData(student, this.StuInfoTableModel);
			//更新未选学生人数和已选学生人数			this.unselectedStuNumLabel1.setText(String.valueOf(nUnselectedStuNum));
			this.selectedStuNumLabel.setText(String.valueOf(nselectedStuNum));
		}
	}
	
	/**
	 * 判断输入日期是否合法	 * 
	 * @param sDate
	 * @return
	 */
	public boolean isValidDate(String sDate) {
		String datePattern1 = "\\d{4}-\\d{2}-\\d{2}";
		String datePattern2 = "^((\\d{2}(([02468][048])|([13579][26]))"
				+ "[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|"
				+ "(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?"
				+ "((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?("
				+ "(((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?"
				+ "((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))";
		if ((sDate != null)) {
			Pattern pattern = Pattern.compile(datePattern1);
			Matcher match = pattern.matcher(sDate);
			if (match.matches()) {
				pattern = Pattern.compile(datePattern2);
				match = pattern.matcher(sDate);
				return match.matches();
			} else {
				// JOptionPane.showMessageDialog(
				// this,"日期格式非法,应为YYYY-MM-DD格式,请重新输入!");
				return false;
			} // end if...else
		}
		// JOptionPane.showMessageDialog(this, "日期格式非法,应为YYYY-MM-DD格式,请重新输入!");
		return false;
	}
	
	
	/**
	 * updateStuInfo() 更新学生信息,并返回更新是否成功	 * @param student
	 * @param strClassI
	 * @return boolean型,bisUpdated 学生信息是否更新成功	 */
	private boolean  updateStuInfo(Student student,
			String strClassId){
		//更新学生信息		// 建立JDBC对象		JdbcConnct jdbcConnection = new JdbcConnct();
		// 定义SQL语句		String strSQL = "";
		// 定义SQL语句执行返回值 int updateLine;
		int updateLine = 0;

		// 定义boolean型变量,是否成功更新		boolean bisUpdated = false;

		//组合SQL语句		strSQL = "update  student s set classId = "+ strClassId+ " where s.studentid = "
				+ student.getStudentId();
		//更新学生信息		// 执行SQL语句		try {
			updateLine = jdbcConnection.updateData(strSQL);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if (updateLine > 0) {
			bisUpdated  = true;
		} else {
			bisUpdated = false;
			 JOptionPane.showMessageDialog(this, "学生信息更新失败,无法为学生分配班级失败!");
		}
		return bisUpdated;
	}
	
	/**
	 * 获取用户点击数据	 * @param  Student类对象 student
	 * @return Student类对象 student
	 */
	private boolean getMouseClickData(Student student,JTable StuInfoTable){
		// 定义Student类属性,学号、姓名、性别、年龄、生日、班级编号、入学日期、		String strstudentId   = "";
		String strstuName = "";
		String strgender      = "";
		String strAge  = "";
		String strbirthDay = "";
		String strClassId  = "";
		String strEnterTime = "";

		int nstudentId = 0;
		int nAge = 0;
		// 定义boolean型变量,是否成功,初始化为false
		boolean bisRight = false;
		// 定义鼠标点击行		int nrow = 0;

		// 获取鼠标点击行		nrow = StuInfoTable.getSelectedRow();
		
		if(nrow < 0){
			//如果用户没有选择行			bisRight = false;
			 JOptionPane.showMessageDialog(this, "您没有选择数据,在增加或删除学生前,请先选择一个学生!");
		}else{
			//如果用户选择行			bisRight = true;
			// 获取行内数据			strstudentId      =  String.valueOf(StuInfoTable.getValueAt(nrow, 0));
			strstuName    =  String.valueOf(StuInfoTable.getValueAt(nrow, 1));
			strgender =  String.valueOf(StuInfoTable.getValueAt(nrow,2));
			strAge      =  String.valueOf(StuInfoTable.getValueAt(nrow, 3));
			strbirthDay       =  String.valueOf(StuInfoTable.getValueAt(nrow, 4));
			strClassId       =  String.valueOf(StuInfoTable.getValueAt(nrow, 5));
			strEnterTime     =  String.valueOf(StuInfoTable.getValueAt(nrow, 6));
			// 类型转换			try {
				nstudentId = Integer.parseInt(strstudentId);
				nAge = Integer.parseInt(strAge);
			} catch (NumberFormatException e) {
				e.printStackTrace();
			}			
		}
		// 为Student对象赋值		student.setValue(nstudentId, strstuName, strgender, nAge,
				strbirthDay,strClassId, strEnterTime);
		return bisRight;
	}
	/**
	 * 从表格中删除一行数据	 * @param StuInfoTable     表格	 * @param SelectStuTableModel  表格模型	 */
	private void removeTableData(JTable StuInfoTable,
			DefaultTableModel SelectStuTableModel){
		// 定义鼠标点击行		int nrow = 0;		
		// 获取鼠标点击行		nrow = StuInfoTable.getSelectedRow();
		//从表格中删除一行数据		SelectStuTableModel.removeRow(nrow);
	}
    /**
     * 向表格添加一行数据     * @param student           Student类对象     * @param SelectStuTableModel  表格模型     */
	private void addTableData(Student student,DefaultTableModel SelectStuTableModel){
		//向表格模型中添加一行数据 学号、姓名、性别、年龄、生日、班级号、入学时间		SelectStuTableModel.addRow(new String[] {
				String.valueOf(student.getStudentId()),
				student.getStudentName(), 
				student.getGender(),
				String.valueOf(student.getAge()),
				student.getBirthDay(),
				student.getClassId(),
				student.getEnterTime() });
	}

	
	
	/**
	 * Student类,用于存储学生信息	 * 
	 * @author Administrator
	 * 
	 */
	class Student {
		// 定义Student类属性,学号、姓名、性别、年龄、生日、班级号、入学日期、		private int nStudentId;

		private String strStuName;

		private String strgender;
		
		private int nage;

		private String strbirthDay;
		
		private String strClassId;

		private String strEnterTime;
		

		/**
		 * Student类含参构造函数		 * 
		 * @param nStudentId  学号		 * @param strStuName  姓名		 * @param strgender   性别		 * @param nage        年龄 
		 * @param strbirthDay 生日		 * @param strClassId  班级号		 * @param strEnterTime入学日期		 */
		public Student(int nStudentId, String strStuName, String strgender,
				int nage, String strbirthDay , 
				String strClassId,String strEnterTime) {
			this.nStudentId        = nStudentId;
			this.strStuName        = strStuName;
			this.strgender         = strgender;
			this.nage              = nage;
			this.strbirthDay       = strbirthDay;
			this.strClassId        = strClassId;
			this.strEnterTime      = strEnterTime;
		}

		/**
		 * Student类无参构造函数,变量初始化为空或0
		 * 
		 */
		public Student() {
			this.nStudentId = 0;
			this.strStuName = "";
			this.strgender  = "";
			this.nage       = 0;
			this.strbirthDay = "";
			this.strEnterTime = "";
		}

		/**
		 * 属性器setStudentId(),为StudentId赋值		 * 
		 * @param nStudentId
		 */
		public void setStudentId(int nStudentId) {
			this.nStudentId = nStudentId;
		}

		/**
		 * 属性器setStuName(),为StuName赋值		 * 
		 * @param strStuName
		 */
		public void setStuName(String strStuName) {
			this.strStuName = strStuName;
		}

		/**
		 * 属性器setgender(),为gender赋值		 * 
		 * @param strgender
		 */
		public void setgender(String strgender) {
			this.strgender = strgender;
		}
		/**
		 *属性器setAge(),为Age赋值 
		 * @param nAge
		 */
		public void setAge(int nAge) {
			this.nage = nAge;
		}
		

		/**
		 * 属性器setBirthDay(),为BirthDay赋值		 * 
		 * @param strbirthDay
		 */
		public void setBirthDay(String strbirthDay) {
			this.strbirthDay = strbirthDay;
		}
		/**
		 * 属性器setClassId(),为ClassId赋值		 * @param strClassId
		 */
		public void setClassId(String strClassId) {
			this.strClassId = strClassId;
		}		
		
        /**
         * 属性器setEnterTime(),为EnterTime赋值         * @param strEnterTime
         */
		public void setEnterTime(String strEnterTime) {
			this.strEnterTime = strEnterTime;
		}		


		/**
		 * 属性器getStudentId(),返回StudentId
		 * 
		 * @return
		 */
		public int getStudentId() {
			return this.nStudentId;
		}

		/**
		 * 属性器getStudentName()
		 * 
		 * @return 返回String型 StudentName
		 */
		public String getStudentName() {
			return this.strStuName;
		}

		/**
		 * 属性器getGender()
		 * 
		 * @return 返回int型 strgender
		 */
		public String getGender() {
			return this.strgender;
		}
		
		/**
		 * 属性器getAge()
		 * 
		 * @return 返回int型 nAge
		 */
		public int getAge() {
			return this.nage;
		}		
		/**
		 *  属性器getBirthDay()
		 * @return 返回String型变量 BirthDay
		 */
		public String getBirthDay() {
			return this.strbirthDay;
		}
		
		/**
		 *  属性器getClassId()
		 * @return 返回String型变量 ClassId
		 */
		public String getClassId() {
			return this.strClassId;
		}
		
		/**
		 * 属性器getFlagYear()
		 * 
		 * @return 返回int型 FlagYear
		 */
		public String getEnterTime() {
			return this.strEnterTime;
		}

		/**
		 * 属性器setValue(),为class类属性赋值		 * 
		 * @param nStudentId
		 *            学号		 * @param strStuName
		 *            姓名		 * @param strgender
		 *            性别		 * @param nage
		 *            年龄           
		 * @param strbirthDay
		 *            生日		 * @param strClassId
		 *            班级号		 * @param strEnterTime
		 *            入学日期		 */
		public void setValue(int nStudentId, String strStuName, String strgender,
				int nage, String strbirthDay , String strClassId, String strEnterTime) {
			this.nStudentId        = nStudentId;
			this.strStuName        = strStuName;
			this.strgender         = strgender;
			this.nage              = nage;
			this.strbirthDay       = strbirthDay;
			this.strClassId        = strClassId;
			this.strEnterTime      = strEnterTime;
		}

	}
	
}

⌨️ 快捷键说明

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