//JDBC
public void conDB() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException e) {
JOptionPane.showMessageDialog(null, "数据库错误");
}
try {
con = DriverManager.getConnection("jdbc:odbc:jettang", "sa", "");
st = con.createStatement();
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "数据库连接失败");
}
}
//关闭连接
public void closeDB() {
try {
st.close();
con.close();
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "数据库关闭失败");
}
}
//事件响应
public void actionPerformed(ActionEvent e) {
//////////////////////////////////////////////
if (e.getSource() == toolBarButton[7]) {
JOptionPane.showMessageDialog(null, "当前版本1.0,操作帮助请看使用说明书");
}
/////////////////////////////////////////////查询(按姓名)
if (e.getSource() == subMenu2[0] || e.getSource() == toolBarButton[0] ||
e.getSource() == subMenu1[2]) {
String idid = JOptionPane.showInputDialog("请输入学生姓名");
if (idid.trim() != "") {
String strSQL = "select * from stuinfo where name ='" + idid +
"'";
try {
rs = st.executeQuery(strSQL);
int count = 0;
while (rs.next()) {
classid = rs.getString("classid");
name = rs.getString("name");
department = rs.getString("department");
sex = rs.getString("sex");
age = rs.getString("age");
call = rs.getString("call");
++count;
}
if (count == 0) {
JOptionPane.showMessageDialog(null, "无此信息");
} else {
classidT.setText(classid);
nameT.setText(name);
sexT.setText(sex);
ageT.setText(age);
callT.setText(call);
departmentT.setText(department);
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "查询失败");
}
}
}
/////////////////////////////////////////////关闭
if (e.getSource() == subMenu1[4]) {
frame.dispose();
closeDB();
}
/////////////////////////////////////////////添加
if (e.getSource() == subMenu1[0] || e.getSource() == subMenu2[1] ||
e.getSource() == toolBarButton[1]) {
JOptionPane.showMessageDialog(null, "请输入你添加的信息再点击提交添加键");
classidT.setEnabled(true);
classidT.setText("");
nameT.setText("");
sexT.setText("");
ageT.setText("");
callT.setText("");
departmentT.setText("");
}
/////////////////////////////////////////////提交添加
if (e.getSource() == toolBarButton[5] || e.getSource() == subMenu2[5]) {
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, "请先点击添加键");
} else {
classid = classidT.getText();
name = nameT.getText();
sex = sexT.getText();
age = ageT.getText();
call = callT.getText();
department = departmentT.getText();
String strSQL =
"insert into stuinfo(classid,name,sex,age,call,department) values('" +
classid + "','" +
name + "','" + sex + "','" + age + "','" + call + "','" +
department + "')";
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() == subMenu2[4] || e.getSource() == toolBarButton[4]) {
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 {
classid = classidT.getText();
name = nameT.getText();
sex = sexT.getText();
age = ageT.getText();
call = callT.getText();
department = departmentT.getText();
String strSQL = "update stuinfo set classid='" + classid +
"',sex='" + sex + "',age='" + age + "',call='" +
call + "',department='" + department + "'" +
"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() == subMenu2[3] || e.getSource() == toolBarButton[3]) {
String idid = JOptionPane.showInputDialog
("请输入你要修改的学生姓名后点击提交修改键确认");
if (idid.trim() != "") {
String strSQL = "select * from stuinfo where name ='" + idid +
"'";
try {
rs = st.executeQuery(strSQL);
int count = 0;
while (rs.next()) {
classid = rs.getString("classid");
name = rs.getString("name");
department = rs.getString("department");
sex = rs.getString("sex");
age = rs.getString("age");
call = rs.getString("call");
++count;
}
if (count == 0) {
JOptionPane.showMessageDialog(null, "无此信息");
} else {
classidT.setText(classid);
nameT.setText(name);
sexT.setText(sex);
ageT.setText(age);
callT.setText(call);
departmentT.setText(department);
classidT.setEnabled(false);
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "修改失败");
}
}
}
/////////////////////////////////////////////////删除
if (e.getSource() == subMenu2[2] || e.getSource() == toolBarButton[2] ||
e.getSource() == subMenu1[1]) {
String idDel = JOptionPane.showInputDialog
("请输入要删除的学生姓名后点击提交删除键确认");
if (idDel.trim() != "") {
String strSQL = "select * from stuinfo where name ='" + idDel +
"'";
try {
rs = st.executeQuery(strSQL);
int count = 0;
while (rs.next()) {
classid = rs.getString("classid");
name = rs.getString("name");
department = rs.getString("department");
sex = rs.getString("sex");
age = rs.getString("age");
call = rs.getString("call");
++count;
}
if (count == 0) {
JOptionPane.showMessageDialog(null, "无此信息");
} else {
classidT.setText(classid);
nameT.setText(name);
sexT.setText(sex);
ageT.setText(age);
callT.setText(call);
departmentT.setText(department);
classidT.setEnabled(false);
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "删除失败");
}
}
} | |