classmagdialog.java
来自「学生管理系统。使用java编程」· Java 代码 · 共 1,997 行 · 第 1/5 页
JAVA
1,997 行
this.EndTextField.setEnabled(false);
this.ClassQueryTextField.setText("");
}
}
private String getUserSelect() {
// 添加组合查询信息
// 定义boolean型变量 isSelectRage用户是否选择按范围查询,初始化false
boolean isSelectRage = false;
// 定义boolean型变量 isRight 用户输入是否正确,初始化false
boolean bisRight = false;
// 定义String类型,查询字段,比较符,用户输入
String strQueryField = "";
String strLogicalSymbol = "";
String strUserInput = "";
int nUserInput = 0;
// 用户在组合框中选择的Item索引
int nUserSelect;
// 定义用户选择范围,Min最小,Max最大
String strMin = "";
String strMax = "";
double dMin = 0.0;
double dMax = 0.0;
// 定义SQL语句
String strSQL = "";
// 定义SQL显示语句
String strSQLView = "";
// 获得查询字段,比较符,用户输入
strQueryField = this.QueryComboBox.getSelectedItem().toString();
strLogicalSymbol = this.CompareComboBox.getSelectedItem().toString();
strUserInput = this.ClassQueryTextField.getText();
// 用户选择的查询字段的索引
nUserSelect = this.QueryComboBox.getSelectedIndex();
// 获得最大值,最小值
strMin = this.FirstTextField.getText();
strMax = this.EndTextField.getText();
// 用户选择的查询字段索引加一
nUserSelect++;
// 打印用户选择的查询字段的索引
System.out.println("\t" + nUserSelect);
// 判断是否选择范围
if (this.SelectRageCheckBox1.isSelected()) {
isSelectRage = true;
// 利用switch语句进行查询判断
switch (nUserSelect) {
case 1:
// 如果用户选择"ClassId"
// 校验用户数据
if (strMin.matches(REG_DIGIT) && strMax.matches(REG_DIGIT)) {
bisRight = true;
} else {
bisRight = false;
JOptionPane.showMessageDialog(this,
"请不要在班级号中输入字母,班级号为7位数字,请重新输入班级号!");
}
// 跳出switch语句
break;
case 2:
// 判断是否为数字型字段
// 如果查询字段为班级名,提示错误"班级名不能按范围查询"
bisRight = false;
JOptionPane.showMessageDialog(this,
"班级名不能按范围查询,\n选择按范围查询时,请选择数值型的查询数据!");
break;
case 3:
// 如果用户选择"班主任名"
// 校验用户数据
bisRight = false;
JOptionPane.showMessageDialog(this,
"班级名不能按范围查询,\n选择按范围查询时,请选择数值型的查询数据!");
break;
case 4:
// 如果用户选择"学生人数"
// 校验用户数据
if (strMin.matches(REG_DIGIT) && strMax.matches(REG_DIGIT)) {
bisRight = true;
} else {
bisRight = false;
JOptionPane.showMessageDialog(this,
"请不要在学生人数中输入字母,学生人数在(0,100)内,请重新输入!");
}
break;
case 5:
// 如果用户选择"届别"
// 校验用户数据
if (strMin.matches(REG_DIGFlagYear)
&& strMax.matches(REG_DIGFlagYear)) {
bisRight = true;
} else {
bisRight = false;
JOptionPane.showMessageDialog(this,
"请不要在届别中输入字母,第一位不为空的四位年份,\n2008,1999等,请重新输入!");
}
break;
default:
JOptionPane.showMessageDialog(this, "您选择的查询字段不存在,请重新选择!");
}
} else {
// 用户没有选择按范围查询
isSelectRage = false;
// 组合where后SQL条件语句
// 用户选择字段为"科目名"
// 利用switch语句进行查询判断
switch (nUserSelect) {
case 1:
// 如果选择班级号、
// 校验用户数据
if (strUserInput.matches(REG_DIGIT)) {
bisRight = true;
strSQL = "ClassId" + " " + strLogicalSymbol + strUserInput;
// SQL显示
strSQLView = strQueryField + " " + strLogicalSymbol
+ strUserInput;
} else {
bisRight = false;
JOptionPane.showMessageDialog(this,
"请不要在班级号中输入字母,班级号为7位数字,请重新输入班级号!");
}
break;
case 2:
// 查询字段为班级名
// 校验班级名
if (strUserInput.length() > 100) {
bisRight = false;
JOptionPane.showMessageDialog(this, "班级名在50个字符以内,请重新输入!");
} else {
bisRight = true;
}
if (strLogicalSymbol.equals("匹配") && bisRight) {
bisRight = true;
strSQL = "ClassName" + " Like '" + strUserInput + "%'";
// SQL显示
strSQLView = strQueryField + "的前几个字符为:" + strUserInput;
} else if (bisRight) {
bisRight = true;
strSQL = "ClassName" + " " + strLogicalSymbol + "'"
+ strUserInput + "'";
// SQL显示
strSQLView = strQueryField + " " + strLogicalSymbol + " "
+ strUserInput;
}
break;
case 3:
// 如果用户选择"班主任名"
if (strLogicalSymbol.equals("匹配")) {
bisRight = true;
strSQL = "TeacherName" + " Like '" + strUserInput + "%'";
// SQL显示
strSQLView = strQueryField + "的前几个字符为:" + strUserInput;
} else {
bisRight = true;
strSQL = "TeacherName" + " " + strLogicalSymbol + "'"
+ strUserInput + "'";
// SQL显示
strSQLView = strQueryField + " " + strLogicalSymbol + " "
+ strUserInput;
}
break;
case 4:
// 如果用户选择"学生人数"
// 校验用户数据
if (strUserInput.matches(REG_DIGIT)) {
// 数值转换
try {
nUserInput = Integer.parseInt(strUserInput);
} catch (NumberFormatException e) {
e.printStackTrace();
}
if (nUserInput > 1000) {
bisRight = false;
JOptionPane.showMessageDialog(this,
"学生人数超出范围,学生人数在(0,100)内,请重新输入!");
} else {
strSQL = "StuNum" + " " + strLogicalSymbol
+ strUserInput;
// SQL显示
strSQLView = strQueryField + " " + strLogicalSymbol
+ strUserInput;
bisRight = true;
}
} else {
bisRight = false;
JOptionPane.showMessageDialog(this,
"请不要在学生人数中输入字母,学生人数在(0,100)内,请重新输入!");
}
break;
case 5:
// 如果用户选择"界别"
// 校验用户数据
if (strUserInput.matches(REG_DIGFlagYear)) {
strSQL = "FlagYear" + " " + strLogicalSymbol + strUserInput;
// SQL显示
strSQLView = strQueryField + " " + strLogicalSymbol
+ strUserInput;
bisRight = true;
} else {
bisRight = false;
JOptionPane.showMessageDialog(this,
"请不要在界别中输入字母,第一位不为空的四位年份,\n2008,1999等,请重新输入!");
}
break;
default:
JOptionPane.showMessageDialog(this, "您选择的查询字段不存在,请重新选择!");
}
}
// 用户选择”按范围查询“且初始校验成功(数字),进行第二轮校验,范围校验
if (!strQueryField.equals("班级名") && !strQueryField.equals("班主任名")
&& isSelectRage && bisRight) {
// 数值转换
try {
dMin = Double.parseDouble(strMin);
dMax = Double.parseDouble(strMax);
} catch (NumberFormatException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "数值转换错误!请确认输入值");
}
// 校验用户输入的信息 输入范围是否前小后大
if (dMin > dMax) {
bisRight = false;
JOptionPane.showMessageDialog(this, "选择按范围查询时,前一个值需小于后一个值");
} else
bisRight = true;
// 根据用户查询字段校验输入范围
if (bisRight) {
// 利用switch语句进行查询判断
switch (nUserSelect) {
case 1:
// 如果用户选择班级号
strSQL = "ClassId" + " between " + dMin + " and " + dMax;
strSQLView = strQueryField + " 大于 " + dMin + " 小于 " + dMax;
// 跳出break语句
break;
case 4:
// 如果用户选择按学生人数进行查询
if (dMin == 0 || dMax > 101) {
bisRight = false;
JOptionPane.showMessageDialog(this,
"您输入的学生人数超出范围。\n学生人数在(0,100]内,请重新输入!");
} else {
// 组合SQL语句
strSQL = "StuNum" + " between " + dMin + " and " + dMax;
// SQL显示
strSQLView = strQueryField + " 大于 " + dMin + " 小于 "
+ dMax;
bisRight = true;
}
break;
case 5:
// 如果用户选择界别进行查询
strSQL = "FlagYear" + " between " + dMin + " and " + dMax;
strSQLView = strQueryField + " 大于 " + dMin + " 小于 " + dMax;
break;
default:
JOptionPane.showMessageDialog(this, "您选择的查询字段不存在,请重新选择!");
}
}
} else if (!bisRight) {
// 若用户操作失败,则用户查询条件数量减一
ncount--;
}
// 对用户选择进行操作 none and or
if (this.PrecisionRadioButton.isSelected()) {
} else if (this.FaintRadioButton.isSelected()) {
// 添加按钮置为可用
this.AddButton.setEnabled(true);
strSQLView = "并且 " + strSQLView;
strSQL = "and " + strSQL;
} else {
// 添加按钮置为可用
this.AddButton.setEnabled(true);
strSQLView = "或者 " + strSQLView;
strSQL = "or " + strSQL;
}
//用户操作正确则显示SQL查询信息
if(bisRight){
this.vecListView.add(strSQLView);
// 向List控件中写入数据
this.QueryConditionList1.setListData(vecListView);
}
return strSQL;
}
private void ClassQueryTableMouseClicked(MouseEvent evt) {
// 用户在表格中点击鼠标,显示选中对象
//定义Class对象
Class newclass = new Class();
//获取鼠标点击数据
newclass = this.getMouseClickData(newclass);
// 显示鼠标点击数据
this.ClassIdTextField.setText(String.valueOf(newclass.getClassId()));
this.ClassNameTextField.setText(newclass.getClassName());
// 显示教师ID
this.TeacherIdTextField.setText(String.valueOf(newclass.getTeacherId()));
//显示专业号
this.majorIDTextField.setText(String.valueOf(newclass.getMajorId()));
this.StuNumTextField.setText(String.valueOf(newclass.getStuNum()));
this.FlagYearTextField.setText(String.valueOf(newclass.getFlagYear()));
// 新增、修改、删除按钮可用
this.NewButton.setEnabled(true);
this.ModifyButton.setEnabled(true);
this.DeleteButton.setEnabled(true);
// 保存按钮不可用
this.SaveButton.setEnabled(false);
// 学科号文本框不可用
this.ClassIdTextField.setEnabled(false);
}
/**获得用户输入的数据
*
*@param Class类对象
*
* @return Class对象
*/
public Class getUserInput(Class newclass) {
// 定义变量 班级号,班级名,班主任ID,专业号,学生人数,界别
String strclassID = "";
String strclassName = "";
String strteacherId = "";
String strmajorId = "";
String strstuNum = "";
String strFlagYear = "";
int nclassID = 0;
int nteacherId = 0;
int nmajorId = 0;
int nstuNum = 0;
int nFlagYear = 0;
// 定义boolean型变量,用户校验是否成功,初始化为False
boolean bisRight = false;
// 获取用户输入信息班级号,班级名,教师ID,专业号,学生人数,界别
strclassID = this.ClassIdTextField.getText();
strclassName = this.ClassNameTextField.getText();
strteacherId = this.TeacherIdTextField.getText();
strmajorId = this.majorIDTextField.getText();
strstuNum = this.StuNumTextField.getText();
strFlagYear = this.FlagYearTextField.getText();
// 用户信息校验
// 校验班级名
if (strclassName.length() > 100) {
bisRight = false;
JOptionPane.showMessageDialog(this, "班级名在50个字符以内,请重新输入!");
} else {
bisRight = true;
}
// 校验班主任ID
if (strteacherId.matches(REG_DIGITTEACH)) {
bisRight = true;
} else {
bisRight = false;
JOptionPane.showMessageDialog(this,
"请不要在班主任ID中输入字母,班主任ID为四位数字,请重新输入!");
}
// 校验专业号
if (strteacherId.matches(REG_DIGIT)) {
bisRight = true;
} else {
bisRight = false;
JOptionPane.showMessageDialog(this,
"请不要在专业号中输入字母,专业号最多为四位数字,请重新输入!");
}
// 校验学生人数
if (strstuNum.matches(REG_DIGIT)) {
bisRight = true;
} else {
bisRight = false;
JOptionPane.showMessageDialog(this,
"请不要在学生人数中输入字母,学生人数在(0,200)内,请重新输入!");
}
// 校验界别
if (strFlagYear.matches(REG_DIGFlagYear)) {
bisRight = true;
} else {
bisRight = false;
JOptionPane.showMessageDialog(this,
"请不要在届别中输入字母,第一位不为空的四位年份,\n2008,1999等,请重新输入!");
}
if (bisRight) {
// 类型转换
try {
nclassID = Integer.parseInt(strclassID);
nteacherId = Integer.parseInt(strteacherId);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?