📄 presentquerydialog.java
字号:
}else{
JOptionPane.showMessageDialog(this, "您查询的班级出勤记录在数据库中不存在!请换一个查询条件。");
}
}
}
/**
* 获得出勤信息
* @throws SQLException
*/
private void initTable() throws SQLException {
// 建立一个JDBC对象
JdbcConnct jdbcConnection = new JdbcConnct();
// 定义Vector变量,存储从数据库查询来的信息
Vector vecData = new Vector();
// 定义SQL语句
String strSQL = "select * from present order by presentid ASC";
PresentQueryTableModel = new DefaultTableModel(row, combListStu);
this.presentTable.setModel(PresentQueryTableModel);
BorderLayout tableHeaderLayout = new BorderLayout();
presentTable.getTableHeader().setLayout(tableHeaderLayout);
presentTable.getTableHeader().setPreferredSize(
new java.awt.Dimension(481, 0));
// this.CourseQueryTable.setRowMargin(20);
// 获得数据
vecData = jdbcConnection.getData(strSQL);
// 依次为各行插入数据
for (int i = 0; i < vecData.size(); i++) {
PresentQueryTableModel.addRow((String[]) vecData.get(i));
}
}
/**
* 学生查询条件添加
*/
private void addButton1ActionPerformed(ActionEvent evt) {
// 定义SQL语句
String strSQL = "";
// 定义boolean型变量 isRight 用户操作是否正确,初始化false
boolean bisRight = false;
// 第一次必须选择”无“条件选项
if (!this.nullRadioButton1.isSelected() && ncount == 1) {
bisRight = false;
JOptionPane.showMessageDialog(this, "选择多条件查询时’无’选项须在第一次操作时选择,\n第一次"
+ "操作后请不要再选择’无’选项!");
} else if (this.nullRadioButton1.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.userInputTextField1.setText("");
}
}
/**
* 根据用户输入返回SQL语句
*
* @return
*/
private String getUserSelect() {
// 添加组合查询信息
// 定义boolean型变量 isRight 用户输入是否正确,初始化false
boolean bisRight = false;
// 定义String类型,查询字段,比较符,用户输入
String strQueryField = "";
String strLogicalSymbol = "";
String strUserInput = "";
// 用户在组合框中选择的Item索引
int nUserSelect;
// 定义SQL语句
String strSQL = "";
// 定义SQL显示语句
String strSQLView = "";
// 获得查询字段,比较符,用户输入
strQueryField = this.queryConditionComboBox.getSelectedItem()
.toString();
strLogicalSymbol = this.compareComboBox.getSelectedItem().toString();
strUserInput = this.userInputTextField1.getText();
// 用户选择的查询字段的索引
nUserSelect = this.queryConditionComboBox.getSelectedIndex();
// 用户选择的查询字段的索引加一
nUserSelect++;
// 打印用户选择的查询字段的索引
System.out.println("\t" + nUserSelect);
// 利用switch语句进行查询判断
switch (nUserSelect) {
case 1:
// 如果用户选择学号
// 校验用户数据
if (strUserInput.matches(REG_DIGIT)) {
bisRight = true;
strSQL = "StudentId" + " " + strLogicalSymbol + strUserInput;
// SQL显示
strSQLView = strQueryField + " " + strLogicalSymbol
+ strUserInput;
} else {
bisRight = false;
JOptionPane.showMessageDialog(this,
"请不要在学号中输入字母,班级号为7位数字,请重新输入学号!");
}
break;
case 2:
// 判断是否为数字型字段
// 如果查询字段为姓名
if (strLogicalSymbol.equals("匹配")) {
bisRight = true;
strSQL = "StuName" + " Like '" + strUserInput + "%'";
// SQL显示
strSQLView = strQueryField + "的前几个字符为:" + strUserInput;
} else {
bisRight = true;
strSQL = "StuName" + " " + strLogicalSymbol + "'"
+ strUserInput + "'";
// SQL显示
strSQLView = strQueryField + " " + strLogicalSymbol + " "
+ strUserInput;
}
break;
case 3:
// 如果用户选择"年龄"
// 校验用户数据
if (strUserInput.matches(REG_DIGITAGE)) {
bisRight = true;
strSQL = "AGE" + " " + strLogicalSymbol + strUserInput;
// SQL显示
strSQLView = strQueryField + " " + strLogicalSymbol
+ strUserInput;
} else {
bisRight = false;
JOptionPane.showMessageDialog(this,
"请不要在年龄中输入字母,年龄为2位数字,请重新输入!");
}
break;
case 4:
// 如果用户选择"班主任名"
// 校验用户数据
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;
default:
JOptionPane.showMessageDialog(this, "您选择的查询字段不存在,请重新选择!");
}
// 如果用户输入错误,查询学生条件减一
if (!bisRight) {
ncount--;
}
// 对用户选择进行操作 none and or
if (this.nullRadioButton1.isSelected()) {
} else if (this.andRadioButton1.isSelected()) {
// 添加按钮置为可用
this.addButton1.setEnabled(true);
strSQLView = "并且 " + strSQLView;
strSQL = "and " + strSQL;
} else {
// 添加按钮置为可用
this.addButton1.setEnabled(true);
strSQLView = "或者 " + strSQLView;
strSQL = "or " + strSQL;
}
// 显示SQL查询信息
this.vecListView.add(strSQLView);
// 向List控件中写入数据
this.studentQueryList1.setListData(vecListView);
return strSQL;
}
/**
* 学生查询
*/
private void queryConditionComboBoxActionPerformed(ActionEvent evt) {
// 根据用户选择匹配比较字段
// 根据用户输入字段更改匹配符
// 定义String型变量, strTemp,存储用户选择的字段
String strUserSelect = "";
// 获取用户选择字段
strUserSelect = this.queryConditionComboBox.getSelectedItem()
.toString();
if (strUserSelect.equals("姓名") || strUserSelect.equals("班主任名")) {
this.compareComboBox.removeAllItems();
this.compareComboBox.addItem("匹配");
this.compareComboBox.addItem("=");
}
if (strUserSelect.equals("学号") || strUserSelect.equals("年龄")) {
this.compareComboBox.removeAllItems();
this.compareComboBox.addItem(">");
this.compareComboBox.addItem("<");
this.compareComboBox.addItem("=");
}
}
/**
* 班级查询条件添加
*/
private void addButtonActionPerformed(ActionEvent evt) {
String strSQL = "";
// 定义boolean型变量 isRight 用户操作是否正确,初始化false
boolean bisRight = false;
// 第一次必须选择”无“条件选项
if (!this.nullRadioButton.isSelected() && ncountClass == 1) {
bisRight = false;
JOptionPane.showMessageDialog(this, "选择多条件查询时’无’选项须在第一次操作时选择,\n第一次"
+ "操作后请不要再选择’无’选项!");
} else if (this.nullRadioButton.isSelected() && ncountClass > 1) {
// 第一次以后不能选择”无“条件选项
bisRight = false;
JOptionPane.showMessageDialog(this, "’无’选项须在第一次操作时选择,\n"
+ "第一次操作后请不要再选择’无’选项!");
} else {
bisRight = true;
// 用户查询的条件数量加一
ncountClass++;
}
if (bisRight) {
// 获得用户选择,并转化为SQL语句
strSQL = this.getUserSelectClass();
System.out.println("\t" + strSQL);
// 向Vector变量vecSQLClass中添加变量
this.vecSQLClass.add(strSQL);
// 清空按钮可用,范围为本框不可用,查询字段文本框置为空
this.clearButton.setEnabled(true);
this.commentTextField.setText("");
}
}
//用户选择班级查询的条件
private String getUserSelectClass() {
// 添加组合查询信息
// 定义boolean型变量 isRight 用户输入是否正确,初始化false
boolean bisRight = false;
// 定义String类型,查询字段,比较符,用户输入
String strQueryField = "";
String strLogicalSymbol = "";
String strUserInput = "";
// 用户在组合框中选择的Item索引
int nUserSelect;
// 定义SQL语句
String strSQL = "";
// 定义SQL显示语句
String strSQLView = "";
// 获得查询字段,比较符,用户输入
strQueryField = this.ClassQueryComboBox.getSelectedItem().toString();
strLogicalSymbol = this.conditionComboBox.getSelectedItem().toString();
strUserInput = this.commentTextField.getText();
// 用户选择的查询字段的索引
nUserSelect = this.ClassQueryComboBox.getSelectedIndex();
// 用户选择的查询字段的索引加一
nUserSelect++;
// 打印用户选择的查询字段的索引
System.out.println("\t" + nUserSelect);
// 利用switch语句进行查询判断
switch (nUserSelect) {
case 1:
// 如果用户选择班级号
// 校验用户数据
if (strUserInput.matches(REG_DIGITCLASS)) {
bisRight = true;
strSQL = "ClassId" + " " + strLogicalSymbol + strUserInput;
// SQL显示
strSQLView = strQueryField + " " + strLogicalSymbol
+ strUserInput;
} else {
bisRight = false;
JOptionPane.showMessageDialog(this,
"请不要在班级号中输入字母,班级号为7位数字,请重新输入班级号!");
}
break;
case 2:
// 判断是否为数字型字段
// 如果查询字段为班级名
if (strLogicalSymbol.equals("匹配")) {
bisRight = true;
strSQL = "ClassName" + " Like '" + strUserInput + "%'";
// SQL显示
strSQLView = strQueryField + "的前几个字符为:" + strUserInput;
} else {
bisRight = true;
strSQL = "ClassName" + " " + strLogicalSymbol + "'"
+ strUserInput + "'";
// SQL显示
strSQLView = strQueryField + " " + strLogicalSymbol + " "
+ strUserInput;
}
break;
case 3:
// 如果用户选择"届别"
// 校验用户数据
if (strUserInput.matches(REG_DIGITFLAGYEAR)) {
bisRight = true;
strSQL = "flagYear" + " " + strLogicalSymbol + "'"
+ strUserInput + "' ";
// SQL显示
strSQLView = strQueryField + " " + strLogicalSymbol
+ strUserInput;
} else {
bisRight = false;
JOptionPane.showMessageDialog(this,
"请不要在届别中输入字母,届别为4位数字,请重新输入!");
}
break;
default:
JOptionPane.showMessageDialog(this, "您选择的查询字段不存在,请重新选择!");
}
// 如果用户输入不正确,班级查询条件数减一
if (!bisRight) {
ncountClass--;
}
// 对用户选择进行操作 none and or
if (this.nullRadioButton.isSelected()) {
} else if (this.addRadioButton.isSelected()) {
// 并且按钮置为可用
this.addButton.setEnabled(true);
strSQLView = "并且 " + strSQLView;
strSQL = "and " + strSQL;
} else {
// 或者按钮置为可用
this.orRadioButton.setEnabled(true);
strSQLView = "或者 " + strSQLView;
strSQL = "or " + strSQL;
}
// 显示SQL查询信息
this.vecListViewClass.add(strSQLView);
// 向List控件中写入数据
this.ClassQueryList.setListData(vecListViewClass);
return strSQL;
}
/**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -