1 packagecom.test1;2 /*
3 * 主函数4 * 1.对界面的表格、按钮、标签定义5 * 2.对按钮的监听6 * 3.通过StuModel链接数据库和增删改查数据库表格7 * 4.删除学生信息8 *9 **/
10 import java.awt.*;11 import java.awt.event.*;12 import java.util.*;13 import java.sql.*;14 import javax.swing.*;15 public class StudentManageSys2 extends JFrame implementsActionListener{16 //JFrame jf;17 //Container container;
18 JPanel jp1,jp2,jp3;19 JLabel label;20 JTextField name;21 JButton chaxun,shuaxin,jb1,jb2,jb3;//包括查询按钮和刷新按钮
22 JTable jt;//表格
23 JScrollPane jsp;//带滚动条
24
25
26 Connection conn;//为删除按键提供变量
27 PreparedStatement ps;28 public static voidmain(String[] args) {29 //TODO Auto-generated method stub
30 StudentManageSys2 sms=newStudentManageSys2();31 }32 publicStudentManageSys2(){33 //jf=new JFrame();34 //container=jf.getContentPane();
35
36
37 jp1=newJPanel();38 jp2=newJPanel();39 jp3=newJPanel();40 //设置上面的用户名,查询栏
41 label=newJLabel();42 label.setText("用户名");43 //label.setLayout(new FlowLayout());
44 name=new JTextField(15);45 chaxun=newJButton();46 chaxun.setText("查询");47 chaxun.addActionListener(this);48 //chaxun.setActionCommand("chaxun");
49 shuaxin=new JButton("刷新");50 shuaxin.addActionListener(this);51 jp1.add(label);52 jp1.add(name);53 jp1.add(chaxun);54 jp1.add(shuaxin);55 this.add(jp1,BorderLayout.NORTH);56
57
58 jt=newJTable();59 jsp=newJScrollPane();60
61
62
63 StuModel sm=newStuModel();64 jt=newJTable(sm);65 jsp=newJScrollPane(jt);66 jp2.add(jsp);67 this.add(jp2,BorderLayout.CENTER);68
69 //设置最下面一行
70 jb1=new JButton("添加");71 jb1.addActionListener(this);72 jb2=new JButton("修改");73 jb2.addActionListener(this);74 jb3=new JButton("删除");75 jb3.addActionListener(this);76 jp3.add(jb1);77 jp3.add(jb2);78 jp3.add(jb3);79 this.add(jp3,BorderLayout.SOUTH);80
81
82
83 this.setVisible(true);84 this.setSize(500,300);85 this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);86 this.setTitle("学生管理系统");87
88 }89 @Override90 public voidactionPerformed(ActionEvent e) {91 //TODO Auto-generated method stub
92 if(e.getSource()==chaxun){93
94 String lookname;95 lookname=this.name.getText().trim();//得到用户输入的内容
96 String sql="select * from stu where stuName='"+lookname+"'";//97 StuModel sm=newStuModel(sql);98 jt.setModel(sm);99 System.out.println("用户想查询"+lookname+"信息");100
101
102
103 }104 else if(e.getSource()==shuaxin){105 StuModel sm=newStuModel();106 jt.setModel(sm);107 }108 else if (e.getSource()==jb1){109 new addStuDialog(this,"添加学生信息",true);110
111
112 }else if(e.getSource()==jb2){113 StuModel sm=newStuModel();114
115 System. out .println("aaaa");116 int rownum =this.jt.getSelectedRow();117 if(rownum==-1) { //提示
118 JOptionPane. showMessageDialog (this, "请选择一行");119 return;//代表不要再往下面走了,谁调用就返回给谁 }120 //显示修改对话框
121 }122 new StudentUpdateDialog(this,"修改对话框",true,sm,rownum);123
124 }else if(e.getSource()==jb3){125
126
127
128 int rownum=this.jt.getSelectedRow();129 if(rownum==-1){130 JOptionPane.showMessageDialog(this, "请选中需要删除的行");131 return;132 }133 StuModel sm=newStuModel();134 String stuId=(String) sm.getValueAt(rownum,0);135 String sql="delete from stu where stuId='"+stuId+"'";136 try{137 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");138 conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=HIRO","sa","1989");139 ps=conn.prepareStatement(sql);140 int i=ps.executeUpdate();141 if(i==1){142 JOptionPane.showMessageDialog(null, "删除成功!");143
144 }else{145 JOptionPane.showMessageDialog(null, "删除失败!");146 }147
148 } catch(Exception e1) {149 //TODO Auto-generated catch block
150 e1.printStackTrace();151 }finally{152 if(ps!=null){153 try{154 ps.close();155 } catch(SQLException e1) {156 //TODO Auto-generated catch block
157 e1.printStackTrace();158 }159 }160 if(conn!=null){161 try{162 conn.close();163 } catch(SQLException e1) {164 //TODO Auto-generated catch block
165 e1.printStackTrace();166 }167 }168 }169
170
171
172 }173 }174 }