?? 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 + -