scoreuniversquerydialog.java

来自「学生管理系统。使用java编程」· Java 代码 · 共 1,902 行 · 第 1/4 页

JAVA
1,902
字号
		} else {
			bisStudent = false;
		}
		//打印是否选择学生		System.out.println("\n 是否选择学生表:"+bisStudent);
		return bisStudent;
	}
    /**
     * 学生成绩查询     * @param evt
     */	
	private void studentQueryButtonActionPerformed(ActionEvent evt) {
		//学生成绩查询		//定义SQL语句		String strSQL = "";
		//定义姓名		String strstuName            =  "";
		
		// 建立一个JDBC对象		JdbcConnct jdbcConnection = new JdbcConnct();
		// 定义Vector变量,存储从数据库查询来的信息		Vector vecData = new Vector();
		
		//鼠标点击行		int nrow = 0;
		//获取鼠标点击行		nrow  = this.ClassQueryTable.getSelectedRow();
		//获取鼠标点击数据		strstuName   = String.valueOf(this.ClassQueryTable.getValueAt(nrow, 1));
		//为查询条件显示赋值		strstuName   +=" 同学 ";
		this.strscoreQueryView = strstuName +this.strscoreQueryView + "成绩查询" ;
		
		//显示查询条件		this.stuQueryViewLabel.setText(this.strscoreQueryView);
			
		//获取用户选择的输入条件		strSQL = this.getStuScoreQuerySQL();
		//打印SQL语句		System.out.println("\n"+ strSQL);
		
		// 刷新table中数据		DefaultTableModel StuScoreTableModel = new DefaultTableModel(row,
				columnStudent);
		this.stuScoreTable.setModel(StuScoreTableModel);

		//根据SQL语句进行查询		
		// 获得数据		try {
			vecData = jdbcConnection.getData(strSQL);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		// 依次为各行插入数据,显示相应数据		if (vecData.size() > 0) {
			this.displayScoreQueryInfo(vecData);

		} else {
			JOptionPane.showMessageDialog(this, "您查询的成绩在数据库中不存在!请换一个查询条件.");
		}		
		
	}
    /**
     * 班级成绩查询,查询一个班的所有科目及某一科目成绩     * @param evt
     */	
	private void classScoreQueryButtonActionPerformed(ActionEvent evt) {
		//班级成绩查询		//定义SQL语句		String strSQL = "";
		
		// 建立一个JDBC对象		JdbcConnct jdbcConnection = new JdbcConnct();
		// 定义Vector变量,存储从数据库查询来的信息		Vector vecData = new Vector();
		
		//获取用户选择的输入条件		strSQL = this.getClassQuerySelect();
		//打印SQL语句		System.out.println("\n"+ strSQL);		
        
		//为查询显示条件赋值		this.strscoreQueryView += "成绩查询" ;		
		//显示查询条件		this.stuQueryViewLabel.setText(this.strscoreQueryView);
		
		// 刷新table中数据		DefaultTableModel StuScoreTableModel = new DefaultTableModel(row,
				columnStudent);
		this.stuScoreTable.setModel(StuScoreTableModel);

		//根据SQL语句进行查询		
		// 获得数据		try {
			vecData = jdbcConnection.getData(strSQL);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		// 依次为各行插入数据,显示相应数据		if (vecData.size() > 0) {
			this.displayScoreQueryInfo(vecData);

		} else {
			JOptionPane.showMessageDialog(this, "您查询的成绩在数据库中不存在!请换一个查询条件.");
		}	
		
	}
	
	/**
	 * 根据用户选择组合学生成绩查询的SQL语句	 * @return String strSQL
	 */
	private String getstuQuerySelect(){
		// 用户在组合框中选择的Item索引		int nUserSelect;
		//用户选择年份		String stryear = "";
		int    nyear   = 0;
		// 定义SQL语句		String strSQL = "";
		//boolean型变量bisfullQuery ,用户是否选择查询全部科目,初始化为false
		boolean bisfullQuery = false;
		//用户输入是否正确,初始化为false
		boolean bisRight     = false;
		// 新建开始时间对象		Date startDate = new Date();
		// 新建开始时间对象		Date endDate = new Date();

		// 定义String类型开始和结束时间		String strStartDate = "";
		String strEndDate = "";
		
		// 新建日期格式化对象		SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd");
		// 新建一个Calendar对象		Calendar calendar = Calendar.getInstance();

		//获得用户输入的年份		stryear      = String.valueOf(this.yearSelectSpinner.getValue());
		
		//校验用户输入		if(stryear.matches(REG_DIGITFLAGYEAR)){
			//如果检验正确			bisRight = true;
			// 数值转换			try {
				nyear = Integer.parseInt(stryear);
			} catch (NumberFormatException e) {
				e.printStackTrace();
			}
		    //初始化设置为所选年的3月1号,Calendar月份从0开始			calendar.set(nyear, 2, 1);
			
		}else{
			bisRight = false;
			JOptionPane.showMessageDialog(this,
					"请不要在年份中输入字母,年份为4位数字,\n如1999、2000等,请重新输入!");				

		}		
        
		//获得用户选择		bisfullQuery = this.fullQueryRadioButton.isSelected();
		//获得用户选择的Item索引		nUserSelect = this.conditionComboBox.getSelectedIndex();
		//用户选择的Item索引加一		nUserSelect++;
		
		if(bisfullQuery){
			//如果选择查询全部成绩			//为查询条件显示变量赋值			this.strscoreQueryView = "全部";			
		}else{
			//如果用户选择查询平时成绩			if(bisRight){
				//如果用户输入正确				bisRight = true;
				switch(nUserSelect){
				case 1:
					//如果用户选择上学期					//为查询条件显示变量赋值					this.strscoreQueryView = stryear +"上学期";
					// 获得开始日期					startDate = calendar.getTime();
					// 加5个月					calendar.add(Calendar.MONTH, 5);
					// 获得结束日期					endDate = calendar.getTime();
					// 将日期格式化为字符串					strStartDate = simpleFormat.format(startDate);
					strEndDate = simpleFormat.format(endDate);
					break;
				case 2:
					//如果用户选择下学期					//为查询条件显示变量赋值					this.strscoreQueryView = stryear +"下学期";
					// 获得开始日期					calendar.set(nyear, 8, 1);
					startDate = calendar.getTime();
					// 加5个月					calendar.add(Calendar.MONTH, 5);
					// 获得结束日期					endDate = calendar.getTime();
					// 将日期格式化为字符串					strStartDate = simpleFormat.format(startDate);
					strEndDate = simpleFormat.format(endDate);
					
					break;
					
					default:								
				}
				//组合SQL语句				strSQL = " and ExamDate >= to_date('"+strStartDate+"','yyyy-mm-dd') and " +
						"ExamDate <= to_date('"+strEndDate +"','yyyy-mm-dd')";				
			}
		}
	
		return strSQL;
	}
	
	/**
	 * 根据用户选择组合SQL查询语句	 * @return String strSQL
	 */
	private String getStuScoreQuerySQL(){
		//学号		String strstudentId   =  "";
		//定义SQL语句		String strSQL = "select sco.studentId, sco.stuName , sco.courseid, sco.courseName,"
			+" to_char(sco.ExamDate, 'yyyy-mm-dd') as ExamDate , sco.examtype, sco.ispresent,sco.examkind, "
			+ "sco.score from view_score  sco";
		String strSQL1 = " where sco.studentId = ";
		String strSQLCompare = "";
		String strSQLEnd = " order by courseId ASC " ;
		//鼠标点击行		int nrow = 0;
		
		//获取鼠标点击行		nrow  = this.ClassQueryTable.getSelectedRow();
		//获取鼠标点击数据		strstudentId = String.valueOf(this.ClassQueryTable.getValueAt(nrow, 0));
		
		//获取用户选择的输入条件		strSQLCompare = this.getstuQuerySelect();
		if(strSQLCompare.length() <= 0 ){
			//若选SQL值为空则选择综合查询			strSQL += strSQL1 + strstudentId + strSQLEnd ;			
		}else{
			//否则组合SQL语句			strSQL += strSQL1 
			+strstudentId +strSQLCompare + strSQLEnd ;			
		}
		
		return strSQL;
	}
	/**
	 * 根据用户选择组合班级成绩查询的SQL子句	 * @return String strSQL
	 */
	private String getClassQuerySelect(){
		//全部查询、按科目查询		//boolean型变量bisfullQuery ,用户是否选择查询全部科目,初始化为false
		boolean bisfullQuery = false;
		//定义班号、班级名		String strclassId    =  "";
		String strclassName  =  "";
		//鼠标点击行		int nrow = 0;
		//用户选择的科目的索引		int nuserSelectItem = 0;
		//定义一维数组aryCourse,存储课程信息		String []aryCourse = new String[2];
		
		//SQL语句		String strSQL = "select sco.studentId, sco.stuName , sco.courseid, sco.courseName,"
				+ "to_char(sco.ExamDate, 'yyyy-mm-dd') as ExamDate, sco.examtype, sco.ispresent,sco.examkind,"
				+ "sco.score "
				+ "from zlb.view_score  sco where sco.studentId IN"
				+ "(select s.studentid  from zlb.student s where  s.classid  = ";
		String strSQL1 = " and sco.courseId = ";
		String strSQLEnd = " order by sco.courseid ASC ";
		
		//获取鼠标点击行		nrow  = this.ClassQueryTable.getSelectedRow();
		//获取用户选择的科目的索引		nuserSelectItem = this.subjectConditionComboBox.getSelectedIndex();
		
		//获取班级号		strclassId = String.valueOf(this.ClassQueryTable.getValueAt(nrow, 0));
		strclassName = String.valueOf(this.ClassQueryTable.getValueAt(nrow, 1));
		
		//获得用户选择		if(this.fullSubjectQueryRadioButton.isSelected()){
			//如果用户选择查询全部科目   
			bisfullQuery = true;
			//为用户查询显示赋值			this.strscoreQueryView =strclassName+ " 全部科目";
			//组合SQL语句			strSQL += strclassId + ")"+ strSQLEnd;
			
		}else{
			//如果用户选择分科目查询			bisfullQuery = false;
			//获得用户选择的课程号			aryCourse = (String[])this.vsubject.get(nuserSelectItem);
			//为用户查询显示赋值			this.strscoreQueryView =strclassName+" "+ aryCourse[1];			
			//组合SQL语句			strSQL += strclassId + ")"+ strSQL1+aryCourse[0]+strSQLEnd;			
		}
		return strSQL;
	}
	

	
	/**
	 * 若用户选择按科目查询则显示该专业的科目	 * @param evt
	 */
	private void subjectQueryRadioButtonActionPerformed(ActionEvent evt) {
		//根据用户输入显示选择科目		//鼠标点击行		int nrow = 0;
		//定义班级号		String strclassId = "";
		//定义SQL语句		String strSQL =  "select cou.courseId, cou.courseName from zlb.view_class_course cou " +
		"where cou.classId = ";
		//定义一维数组aryCourse[],存储课程信息		String []aryCourse = new String[2];
		// 建立一个JDBC对象		JdbcConnct jdbcConnection = new JdbcConnct();
		
		//获取鼠标点击行		nrow  = this.ClassQueryTable.getSelectedRow();
		//获取班级号		strclassId = String.valueOf(this.ClassQueryTable.getValueAt(nrow, 0));
		
		//组合SQL语句		strSQL += strclassId;
		//根据SQL语句进行查询		
		// 获得数据		// 利用Vector变量存储从数据库查询来的信息		try {
			this.vsubject = jdbcConnection.getData(strSQL);
		} catch (SQLException e) {
			e.printStackTrace();
		}		
		
		//科目选择可见		this.subjectConditionComboBox.setVisible(true);
		//显示查询科目信息		//清空课程选择组合框数据		this.subjectConditionComboBox.removeAllItems();
		
		if(this.vsubject.size() > 0){
			for(int i = 0 ; i< this.vsubject.size() ; i++){
				aryCourse = (String[])this.vsubject.get(i);
				//为Combobox添加课程信息				this.subjectConditionComboBox.addItem(aryCourse[1]);
			}			
		}else{
			//该专业的课程不存在,请先为该专业添加课程			JOptionPane.showMessageDialog(this, "该专业的课程不存在,请先为该专业添加课程");
		}		
	}
	
	/**
	 * 显示成绩查询信息	 * @param vecData
	 */
	private void displayScoreQueryInfo(Vector vecData){
		//显示成绩查询信息		//定义一维数组aryScoreInfo存储一条成绩信息		String [] aryScoreInfo = new String[9];
		//定义int型变量 nexamType考试性质		int    nexamType        = 0;
		//定义成绩显示变量 vDataView
		Vector vDataView = new Vector(200);		
		
		//根据状态信息表转换成绩信息		for(int i = 0 ; i < vecData.size(); i++){
			aryScoreInfo =(String []) vecData.get(i);
			//转换考试类型字段			if(aryScoreInfo[5].equals("M")){
				aryScoreInfo[5] = "期中";				
			}else if(aryScoreInfo[5].equals("F")){
				aryScoreInfo[5] = "期末";				
			}
            //转换出勤状态字段			if(aryScoreInfo[6].equals("Y")){
				aryScoreInfo[6] = "参考";	
			}else if(aryScoreInfo[6].equals("N")){
				aryScoreInfo[6] = "缺考";				
			}
            //转换考试性质字段			try{
				nexamType = Integer.parseInt(aryScoreInfo[7]);
			}catch(NumberFormatException e){
				e.printStackTrace();				
			}
			//利用switch语句进行判断			switch(nexamType){
			case 1:
				//考试性质值为1,第一次考试				aryScoreInfo[7] = "第一次考试";				
				
				break;
			case 2:
				//考试性质值为2,补考				aryScoreInfo[7] = "补考";				
				
				break;
			case 3:
				//考试性质值为3,重修				aryScoreInfo[7] = "重修";				
				
				break;
			case 4:
				//考试性质值为4,清考				aryScoreInfo[7] = "清考";			
				
				break;				
				default:
					JOptionPane.showMessageDialog(this, "考试性质号为:"+nexamType+"考试性质不存在.");
			}
			//重新为VecDataView赋值			vDataView.add(aryScoreInfo);
		}
		//显示成绩查询信息		// 刷新table中数据		DefaultTableModel StuScoreTableModel = new DefaultTableModel(row,
				columnStudent);
		this.stuScoreTable.setModel(StuScoreTableModel);
		
		for (int i = 0; i < vDataView.size(); i++) {
			StuScoreTableModel.addRow((String[]) vDataView.get(i));
		}
		// 显示查询数据		this.ScorQueryTabbedPane
				.setSelectedComponent(this.dataViewPanel);
	}
    
	/**
	 * 选择全部科目,科目组合框置为false
	 * @param evt
	 */
	private void fullSubjectQueryRadioButtonActionPerformed(ActionEvent evt) {
		//选择全部科目,科目选择组合框不可见		this.subjectConditionComboBox.setVisible(false);
	}
	/**
	 * 用户选择全部成绩,年份选择置为false
	 * @param evt
	 */
	private void fullQueryRadioButtonActionPerformed(ActionEvent evt) {
		//用户选择全部成绩,年份选择置为false
		this.yearSelectSpinner.setVisible(false);
		this.conditionComboBox.setVisible(false);
	}
	/**
	 * 用户选择全部成绩,年份选择置为false
	 * @param evt
	 */
	private void commonRadioButtonActionPerformed(ActionEvent evt) {
		//用户选择平时成绩,年份选择置为true
		this.yearSelectSpinner.setVisible(true);
		this.conditionComboBox.setVisible(true);
	}

}

⌨️ 快捷键说明

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