📄 selectexamdialog.java
字号:
}
}
}
this.setSize(600, 475);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 退出考试查询
* @param evt
*/
private void exitButtonActionPerformed(ActionEvent evt) {
this.dispose();
}
/**
* 考试查询
* @param evt
*/
private void selectButtonActionPerformed(ActionEvent evt) {
// 建立一个JDBC对象
JdbcConnct jdbcConnection = new JdbcConnct();
// 定义Vector变量,存储从数据库查询来的信息
Vector vecData = new Vector();
// 定义SQL语句组合项
String strSQL = "select * from examQuery_v ";
String strSQL1 = " where ";
String strTemp = " order by CourseId ASC";
// 组合SQL语句
if (this.vecSQL.isEmpty()) {
strSQL += strTemp;
} else {
for (int i = 0; i < this.vecSQL.size(); i++) {
strSQL1 += this.vecSQL.get(i).toString();
}
strSQL += strSQL1 + strTemp;
}
// 刷新table中数据
DefaultTableModel ExamQueryTableModel = new DefaultTableModel(row,
columnExam);
this.examQueryTable.setModel(ExamQueryTableModel);
// 获得数据
try {
vecData = jdbcConnection.getData(strSQL);
} catch (SQLException e) {
e.printStackTrace();
}
// 依次为各行插入数据
if (vecData.size() > 0) {
for (int i = 0; i < vecData.size(); i++) {
ExamQueryTableModel.addRow((String[]) vecData.get(i));
}
// 显示查询数据
this.examQueryTabbedPane.setSelectedComponent(this.dataViewPanel);
} else {
JOptionPane.showMessageDialog(this, "您查询的考试记录" +
"在数据库中不存在!请换一个查询条件.");
}
}
/**
* 清除查询条件
* @param evt
*/
private void clearExamQueryButtonActionPerformed(ActionEvent evt){
ncountExam = 1;
// 清空Vector变量vecSQL vecListView清空List控件中查询条件
this.vecSQL.clear();
this.vecListView.clear();
this.studentQueryList1.setListData(vecListView);
// 清除按钮不可用
this.clearExamQueryButton.setEnabled(false);
}
/**增加查询条件
*
* @param evt
*/
private void addExamQueryButtonActionPerformed(ActionEvent evt){
// 定义SQL语句
String strSQL = "";
// 定义boolean型变量 isRight 用户操作是否正确,初始化false
boolean bisRight = false;
// 第一次必须选择”无“条件选项
if (!this.nullRadioButton1.isSelected() && ncountExam == 1) {
bisRight = false;
JOptionPane.showMessageDialog(this, "选择多条件查询时’无’选项须在第一次操作时选择,\n第一次"
+ "操作后请选择‘或者’、‘并且’选项!");
} else if (this.nullRadioButton1.isSelected() && ncountExam > 1) {
// 第一次以后不能选择”无“条件选项
bisRight = false;
JOptionPane.showMessageDialog(this, "’无’选项须在第一次操作时选择,\n"
+ "第一次操作后请选择‘或者’、‘并且’选项!");
} else {
bisRight = true;
// 用户查询的条件数量加一
ncountExam++;
}
if (bisRight) {
// 获得用户选择,并转化为SQL语句
strSQL = this.getUserSelect();
System.out.println("\t" + strSQL);
// 向Vector变量vecSQL中添加变量
this.vecSQL.add(strSQL);
// 清空按钮可用,范围为本框不可用,查询字段文本框置为空
this.clearExamQueryButton.setEnabled(true);
this.userInputTextField.setText("");
}
}
/**
* 根据用户的查询条件匹配查询符号
* @param evt
*/
private void examQueryTypeComboBoxActionPerformed(ActionEvent evt) {
String strUserSelectType = "";
// 获取用户选择字段
strUserSelectType = this.examQueryTypeComboBox.getSelectedItem()
.toString();
if (strUserSelectType.equals("科目名")) {
this.logicSymbloComboBox.removeAllItems();
this.logicSymbloComboBox.addItem("匹配");
this.logicSymbloComboBox.addItem("=");
}
if (strUserSelectType.equals("科目号")) {
this.logicSymbloComboBox.removeAllItems();
this.logicSymbloComboBox.addItem(">");
this.logicSymbloComboBox.addItem("<");
this.logicSymbloComboBox.addItem("=");
}
}
/**
* 根据用户选择组合SQL语句
* @return strSQL SQL语句
*/
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.examQueryTypeComboBox.getSelectedItem()
.toString();
strLogicalSymbol = this.logicSymbloComboBox.getSelectedItem().toString();
strUserInput = this.userInputTextField.getText();
// 用户选择的查询字段的索引
nUserSelect = this.examQueryTypeComboBox.getSelectedIndex();
// 用户选择的查询字段的索引加一
nUserSelect++;
// 利用switch语句进行查询判断
switch (nUserSelect) {
case 1:
// 如果用户选择科目号
// 校验用户数据
if (strUserInput.matches(REG_DIGITCOURSE)) {
bisRight = true;
strSQL = "CourseId" + " " + strLogicalSymbol + strUserInput;
// SQL显示
strSQLView = strQueryField + " " + strLogicalSymbol
+ strUserInput;
} else {
bisRight = false;
JOptionPane.showMessageDialog(this,
"请不要在科目号中输入字母,科目号为7位数字,请重新输入!");
}
break;
case 2:
// 判断是否为数字型字段
// 如果查询字段为科目名
if (strLogicalSymbol.equals("匹配")) {
bisRight = true;
strSQL = "CourseName" + " Like '" + strUserInput + "%'";
// SQL显示
strSQLView = strQueryField + "的前几个字符为:" + strUserInput;
} else {
bisRight = true;
strSQL = "CourseName" + " " + strLogicalSymbol + "'"
+ strUserInput + "'";
// SQL显示
strSQLView = strQueryField + " " + strLogicalSymbol + " "
+ strUserInput;
}
break;
default:
JOptionPane.showMessageDialog(this, "您选择的查询字段不存在,请重新选择!");
}
// 如果用户输入错误,查询学生条件减一
if (!bisRight) {
ncountExam--;
} else {
// 如果用户输入正确,则返回SQL语句
// 对用户选择进行操作 none and or
if (this.nullRadioButton1.isSelected()) {
} else if (this.andRadioButton1.isSelected()) {
// 添加按钮置为可用
this.addExamQueryButton.setEnabled(true);
strSQLView = "并且 " + strSQLView;
strSQL = "and " + strSQL;
} else {
// 添加按钮置为可用
this.addExamQueryButton.setEnabled(true);
strSQLView = "或者 " + strSQLView;
strSQL = "or " + strSQL;
}
// 显示SQL查询信息
this.vecListView.add(strSQLView);
// 向List控件中写入数据
this.studentQueryList1.setListData(vecListView);
}
return strSQL;
}
/**
* 鼠标双击事件
* @param evt
*/
private void examQueryTableMouseClicked(ActionEvent evt){
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -