//////////////////////////////////////////////////提交删除
if (e.getSource() == toolBarButton[6] ||
e.getSource() == subMenu2[6]) {
if ((classidT.getText().trim()).equals("") ||
(nameT.getText().trim()).equals("") ||
(sexT.getText().trim()).equals("") ||
(ageT.getText().trim()).equals("") ||
(callT.getText().trim()).equals("") ||
(departmentT.getText().trim()).equals("")) {
JOptionPane.showMessageDialog(null, "请先点击删除键");
return;
} else {
name = nameT.getText();
String strSQL = "delete from stuinfo where name='" + name +
"'";
try {
st.executeUpdate(strSQL);
} catch (Exception exx) {
JOptionPane.showMessageDialog(null, "删除失败");
return;
}
JOptionPane.showMessageDialog(null, "删除成功");
classidT.setText("");
nameT.setText("");
sexT.setText("");
ageT.setText("");
callT.setText("");
departmentT.setText("");
}
}
//////////////////////////////////////////////集体查询(按班级)
if (e.getSource() == subMenu3[0]) {
classidT.setText("");
nameT.setText("");
sexT.setText("");
ageT.setText("");
callT.setText("");
departmentT.setText("");
if ((classidT.getText().trim()).equals("")) {
String idclass = JOptionPane.showInputDialog("请输入班级");
String strSQL = "select * from stuinfo where classid='" +
idclass +
"'";
try {
rs = st.executeQuery(strSQL);
displayres(rs);
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "查询失败");
}
} else {
String idclass = classidT.getText();
String strSQL = "select * from stuinfo where classid='" +
idclass +
"'";
try {
rs = st.executeQuery(strSQL);
displayres(rs);
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "查询失败");
}
}
}
////////////////////////////按单位
if (e.getSource() == subMenu3[1]) {
classidT.setText("");
nameT.setText("");
sexT.setText("");
ageT.setText("");
callT.setText("");
departmentT.setText("");
if ((departmentT.getText().trim()).equals("")) {
String idclass = JOptionPane.showInputDialog("请输入单位");
String strSQL = "select * from stuinfo where department='" +
idclass +
"'";
try {
rs = st.executeQuery(strSQL);
displayres(rs);
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "查询失败");
}
} else {
String idclass = departmentT.getText();
String strSQL = "select * from stuinfo where department='" +
idclass +
"'";
try {
rs = st.executeQuery(strSQL);
displayres(rs);
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "查询失败");
}
}
}
}
public void displayres(ResultSet rs1) throws SQLException {
//定位到达第一条记录
boolean moreRecords = rs1.next();
//如果没有记录,则提示一条消息
if (!moreRecords) {
JOptionPane.showMessageDialog(null, "无此记录");
return;
}
try {
//获取数据
getNextRow(rs1);
//刷新Table
c.validate();
}
catch (SQLException sqlex) {
sqlex.printStackTrace();
}
}
public void getNextRow(ResultSet rs2) throws
SQLException {
int count = 1;
while (rs2.next()) {
name = " 姓名:" + rs2.getString(1);
age = " 年龄:" + rs2.getString(2) + " 单位:";
department = rs2.getString(3) + "\n";
textT.append(count + "." + name + age + department);
count++;
}
}
}
查看本文来源