根据韩顺平老师视频整理:
效果图:
1.主函数
1 package com.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 implements ActionListener{ 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 void main(String[] args) { 29 // TODO Auto-generated method stub 30 StudentManageSys2 sms=new StudentManageSys2(); 31 } 32 public StudentManageSys2(){ 33 // jf=new JFrame(); 34 // container=jf.getContentPane(); 35 36 37 jp1=new JPanel(); 38 jp2=new JPanel(); 39 jp3=new JPanel(); 40 //设置上面的用户名,查询栏 41 label=new JLabel(); 42 label.setText("用户名"); 43 //label.setLayout(new FlowLayout()); 44 name=new JTextField(15); 45 chaxun=new JButton(); 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=new JTable(); 59 jsp=new JScrollPane(); 60 61 62 63 StuModel sm=new StuModel(); 64 jt=new JTable(sm); 65 jsp=new JScrollPane(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 @Override 90 public void actionPerformed(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=new StuModel(sql); 98 jt.setModel(sm); 99 System.out.println("用户想查询"+lookname+"信息"); 100 101 102 103 } 104 else if(e.getSource()==shuaxin){ 105 StuModel sm=new StuModel(); 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=new StuModel(); 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=new StuModel(); 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 }
2.数据模型
1 package com.test1; 2 /* 3 * 链接数据库语句和修改数据库表格命令,放回表格数据 4 * 5 * */ 6 import java.sql.Connection; 7 import java.sql.DriverManager; 8 import java.sql.PreparedStatement; 9 import java.sql.ResultSet; 10 import java.util.Vector; 11 import javax.swing.table.AbstractTableModel; 12 public class StuModel extends AbstractTableModel { 13 Vector rowData,columnName; 14 15 Connection ct=null; 16 PreparedStatement ps=null; 17 ResultSet rs=null; 18 public void getInit(String sql){ 19 if(sql.equals("")){ 20 sql="select * from stu"; 21 } 22 try { 23 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 24 String url="jdbc:sqlserver://localhost:1433;databaseName=HIRO"; 25 ct=DriverManager.getConnection(url,"sa","1989"); 26 ps=ct.prepareStatement(sql); 27 rs=ps.executeQuery(); 28 29 columnName=new Vector(); 30 columnName.add("学号"); 31 columnName.add("名字"); 32 columnName.add("性别"); 33 columnName.add("年龄"); 34 columnName.add("籍贯"); 35 columnName.add("系别"); 36 37 rowData=new Vector(); 38 while(rs.next()){ 39 Vector hang=new Vector();//hang要放在循环里面,不然全部都是第一行的数据 40 hang.add(rs.getString(1)); 41 hang.add(rs.getString(2)); 42 hang.add(rs.getString(3)); 43 hang.add(rs.getInt(4)); 44 hang.add(rs.getString(5)); 45 hang.add(rs.getString(6)); 46 rowData.add(hang); 47 } 48 } catch (Exception e) { 49 // TODO Auto-generated catch block 50 e.printStackTrace(); 51 }finally{ 52 try{ 53 if(rs!=null){ 54 rs.close(); 55 }if(ps!=null){ 56 ps.close(); 57 }if(ct!=null){ 58 ct.close(); 59 } 60 61 }catch(Exception e){ 62 e.printStackTrace(); 63 } 64 } 65 } 66 67 public StuModel(String sql){ 68 this.getInit(sql); 69 } 70 71 72 public StuModel(){ 73 this.getInit(""); 74 } 75 76 @Override//得到列数 77 public int getColumnCount() { 78 // TODO Auto-generated method stub 79 return this.columnName.size(); 80 } 81 82 @Override//重写getColumnName得到列名 83 public String getColumnName(int column) { 84 // TODO Auto-generated method stub 85 return (String)this.columnName.get(column); 86 } 87 88 @Override//得到行数 89 public int getRowCount() { 90 // TODO Auto-generated method stub 91 return this.rowData.size(); 92 } 93 94 @Override//得到某行某列的数据 95 public Object getValueAt(int arg0, int arg1) { 96 // TODO Auto-generated method stub 97 return ((Vector)this.rowData.get(arg0)).get(arg1); 98 } 99 100 }
3.添加学生信息
1 package com.test1; 2 /** 3 * 点击添加按钮,添加学生个人信息 4 * */ 5 import java.awt.*; 6 import java.awt.event.ActionEvent; 7 import java.awt.event.ActionListener; 8 9 import javax.swing.*; 10 import java.sql.*; 11 import javax.swing.event.*; 12 public class addStuDialog extends JDialog implements ActionListener{ 13 14 JLabel jl1,jl2,jl3,jl4,jl5,jl6; 15 JTextField jtf1,jtf2,jtf3,jtf4,jtf5,jtf6; 16 JButton jb1,jb2; 17 JPanel jp1,jp2,jp3; 18 // public static void main(String[] args) { 19 // // TODO Auto-generated method stub 20 // 21 // } 22 public addStuDialog(Frame owner,String title,boolean modal){ 23 super(owner,title,modal); 24 25 26 27 jl1=new JLabel(" 学号"); 28 jl2=new JLabel(" 名字"); 29 jl3=new JLabel(" 性别"); 30 jl4=new JLabel(" 年龄"); 31 jl5=new JLabel(" 籍贯"); 32 jl6=new JLabel(" 系别"); 33 34 jtf1=new JTextField(30); 35 jtf2=new JTextField(30); 36 jtf3=new JTextField(30); 37 jtf4=new JTextField(30); 38 jtf5=new JTextField(30); 39 jtf6=new JTextField(30); 40 41 jp1=new JPanel(); 42 jp2=new JPanel(); 43 jp3=new JPanel(); 44 jp1.setLayout(new GridLayout(6,1,5,5)); 45 jp2.setLayout(new GridLayout(6,1,5,5)); 46 jp1.add(jl1); 47 jp1.add(jl2); 48 jp1.add(jl3); 49 jp1.add(jl4); 50 jp1.add(jl5); 51 jp1.add(jl6); 52 53 jp2.add(jtf1); 54 jp2.add(jtf2); 55 jp2.add(jtf3); 56 jp2.add(jtf4); 57 jp2.add(jtf5); 58 jp2.add(jtf6); 59 60 jb1=new JButton("确定"); 61 jb1.addActionListener(this); 62 63 jb2=new JButton("取消"); 64 jb2.addActionListener(this); 65 jp3.add(jb1); 66 jp3.add(jb2); 67 68 this.add(jp1,BorderLayout.WEST); 69 this.add(jp2,BorderLayout.EAST); 70 this.add(jp3,BorderLayout.SOUTH); 71 72 this.setSize(400,300); 73 this.setLocation(200, 150); 74 this.setVisible(true); 75 this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); 76 77 } 78 @Override 79 public void actionPerformed(ActionEvent arg0) { 80 // TODO Auto-generated method stub 81 if(arg0.getSource()==jb1){ 82 83 Connection conn=null; 84 PreparedStatement ps=null; 85 try{ 86 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 87 String url="jdbc:sqlserver://localhost:1433;databaseName=HIRO"; 88 conn=DriverManager.getConnection(url,"sa","1989"); 89 ps=conn.prepareStatement("insert into stu values (?,?,?,?,?,?)"); 90 ps.setString(1, this.jtf1.getText().trim()); 91 ps.setString(2, this.jtf2.getText().trim()); 92 93 ps.setString(3, this.jtf3.getText().trim()); 94 ps.setInt(4, Integer.parseInt(this.jtf4.getText().trim())); 95 ps.setString(5, this.jtf5.getText().trim()); 96 ps.setString(6, this.jtf6.getText().trim()); 97 if(this.jtf1.getText().trim()!=null&&this.jtf2.getText().trim()!=null){ 98 int i=ps.executeUpdate(); 99 JOptionPane.showMessageDialog(null, "添加"+this.jtf2.getText().trim()+"成员成功"); 100 101 }else{ 102 JOptionPane.showMessageDialog(null, "添加"+this.jtf2.getText().trim()+"成员失败"); 103 return; 104 } 105 106 }catch(Exception e){ 107 e.printStackTrace(); 108 }finally{ 109 if(ps!=null){ 110 try { 111 ps.close(); 112 } catch (Exception e) { 113 // TODO Auto-generated catch block 114 e.printStackTrace(); 115 } 116 } 117 if(conn!=null){ 118 try { 119 conn.close(); 120 } catch (SQLException e) { 121 // TODO Auto-generated catch block 122 e.printStackTrace(); 123 } 124 } 125 } 126 this.dispose(); 127 } 128 if(arg0.getSource()==jb2){ 129 this.dispose(); 130 } 131 } 132 }
4.修改学生信息,设置学号和名字不能修改
1 package com.test1; 2 /* 3 * 修改已经存在的学生信息 4 * */ 5 import javax.swing.*; 6 import java.awt.*; 7 import java.awt.event.ActionEvent; 8 import java.awt.event.ActionListener; 9 10 import javax.swing.event.*; 11 import java.sql.*; 12 public class StudentUpdateDialog extends JDialog implements ActionListener{ 13 //定义我需要的swing组件 14 JLabel jl1, jl2,jl3, jl4, jl5 ,jl6; 15 JButton jb1,jb2; 16 JTextField jtf1,jtf2,jtf3,jtf4,jtf5,jtf6; 17 JPanel jp1,jp2,jp3; 18 19 //构造函数 Frame 代表父窗口口,title 代表窗口的名字,model指定是模式窗口,还是非模式的窗口 20 public StudentUpdateDialog(Frame owner,String title,boolean model,StuModel sm,int rownum) { 21 super(owner,title, model); //调用父类构造 方法,达到模式对话框效果 22 jl1=new JLabel(" 学号"); 23 jl2=new JLabel(" 姓名"); 24 jl3=new JLabel(" 性别"); 25 jl4=new JLabel(" 年龄"); 26 jl5=new JLabel(" 籍贯"); 27 jl6=new JLabel(" 系别"); 28 29 jtf1=new JTextField(30); 30 jtf2=new JTextField(30); 31 jtf3=new JTextField(30); 32 jtf4=new JTextField(30); 33 jtf5=new JTextField(30); 34 jtf6=new JTextField(30); 35 //初始化数据 36 37 jtf1.setText((String)sm.getValueAt(rownum, 0)); //setEditable设置指定的 boolean 变量,以指 示此 文本控件 是否应该为可编辑的 38 jtf1.setEditable(false); 39 jtf2.setText((String)sm.getValueAt(rownum, 1)); 40 jtf2.setEditable(false); 41 jtf3.setText((String)sm.getValueAt(rownum, 2)); 42 jtf4.setText(sm.getValueAt(rownum, 3)+""); 43 jtf5.setText((String)sm.getValueAt(rownum, 4)); 44 jtf6.setText((String)sm.getValueAt(rownum, 5)); 45 46 jb1=new JButton ("修改"); 47 jb2=new JButton ("取消"); 48 jp1=new JPanel(); 49 jp2=new JPanel(); 50 jp3=new JPanel(); 51 //设置布局 52 jp1.setLayout(new GridLayout(6,1,5,5)); 53 jp2.setLayout(new GridLayout(6,1,5,5)); 54 //添加组件 55 jp1.add(jl1); 56 jp1.add(jl2); 57 jp1.add(jl3); 58 jp1.add(jl4); 59 jp1.add(jl5); 60 jp1.add(jl6); 61 62 jp2.add(jtf1); 63 jp2.add(jtf2); 64 jp2.add(jtf3); 65 jp2.add(jtf4); 66 jp2.add(jtf5); 67 jp2.add(jtf6); 68 69 jp3.add(jb1); 70 jp3.add(jb2); 71 this.add(jp1,BorderLayout.WEST); 72 this.add(jp2,BorderLayout.CENTER); 73 this.add(jp3,BorderLayout.SOUTH); 74 //注册监听 75 jb1.addActionListener(this); 76 jb2.addActionListener(this); //展现 77 this.setSize(400,300); // 78 this.setLocation(200,150); 79 this.setVisible(true); 80 } 81 82 83 @Override 84 public void actionPerformed(ActionEvent e) { 85 // TODO Auto-generated method stub 86 if(e.getSource()==jb1) { //对用户点击添加按钮后的响应动作 //连接数据库 87 Connection ct =null; 88 PreparedStatement ps =null; 89 try { 90 //加载驱动 91 // Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 92 // String url="jdbc:sqlserver://localhost:1433;databaseName=HIRO"; 93 94 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 95 String url="jdbc:sqlserver://localhost:1433;databaseName=HIRO"; 96 ct=DriverManager.getConnection(url,"sa","1989"); 97 98 //预编译的都是通过添加参数的方式来赋值 99 System.out.println("已连接数据库"); 100 ps=ct.prepareStatement("update stu set stuName=?,stuSex=?,stuAge=?,stuJg=?,stuDept =? where stuId=?"); 101 ps.setString(1, this.jtf2.getText()); 102 ps.setString(2, this.jtf3.getText()); 103 ps.setString(3, this.jtf4.getText()); // 104 ps.setInt(3,Integer.parseInt(this.jtf4.getText())); 105 ps.setString(4, this.jtf5.getText()); 106 ps.setString(5, this.jtf6.getText()); 107 ps.setString(6, this.jtf1.getText()); 108 109 int i=ps.executeUpdate(); 110 if(i==1) { 111 System.out.print("修改成功ok"); 112 } else { 113 System.out.print("修改失败"); } 114 } catch (Exception e1) { 115 // TODO Auto-generated catch block 116 e1.printStackTrace(); 117 } finally { 118 try { ps.close(); ct.close(); 119 } catch (SQLException e1) { 120 // TODO Auto-generated catch block 121 e1.printStackTrace(); 122 } 123 } 124 //关闭对话框,关闭添加对话框 125 this.dispose(); 126 } else if(e.getSource() == jb2) { 127 dispose(); 128 } 129 } 130 }