📄 stumatchclassdialog.java
字号:
this.setSize(900, 503);
} catch (Exception e) {
e.printStackTrace();
}
}
private void ExitButtonActionPerformed(ActionEvent evt) {
//退出 this.dispose();
}
/**
* 根据用户选择匹配比较符号
* @param evt
*/
private void stuQueryConditionComboBoxActionPerformed(ActionEvent evt) {
//根据用户选择匹配比较符号 //用户选择查询条件的索引 int nuserSelect = 0;
//定义颜色对象,默认为blue
Color color = Color.BLUE;
//获得用户选择的查询条件索引 nuserSelect = this.stuQueryConditionComboBox.getSelectedIndex();
//索引加一 nuserSelect++;
switch(nuserSelect){
case 1:
case 4:
//如果用户选择学号或年龄 //清空比较符号组合框 this.compareSymbolComboBox.removeAllItems();
//为组合框添加比较符号 this.compareSymbolComboBox.addItem(" >");
this.compareSymbolComboBox.addItem(" <");
this.compareSymbolComboBox.addItem(" =");
//用户输入文本框置为可见、可用,时间输入框不可见、不可用 this.userInputTextField.setVisible(true);
this.userInputTextField.setEnabled(true);
this.dateInputTextField.setVisible(false);
this.dateInputTextField.setEnabled(false);
//日期输入标签还原为普通颜色,日期输入标签不可见 color = Color.black;
this.queryByDateLabel.setForeground(color);
this.queryByDateLabel.setVisible(false);
break;
case 2:
case 3:
//如果用户选择姓名或性别 //清空比较符号组合框 this.compareSymbolComboBox.removeAllItems();
//为组合框添加比较符号 this.compareSymbolComboBox.addItem("匹配");
this.compareSymbolComboBox.addItem("=");
////用户输入文本框置为可见、可用,时间输入框不可见、不可用 this.userInputTextField.setVisible(true);
this.userInputTextField.setEnabled(true);
this.dateInputTextField.setVisible(false);
this.dateInputTextField.setEnabled(false);
//日期输入标签还原为普通颜色,日期输入标签不可见 color = Color.black;
this.queryByDateLabel.setForeground(color);
this.queryByDateLabel.setVisible(false);
break;
case 5:
case 6:
//如果用户选择生日或入学时间 //清空比较符号组合框 this.compareSymbolComboBox.removeAllItems();
//为组合框添加比较符号 this.compareSymbolComboBox.addItem(" >");
this.compareSymbolComboBox.addItem(" <");
this.compareSymbolComboBox.addItem(" =");
//用户输入文本框置为不可见、不可用,时间输入框可见、可用 this.userInputTextField.setVisible(false);
this.userInputTextField.setEnabled(false);
this.dateInputTextField.setVisible(true);
this.dateInputTextField.setEnabled(true);
//日期输入标签蓝色字符显示,日期输入标签可见 color = Color.BLUE;
this.queryByDateLabel.setForeground(color);
this.queryByDateLabel.setVisible(true);
break;
default:
JOptionPane.showMessageDialog(this, "您所选的查询条件没有匹配的比较符。");
}
}
/**
* 根据用户输入获取SQL语句 * @return String strSQL SQL子句 */
private String getUserSelect(){
//用户选择查询条件的索引 int nuserSelect = 0;
//用户输入是否正确,正确则生成SQL子句,初始化为空 boolean isRight = false ;
//SQL子句 String strSQL = "";
//List控件中显示信息 String strListView = "";
//定义用户查询条件、比较符号及用户输入 String strQueryCondition = "";
String strLogicalSymbol = "";
String strUserInput = "";
String strUserDateInput = "";
//取得用户查询条件、比较符号及用户输入 strQueryCondition = this.stuQueryConditionComboBox.getSelectedItem().toString();
strLogicalSymbol = this.compareSymbolComboBox.getSelectedItem().toString();
strUserInput = this.userInputTextField.getText();
strUserDateInput = this.dateInputTextField.getText();
//获得用户选择的查询条件索引 nuserSelect = this.stuQueryConditionComboBox.getSelectedIndex();
//索引加一 nuserSelect++;
switch(nuserSelect){
case 1:
//如果用户选择学号 if(strUserInput.matches(REG_DITSTUNUM)){
//用户校验成功 isRight = true;
//组合SQL子句 strSQL = " studentId " +strLogicalSymbol + " "+ strUserInput ;
strListView = strQueryCondition +" "+strLogicalSymbol + " "+ strUserInput;
}else{
//用户校验失败 isRight = false;
JOptionPane.showMessageDialog(this, "您输入的学号中含有字母或其他符号,学号为第一位不为零的10位数字,\n"
+"请重新输入.");
}
break;
case 2:
//如果用户选择姓名 if(strUserInput.length() > 40){
//用户校验失败 isRight = false;
JOptionPane.showMessageDialog(this, "您输入的姓名长度超出范围,姓名长度不超过20个字符,\n"
+"请重新输入.");
}else{
//用户校验成功 if(strLogicalSymbol.equals("匹配")){
//如果用户选择匹配 isRight = true;
//组合SQL子句 strSQL = " stuName " +" Like " + " '"+ strUserInput +"%' " ;
strListView = strQueryCondition +" "+strLogicalSymbol + " "+ strUserInput;
}else if(strLogicalSymbol.equals("=")){
isRight = true;
//组合SQL子句 strSQL = " stuName " +strLogicalSymbol + " "+ strUserInput ;
strListView = strQueryCondition +" "+strLogicalSymbol + " "+ strUserInput;
}
}
break;
case 4:
//如果用户选择年龄 //校验用户输入的年龄 isRight = this.isRightAge(strUserInput);
//如果年龄校验成功 if(isRight){
//组合SQL子句 strSQL = " age " +strLogicalSymbol + " "+ strUserInput ;
strListView = strQueryCondition +" "+strLogicalSymbol + " "+ strUserInput;
}else{
//如果年龄校验失败 isRight = false;
}
break;
case 3:
//如果用户选择性别 //判断用户输入的长度 if(strUserInput.length() > 2){
isRight = false;
JOptionPane.showMessageDialog(this, "您输入性别超过两个字符,性别为‘男’或‘女’,\n"
+"请重新输入.");
}else if(strUserInput.equals("男")){
isRight = true;
//组合SQL子句 strSQL = " gender " +" = " + "'M'" ;
strListView = strQueryCondition +" "+strLogicalSymbol + " "+ strUserInput;
}else if(strUserInput.equals("女")){
isRight = true;
//组合SQL子句 strSQL = " gender " +" = " + "'W'" ;
strListView = strQueryCondition +" "+strLogicalSymbol + " "+ strUserInput;
}else{
//用户校验失败 isRight = false;
JOptionPane.showMessageDialog(this, "性别为‘男’或‘女’,"
+"请重新输入.");
}
break;
case 5:
//如果用户选择生日 //校验用户输入 if(isValidDate(strUserDateInput)){
//如果校验成功 isRight = true;
//组合SQL子句 strSQL = " birthDay " +strLogicalSymbol + " "
+ "to_date('"+strUserDateInput +"','yyyy-mm-dd')" ;
strListView = strQueryCondition +" "+strLogicalSymbol + " "+ strUserDateInput;
}else{
isRight = false;
JOptionPane.showMessageDialog(this, "日期输入错误,"
+"请重新选择.");
}
break;
case 6:
//如果用户选择入学时间 //校验用户输入 if(this.isValidDate(strUserDateInput)){
//如果校验成功 isRight = true;
//组合SQL子句 strSQL = " enterTime " +strLogicalSymbol + " "
+ "to_date('"+strUserDateInput +"','yyyy-mm-dd')" ;
strListView = strQueryCondition +" "+strLogicalSymbol + " "+ strUserDateInput;
}else{
isRight = false;
JOptionPane.showMessageDialog(this, "入学时间输入错误,"
+"请重新选择.");
}
break;
default:
JOptionPane.showMessageDialog(this, "您所选的查询条件没有匹配的比较符。");
}
//根据用户选择添加无、与、或条件 if(!isRight){
//如果用户输入检验失败,查询条件减一 ncount--;
}
// 对用户选择进行操作 none and or
if (this.nullRadioButton.isSelected()) {
} else if (this.andjRadioButton.isSelected()) {
// 添加按钮置为可用 this.addButton.setEnabled(true);
strListView = "并且 " + strListView;
strSQL = "and " + strSQL;
} else {
// 添加按钮置为可用 this.addButton.setEnabled(true);
strListView = "或者 " + strListView;
strSQL = "or " + strSQL;
}
//如果用户输入校验成功则在List控件中显示查询信息 if(isRight){
this.vecListView.add(strListView);
// 向List控件中写入数据 this.sqlViewList1.setListData(vecListView);
}
return strSQL;
}
/**
* 校验用户输入年龄是否正确
* @param strStuAge String型
* @return isRightAge boolean型 ,校验成功则返回true,否则false
*/
private boolean isRightAge(String strStuAge){
//年龄 int nStuAge = 0;
//用户是否输入正确年龄 boolean isRightAge = false ;
//校验用户输入 if(strStuAge.matches(REG_DITAGE)){
//数值转换 try{
nStuAge = Integer.parseInt(strStuAge);
}catch(NumberFormatException e){
e.printStackTrace();
}
if(nStuAge > 200){
isRightAge = false;
JOptionPane.showMessageDialog(this, "您输入的年龄大于200,年龄在[1,200]间,\n" +
"请重新输入.");
}else
isRightAge = true;
}else{
isRightAge = false;
JOptionPane.showMessageDialog(this, "年龄中不能输入字母,年龄在[1,200]间,\n" +
"请重新输入.");
}
return isRightAge;
}
private void stuQueryButtonActionPerformed(ActionEvent evt) {
//查询 // 建立JDBC对象 JdbcConnct jdbcConnection = new JdbcConnct();
// 定义Vector变量,存储从数据库查询来的信息 Vector vecData = new Vector();
//班级号strClassId
String strClassId = "";
// 定义SQL语句组合项 String strSQL = "select StudentId, StuName, gender , age ,BirthDay, ClassId, EnterTime " +
"from student ";
String strSQL1 = " where ";
String strSQL2 = " and ClassId != ";
String strTemp = " order by studentid ASC";
//定义查询到学生人数 String strselectedStuNum = "";
//获得班级号 strClassId = this.classIdViewLabel.getText();
// 组合SQL语句 strSQL2 +=strClassId ;
if (this.vecSQL.isEmpty()) {
strSQL += strTemp;
} else {
for (int i = 0; i < this.vecSQL.size(); i++) {
strSQL1 += this.vecSQL.get(i).toString();
}
strSQL += strSQL1 + strSQL2 + strTemp;
// 刷新table中数据 StuInfoTableModel = new DefaultTableModel(row,
columnStudent);
this.StuInfoTable.setModel(StuInfoTableModel);
// 获得数据 try {
vecData = jdbcConnection.getData(strSQL);
} catch (SQLException e) {
e.printStackTrace();
}
// 依次为各行插入数据 if (vecData.size() > 0) {
//获得查询学生人数 nunselectedStuNum = vecData.size();
//组合学生人数 strselectedStuNum = String.valueOf(nunselectedStuNum) ;
for (int i = 0; i < vecData.size(); i++) {
StuInfoTableModel.addRow((String[]) vecData.get(i));
}
// 显示查询数据 this.stuMatchClassTabbedPane1.setSelectedComponent(this.dataPanel);
//显示查询到的学生人数 this.unselectedStuNumLabel1.setText(strselectedStuNum);
} else {
JOptionPane.showMessageDialog(this, "您查询的记录在数据库中不存在!请换一个查询条件.");
}
}
}
private void clearButtonActionPerformed(ActionEvent evt) {
//查询条件数量置为1
ncount = 1;
// 清空Vector变量vecSQL vecListView清空List控件中查询条件 this.vecSQL.clear();
this.vecListView.clear();
this.sqlViewList1.setListData(vecListView);
// 清除按钮不可用 this.clearButton.setEnabled(false);
}
private void addButtonActionPerformed(ActionEvent evt) {
//增加 String strSQL = "";
// 定义boolean型变量 isRight 用户操作是否正确,初始化false
boolean bisRight = false;
// 第一次必须选择”无“条件选项 if (!this.nullRadioButton.isSelected() && ncount == 1) {
bisRight = false;
JOptionPane.showMessageDialog(this, "选择多条件查询时’无’选项须在第一次操作时选择,\n第一次"
+ "操作后请选择’或者’、‘并且’选项!");
} else if (this.nullRadioButton.isSelected() && ncount > 1) {
// 第一次以后不能选择”无“条件选项 bisRight = false;
JOptionPane.showMessageDialog(this, "’无’选项须在第一次操作时选择,\n"
+ "第一次操作后请选择’或者’、‘并且’选项!!");
} else {
bisRight = true;
// 用户查询的条件数量加一 ncount++;
}
if (bisRight) {
// 获得用户选择,并转化为SQL语句 strSQL = this.getUserSelect();
System.out.println("\t" + strSQL);
// 向Vector变量vecSQL中添加变量 this.vecSQL.add(strSQL);
// 清空按钮可用,查询字段文本框置为空 this.clearButton.setEnabled(true);
this.userInputTextField.setText("");
this.dateInputTextField.setText("");
}
}
private void dateInputTextFieldMouseClicked(MouseEvent evt) {
//选择日期 CalendarJDialog calenderDialog = new CalendarJDialog(null);
calenderDialog.setModal(true);
calenderDialog.setVisible(true);
this.dateInputTextField.setText(calenderDialog.toString());
}
/**
* 向班级移动学生 * @param evt
*/
private void SelectButtonActionPerformed(ActionEvent evt) {
//向班级移动学生 //班级已选学生人数 String strselectedStuNum = "";
int nselectedStuNum = 0;
//班级人数 String strStuNum ="";
int nStuNum = 0;
//未选学生人数 String strUnselectedStuNum = "";
int nUnselectedStuNum = 0;
//班级号 String strClassId = "";
// 定义boolean型变量,是否成功增加学生 boolean bisRight = false;
//建立学生对象 Student student = new Student();
//获取班级已选学生人数、班级人数、未选学生人数,
strselectedStuNum = this.selectedStuNumLabel.getText();
strStuNum = this.stuNumViewLabel.getText();
strUnselectedStuNum = this.unselectedStuNumLabel1.getText();
strClassId = this.classIdViewLabel.getText();
//数值转换 try{
nselectedStuNum = Integer.parseInt(strselectedStuNum);
nStuNum = Integer.parseInt(strStuNum);
nUnselectedStuNum = Integer.parseInt(strUnselectedStuNum);
}catch(NumberFormatException e){
e.printStackTrace();
}
//已选学生人数加一 nselectedStuNum++;
//校验班级人数是否足额 if(nselectedStuNum > nStuNum){
bisRight = false;
JOptionPane.showMessageDialog(this, "班级人数已满,无法为该班级分配学生!");
}else{
bisRight = true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -