?? stupsycomquerydialog.java
字號:
// 獲得數據 try {
vecData = jdbcConnection.getData(strSQL);
} catch (SQLException e) {
e.printStackTrace();
}
//依次為各行插入數據,顯示相應數據 if(vecData.size() > 0){
this.displayStuPsyCommentQueryInfo(vecData);
}else{
JOptionPane.showMessageDialog(this, "您查詢的成績在數據庫中不存在!請換一個查詢條件.");
}
}
/**
* 根據用戶選擇組合SQL語句 * @return
*/
private String getUserSelect(){
//根據用戶選擇組合SQL語句 //定義用戶選擇的周月組合框索引 int nuserSelect = 0;
//定義用戶選擇的周、月數索引 int nweekOrMon = 0;
//周\月評論查詢顯示變量strQueryView
String strQueryView = "";
//用戶輸入是否正確 bisRight,初始化為false
boolean bisRight = false;
//是否查詢全部月份bisWeekQuery,初始化為false
boolean bisWeekQuery = false;
//SQL語句 String strSQL = "select t.yearterm, t.weekmonnum, t.studentid,"
+ "t.stuname, t.studycomment, t.psychcomment,"
+ "t.teachername, t.weekormon from ws_stu_tea t ";
String strSQL1 = " where ";
String strSQL2 = "";
String strSQL3 = "";
String strSQLEnd = " order by t.studentid ASC";
//用戶選擇的學期 String stryearTerm = "";
//定義心理\學習框查詢類型 String strstuPsyType = "";
//定義姓名\學號查詢類型 String strQueryType = "";
//定義用戶輸入字段 String struserInput = "";
// 獲取用戶選擇的周月選擇組合框索引 nuserSelect = this.weekMonComboBox.getSelectedIndex();
// 獲取用戶選擇的周、月數索引 nweekOrMon = this.conditionComboBox.getSelectedIndex();
//組合框索引加一 nuserSelect ++;
//周、月數索引加一 nweekOrMon++;
//獲得用戶選擇的學期 stryearTerm = this.yearTermSelectComboBox.getSelectedItem().toString();
//獲得用戶輸入的學號\姓名查詢類型 strQueryType = this.queryConditionComboBox.getSelectedItem().toString();
//組合SQL語句 //學期查詢條件 if(stryearTerm.equals("所有學期")){
strSQL3 = " ";
}else{
strSQL3 = " and t.yearterm = '"+ stryearTerm +"'";
}
//為查詢條件顯示賦值 strQueryView = stryearTerm;
//獲得周數\月數 switch(nuserSelect){
case 1:
//如果用戶選擇查詢全部周 this.bisWeekQuery = true;
//添加查詢條件顯示 strQueryView += " "+"全部周";
break;
case 2:
//如果用戶選擇查詢全部月 this.bisWeekQuery = false;
//添加查詢條件顯示 strQueryView += " "+"全部月";
break;
case 3:
//如果用戶選擇查詢周數 strSQL2 = " and t.weekmonnum = "+nweekOrMon;
//查詢周評論 this.bisWeekQuery = true;
//添加查詢條件顯示 strQueryView += " "+"第"+nweekOrMon+"周";
break;
case 4:
//如果用戶選擇查詢月數 strSQL2 = " and t.weekmonnum = "+nweekOrMon;
//查詢月評論 this.bisWeekQuery = false;
//添加查詢條件顯示 strQueryView += " "+"第"+nweekOrMon+"月";
break;
default:
}
//獲得心理\學習查詢類型 將評論類型賦給全局變量nCommentType,顯示查詢條件 this.nCommentType =this.getstuPsyType(strQueryView);
//組合SQL語句 if (this.vecSQL.isEmpty()) {
strSQL += strSQL1+"t.StudentId > 1 ";
} else {
for (int i = 0; i < this.vecSQL.size(); i++) {
strSQL1 += this.vecSQL.get(i).toString();
}
strSQL += strSQL1;
}
//獲得學生查詢字段
strSQL +=strSQL2+strSQL3 + strSQLEnd;
return strSQL;
}
/**
* 清除列表框中的查詢條件 * @param evt
*/
private void ClearButtonActionPerformed(ActionEvent evt){
ncount = 1;
// 清空Vector變量vecSQL vecListView清空List控件中查詢條件 this.vecSQL.clear();
this.vecListView.clear();
this.studentQueryList1.setListData(vecListView);
// 清除按鈕不可用 this.ClearButton.setEnabled(false);
}
/**
* 為列表框增加查詢條件 * @param evt
*/
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.getStuQueryCondition();
System.out.println("\t" + strSQL);
// 向Vector變量vecSQL中添加變量 this.vecSQL.add(strSQL);
// 清空按鈕可用,范圍為本框不可用,查詢字段文本框置為空 this.ClearButton.setEnabled(true);
this.userInputTextField.setText("");
}
}
/**
* 獲得用戶選擇學習心理查詢的類型 * @return
*/
private int getstuPsyType(String strQueryView){
//定義心理\學習框查詢類型 String strstuPsyType = "";
//定義int型變量 nCommentType,1-全部評論、2-心理評論、3-學習評論 //初始化為1
int nCommentType = 1;
//獲得評論 strstuPsyType = this.stuPsyComboBox.getSelectedItem().toString();
//心理\學習查詢條件 if(strstuPsyType.equals("全部評論")){
//如果用戶選擇全部評論,評論類型值為1
nCommentType = 1;
}else if(strstuPsyType.equals("學習評論")){
//如果用戶選擇學習評論,評論類型值為2
nCommentType = 2;
}else if(strstuPsyType.equals("心理評論")){
//如果用戶選擇心理評論,評論類型值為3
nCommentType = 3;
}
//添加查詢條件顯示信息 strQueryView += " "+strstuPsyType;
//顯示查詢條件 this.queryViewLabel.setText(strQueryView);
return nCommentType;
}
/**
* 根據用戶選擇 獲取學生查詢條件 * @return
*/
private String getStuQueryCondition(){
// 定義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.userInputTextField.getText();
// 用戶選擇的查詢字段的索引 nUserSelect = this.queryConditionComboBox.getSelectedIndex();
// 用戶選擇的查詢字段的索引加一 nUserSelect++;
// 利用switch語句進行查詢判斷 switch (nUserSelect) {
case 1:
// 如果用戶選擇學號 // 校驗用戶數據 if (strUserInput.matches(REG_DIGIT)) {
bisRight = true;
strSQL = "t.StudentId" + " " + strLogicalSymbol + strUserInput;
// SQL顯示 strSQLView = strQueryField + " " + strLogicalSymbol
+ strUserInput;
} else {
bisRight = false;
JOptionPane.showMessageDialog(this,
"請不要在學號中輸入字母,班級號為7位數字,請重新輸入學號!");
}
break;
case 2:
// 判斷是否為數字型字段 // 如果查詢字段為姓名 if (strLogicalSymbol.equals("匹配")) {
bisRight = true;
strSQL = "t.StuName" + " Like '" + strUserInput + "%'";
// SQL顯示 strSQLView = strQueryField + "的前幾個字符為:" + strUserInput;
} else {
bisRight = true;
strSQL = "t.StuName" + " " + 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.addQueryButton.setEnabled(true);
strSQLView = "并且 " + strSQLView;
strSQL = "and " + strSQL;
} else {
// 添加按鈕置為可用 this.addQueryButton.setEnabled(true);
strSQLView = "或者 " + strSQLView;
strSQL = "or " + strSQL;
}
// 顯示SQL查詢信息 this.vecListView.add(strSQLView);
// 向List控件中寫入數據 this.studentQueryList1.setListData(vecListView);
return strSQL;
}
/**
* 根據用戶輸入選擇比較條件 * @param evt
*/
private void queryConditionComboBoxActionPerformed(ActionEvent evt) {
//用戶選擇組合框的Item索引 int nuserSelect = 0;
// 獲取用戶選擇字段 nuserSelect = this.queryConditionComboBox.getSelectedIndex();
//組合框索引加一 nuserSelect ++;
//根據用戶輸入選擇比較條件 if(nuserSelect == 1){
//如果用戶選擇學號 //清空比較選擇框 this.compareComboBox.removeAllItems();
//增加選項 this.compareComboBox.addItem(">");
this.compareComboBox.addItem("<");
this.compareComboBox.addItem("=");
}else if(nuserSelect == 2){
//如果用戶選擇姓名 //清空比較選擇框 this.compareComboBox.removeAllItems();
//增加選項 this.compareComboBox.addItem("匹配");
this.compareComboBox.addItem(" = ");
}
}
/**
* 顯示成績查詢信息 * @param vecData
*/
private void displayStuPsyCommentQueryInfo(Vector vecData){
//顯示成績查詢信息 //定義一維數組aryCommentInfo1存儲一條成績信息 String [] aryCommentInfo1 = new String[8];
//定義一維數組aryCommentInfo存儲一條學習心理評論信息 String [] aryCommentInfo2 = new String[7];
//定義Vector查詢數據臨時變量 vDataTemp
Vector vDataTemp = new Vector(200);
//定義查詢顯示變量 vDataView
Vector vDataView = new Vector(200);
//周月類型,初始化為false
boolean bisWeekType = false;
//評論類型,初始化為1
int ncommentType =1;
//獲得周月類型 bisWeekType = this.bisWeekQuery;
//獲得查詢類型,全部評論、心理評論、學習評論 ncommentType = this.nCommentType;
// 根據狀態信息表轉換成績信息 for (int i = 0; i < vecData.size(); i++) {
aryCommentInfo1 = (String[]) vecData.get(i);
switch (ncommentType) {
case 1:
// 如果查詢全部評論,不改動 break;
case 2:
// 如果查詢學習評論 aryCommentInfo1[5] = "";
break;
case 3:
// 如果查詢心理評論 aryCommentInfo1[6] = "";
break;
default:
}
// 時間類型,周評、月評 if (bisWeekType && aryCommentInfo1[7].equals("W")) {
// 重新為VecDataView賦值 vDataTemp.add(aryCommentInfo1);
} else if (!bisWeekType && aryCommentInfo1[7].equals("M")) {
vDataTemp.add(aryCommentInfo1);
}
}
for (int i = 0; i < vDataTemp.size(); i++) {
aryCommentInfo1 = (String[]) vDataTemp.get(i);
// 將周月分別顯示 if (aryCommentInfo1[7].equals("W")) {
aryCommentInfo1[1] = "第" + aryCommentInfo1[1] + "周";
} else if (aryCommentInfo1[7].equals("M")) {
aryCommentInfo1[1] = "第" + aryCommentInfo1[1] + "月";
}
// 為7個字段的顯示賦值 for (int j = 0; j < aryCommentInfo2.length; j++) {
aryCommentInfo2[j] = aryCommentInfo1[j];
}
// 為vecData賦值
vDataView.add(aryCommentInfo2);
}
//顯示成績查詢信息 // 刷新table中數據 DefaultTableModel stuPsyTableModel = new DefaultTableModel(row,
this.columnStuPsyQuery);
this.stuPsyTable.setModel(stuPsyTableModel);
for (int i = 0; i < vDataView.size(); i++) {
stuPsyTableModel.addRow((String[]) vDataView.get(i));
}
// 顯示查詢數據 this.StuPsyTabbedPane
.setSelectedComponent(this.dataViewPanel);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -