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