首页 > 代码库 > java版本的学生管理系统

java版本的学生管理系统

  1 import java.awt.BorderLayout;  2 import java.awt.Color;  3 import java.awt.Frame;  4 import java.awt.event.ActionEvent;  5 import java.awt.event.ActionListener;  6 import java.io.UnsupportedEncodingException;  7 import java.sql.Connection;  8 import java.sql.DriverManager;  9 import java.sql.PreparedStatement; 10 import java.sql.ResultSet; 11 import java.sql.SQLException; 12 import java.util.Vector; 13  14 import javax.swing.Box; 15 import javax.swing.ButtonGroup; 16 import javax.swing.JButton; 17 import javax.swing.JDialog; 18 import javax.swing.JFrame; 19 import javax.swing.JLabel; 20 import javax.swing.JOptionPane; 21 import javax.swing.JPanel; 22 import javax.swing.JRadioButton; 23 import javax.swing.JScrollPane; 24 import javax.swing.JTable; 25 import javax.swing.JTextField; 26 import javax.swing.plaf.OptionPaneUI; 27 import javax.swing.table.AbstractTableModel; 28  29 /** 30  * @author Administrator mini版学生管理系统 实现: crud所有功能 31  */ 32  33 public class Demo_4 { 34  35     /** 36      * @param args 37      *            作者: 龚细军 date: 2014/11/23 38      *            完成时间: 2014/11/25/ 39      */ 40  41     public static void main(String args[]) { 42         // 创建一个面板对象 43         MyJFrame mf = new MyJFrame(); 44     } 45 } 46  47 // 定义一个我自己的主界面 48 class MyJFrame extends JFrame implements ActionListener { 49  50     JTable jt; 51     MyJTable mjt; 52     // 必要地分布板块 53     JPanel jpup, jpdown, jpcenter; 54     // 实现数据的 增删查改即crud操作 55     JButton[] button = new JButton[4]; 56     // 用来辅助查找模块的框框 57     JTextField text; 58  59     JScrollPane jsp = null; 60  61     /* 62      * 构造函数 功能: 实现窗口的初始化 63      */ 64     MyJFrame() { 65         mjt = new MyJTable(); 66         jt = new JTable(mjt); 67         // jt.setModel(mjt); 68         jpup = new JPanel(); 69         jpdown = new JPanel(); 70         jpcenter = new JPanel(); 71         text = new JTextField(14); 72         button[0] = new JButton(" 查询  "); 73         button[1] = new JButton(" 添加  "); 74         button[2] = new JButton(" 修改  "); 75         button[3] = new JButton(" 删除  "); 76         for (int i = 0; i < 4; i++) 77             button[i].addActionListener(this); 78         jpup.add(new JLabel("用户信息")); 79         jpup.add(text); 80         jpup.add(button[0]); 81         for (int i = 1; i < 4; i++) 82             jpdown.add(button[i]); 83         jt.setBackground(Color.CYAN); 84         jsp = new JScrollPane(jt); 85         jpcenter.add(jsp); 86         // 设置一些布局 borderLaRyou 87         this.add(jpup, BorderLayout.NORTH); 88         this.add(jpcenter, BorderLayout.CENTER); 89         this.add(jpdown, BorderLayout.SOUTH); 90         // 设置窗口的标题 91         this.setTitle("mini版的学生管理系统"); 92         // 设置窗口的尺寸 93         this.setSize(500, 300); 94         // 窗口的尺寸不可改变 95         // this.setResizable(false); 96         // 设置窗口是否可见 97         this.setVisible(true); 98         // 设置窗口的注销模式 99         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);100     }101 102     @Override103     public void actionPerformed(ActionEvent e) {104         // TODO Auto-generated method stub105         int i = 0;106         for (i = 0; i < 4; i++)107             if (e.getSource() == button[i])108                 break;109         String cmd = text.getText().toString().trim();110         switch (i) {111         // 查询112         case 0:113             if (cmd.isEmpty()) {114                 JOptionPane.showMessageDialog(button[0], "请输入用户名");115                 mjt = new MyJTable();116             } else117                 mjt = new MyJTable(cmd);118             jt.setModel(mjt);119             break;120         // 添加121         case 1:122             AddJFrame myAdd = new AddJFrame(this, true);123             mjt = new MyJTable();124             jt.setModel(mjt);125             break;126         // 修改127         case 2:128             // 需要得到我们点中的行列号129             int rownum = this.jt.getSelectedRow();130             if (-1 == rownum) {131                 JOptionPane.showMessageDialog(this, "请选中你要修改人");132                 return;133             } else {134                 String name = (String) mjt.getValueAt(rownum, 0);135                 UpdataJFrame Uj = new UpdataJFrame(this, true, name);136                 mjt = new MyJTable();137                 jt.setModel(mjt);138             }139             break;140         // 删除141         case 3:142             int row = this.jt.getSelectedRow();143             if (-1 == row) {144                 JOptionPane.showMessageDialog(this, "请选中你要修改人");145                 return;146             } else {147                 String name = (String) mjt.getValueAt(row, 1);148                 Delete det = new Delete(this, name.trim(), true);149             }150             mjt = new MyJTable();151             jt.setModel(mjt);152             break;153         // 啥也不干154         default:155 156             break;157         }158     }159 160 }161 162 // 定义一个自己的table类163 class MyJTable extends AbstractTableModel {164 165     // sql的几个变量166     Connection ct = null;167     PreparedStatement ps = null;168     ResultSet rs = null;169     Vector rowdata, colName;170 171     // 非带参数的构造函数,用于初始化172     MyJTable() {173         this.func("", false);174     }175 176     // 带参数的构造函数,用于查询177     MyJTable(String name) {178         this.func(name, true);179     };180 181     public void func(String name, boolean tag) {182         String sql;183         try {184             // 加载上jdbc_odbc驱动185             Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");186             // 连接sql187             ct = DriverManager.getConnection(188                             "jdbc:microsoft:sqlserver://localhost:1434;databaseName=Demo_1",189                             "sa", "869261636123");190             if (!tag) {191                 sql = new String("select * from stu");192                 ps = ct.prepareStatement(sql);193             } else {194                 sql = new String("Select * from Stu where Stuname=?");195                 ps = ct.prepareStatement(sql);196                 ps.setString(1, name);197             }198             rs = ps.executeQuery();199             /*200              * 设置表单的属性201              */202             colName = new Vector();203             rowdata = http://www.mamicode.com/new Vector();204             String[] ss = { "学号", "名字", "性别", "年龄", "家庭地址", "系别" };205             for (int i = 0; i < 6; i++)206                 colName.add(ss[i]);207             while (rs.next()) {208                 Vector hang = new Vector();209                 for (int i = 1; i <= 6; i++) {210                     if (4 == i)211                         hang.add(rs.getInt(i));212                     else {213                         String st = new String(214                                 (rs.getString(i)).getBytes("gbk"), "gb2312");215                         hang.add(st);216                     }217                 }218                 rowdata.add(hang);219             }220         } catch (Exception e) {221             // TODO Auto-generated catch block222             e.printStackTrace();223         } finally {224             // 关闭一些窗口225             try {226                 if (rs != null)227                     rs.close();228                 if (ps != null)229                     ps.close();230                 if (ct != null)231                     ct.close();232             } catch (SQLException e) {233                 // TODO Auto-generated catch block234                 e.printStackTrace();235             }236         }237     }238 239     // 返回它的行数240     @Override241     public int getRowCount() {242         // TODO Auto-generated method stub243         return this.rowdata.size();244     }245 246     // 返回它的列数247     @Override248     public int getColumnCount() {249         // TODO Auto-generated method stub250         return this.colName.size();251     }252 253     // 返回该表单的内容254     @Override255     public Object getValueAt(int rowIndex, int columnIndex) {256         // TODO Auto-generated method stub257         return ((Vector) this.rowdata.get(rowIndex)).get(columnIndex);258     }259 260     @Override261     public String getColumnName(int column) {262         // TODO Auto-generated method stub263         return (String) this.colName.get(column);264     }265 }266 267 // 定义一个添加数据的界面268 class AddJFrame extends JDialog implements ActionListener {269 270     // 填写信息的空格271     private String sex = null;272     JTextField[] jt = new JTextField[5];273     JButton[] jb = new JButton[2];274     // 单选按钮275     JRadioButton[] jradio = new JRadioButton[2];276     // 组合单选按钮277     ButtonGroup group;278     // 用于设置 盒式布局 BoxLayout279     Box[] mybox = new Box[4];280     JPanel jp, jp1;281     // SQL几个常用的变量类型282     Connection ct = null;283     PreparedStatement ps = null;284 285     public AddJFrame(Frame Father, boolean Model) {286         // 采用模式对话框287         super(Father, Model);288         for (int i = 0; i < 5; i++)289             jt[i] = new JTextField(10);290         jb[0] = new JButton("确认");291         jb[0].addActionListener(this);292         jb[1] = new JButton("取消");293         jb[1].addActionListener(this);294         jradio[0] = new JRadioButton("男");295         jradio[0].addActionListener(this);296         jradio[1] = new JRadioButton("女");297         jradio[1].addActionListener(this);298         // 设置水平299         mybox[0] = Box.createVerticalBox();300         mybox[0].add(Box.createVerticalStrut(15));301         mybox[0].add(new JLabel("学号:"));302         mybox[0].add(Box.createVerticalStrut(10));303         mybox[0].add(new JLabel("名字:"));304         mybox[0].add(Box.createVerticalStrut(20));305         mybox[0].add(new JLabel("性别:"));306         mybox[0].add(Box.createVerticalStrut(25));307         mybox[0].add(new JLabel("年龄:"));308         mybox[0].add(Box.createVerticalStrut(10));309         mybox[0].add(new JLabel("家庭住址:"));310         mybox[0].add(Box.createVerticalStrut(10));311         mybox[0].add(new JLabel("系别:"));312         mybox[0].add(Box.createVerticalStrut(10));313         // 设置另一边的水平314         mybox[1] = Box.createVerticalBox();315         mybox[1].add(Box.createVerticalStrut(18));316         mybox[1].add(jt[0]);317         mybox[1].add(Box.createVerticalStrut(8));318         mybox[1].add(jt[1]);319         mybox[1].add(Box.createVerticalStrut(8));320         jp = new JPanel();321         // 单选群322         group = new ButtonGroup();323         group.add(jradio[0]);324         group.add(jradio[1]);325         jp.add(jradio[0]);326         jp.add(jradio[1]);327         mybox[1].add(jp);328         /*329          * group =new ButtonGroup(); group.add(jradio[0]); group.add(jradio[1]);330          * mybox[1].add(group); 此处需要改变的 东西,改成combox来进行,或者直接改成一样的文本框也许331          */332         mybox[1].add(Box.createVerticalStrut(5));333         mybox[1].add(jt[2]);334         mybox[1].add(Box.createVerticalStrut(8));335         mybox[1].add(jt[3]);336         mybox[1].add(Box.createVerticalStrut(8));337         mybox[1].add(jt[4]);338         mybox[1].add(Box.createVerticalStrut(8));339         // 按钮部分340         jp1 = new JPanel();341         jp1.add(jb[0], BorderLayout.EAST);342         jp1.add(jb[1], BorderLayout.WEST);343         mybox[2] = Box.createHorizontalBox();344         mybox[2].add(mybox[0]);345         mybox[2].add(Box.createHorizontalStrut(10));346         mybox[2].add(mybox[1]);347         this.add(mybox[2], BorderLayout.NORTH);348         this.add(jp1, BorderLayout.SOUTH);349         init();350     }351 352     // 显示板块部分353     public void init() {354         this.setTitle("个人信息板块");355         this.setBackground(Color.magenta);356         this.setBounds(400, 300, 300, 300);357         this.setVisible(true);358         // this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);359     }360 361     @Override362     public void actionPerformed(ActionEvent e) {363         // TODO Auto-generated method stub364         // 将数据写入数据库365         if (e.getSource() == jb[0]) {366             // 对写入的数据进行处理367             try {368 369                 Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");370                 ct = DriverManager371                         .getConnection(372                                 "jdbc:microsoft:sqlserver://localhost:1434;databaseName=Demo_1",373                                 "sa", "869261636123");374                 ps = ct.prepareStatement("insert into stu values(?,?,?,?,?,?)");375                 for (int i = 1, j = 0; i <= 6; i++) {376                     if (3 == i) {377                         // 去掉多余的空格,但是这里存在一个小bug(合理的处理为正则表达式)378                         ps.setString(i, this.getSex().trim());379                         // ps.setInt(i,);380                     } else if (4 == i) {381                         String tem = jt[j].getText().toString().trim();382                         int value =http://www.mamicode.com/ Integer.valueOf(tem).intValue();383                         ps.setInt(i, value);384                         ++j;385                     } else {386                         ps.setString(i, jt[j].getText().toString().trim());387                         ++j;388                     }389                 }390                 int i = ps.executeUpdate();391 392                 if (1 == i)393                     JOptionPane.showMessageDialog(this, "添加成功!");394                 else395                     JOptionPane.showMessageDialog(this, "添加失败!");396 397             } catch (Exception e1) {398                 // TODO Auto-generated catch block399                 e1.printStackTrace();400             } finally {401                 // 关闭数据库的一些调用函数402                 try {403                     if (ps != null)404                         ps.close();405                     if (ct != null)406                         ct.close();407                 } catch (SQLException e1) {408                     // TODO Auto-generated catch block409                     e1.printStackTrace();410                 }411             }412             this.dispose();413         } else if (e.getSource() == jb[0])414             // 释放点该窗口, 退出该版面415             this.dispose();416         else if (jradio[0].isSelected()) {417             // 将名字改为男418             this.setSex("男");419         } else if (jradio[1].isSelected()) {420             // 将名字改为女421             this.setSex("女");422         }423     }424 425     public String getSex() {426         return this.sex;427     }428 429     public void setSex(String sex) {430         this.sex = sex;431     }432 433 }434 435 // 定义一个修改数据的界面436 class UpdataJFrame extends JDialog implements ActionListener {437 438     // 填写信息的空格439     private String sex = null;440     JTextField[] jt = new JTextField[5];441     JButton[] jb = new JButton[2];442     // 单选按钮443     JRadioButton[] jradio = new JRadioButton[2];444     // 组合单选按钮445     ButtonGroup group;446     // 用于设置 盒式布局 BoxLayout447     Box[] mybox = new Box[4];448     JPanel jp, jp1;449     // SQL几个常用的变量类型450     Connection ct = null;451     PreparedStatement ps = null;452 453     public UpdataJFrame(Frame Father, boolean Model, String name) {454         // 采用模式对话框455         super(Father, Model);456         for (int i = 0; i < 5; i++)457             jt[i] = new JTextField(10);458         // 设置为仅仅读取459         jt[0].setEditable(false);460         // 下方的几个按钮461         jb[0] = new JButton("确认");462         jb[0].addActionListener(this);463         jb[1] = new JButton("取消");464         jb[1].addActionListener(this);465         jradio[0] = new JRadioButton("男");466         jradio[0].addActionListener(this);467         jradio[1] = new JRadioButton("女");468         jradio[1].addActionListener(this);469         // 设置水平470         mybox[0] = Box.createVerticalBox();471         mybox[0].add(Box.createVerticalStrut(15));472         mybox[0].add(new JLabel("学号:"));473         mybox[0].add(Box.createVerticalStrut(10));474         mybox[0].add(new JLabel("名字:"));475         mybox[0].add(Box.createVerticalStrut(20));476         mybox[0].add(new JLabel("性别:"));477         mybox[0].add(Box.createVerticalStrut(25));478         mybox[0].add(new JLabel("年龄:"));479         mybox[0].add(Box.createVerticalStrut(10));480         mybox[0].add(new JLabel("家庭住址:"));481         mybox[0].add(Box.createVerticalStrut(10));482         mybox[0].add(new JLabel("系别:"));483         mybox[0].add(Box.createVerticalStrut(10));484         // 设置另一边的水平485         mybox[1] = Box.createVerticalBox();486         mybox[1].add(Box.createVerticalStrut(18));487         mybox[1].add(jt[0]);488         mybox[1].add(Box.createVerticalStrut(8));489         mybox[1].add(jt[1]);490         mybox[1].add(Box.createVerticalStrut(8));491         jp = new JPanel();492         // 单选群493         group = new ButtonGroup();494         group.add(jradio[0]);495         group.add(jradio[1]);496         jp.add(jradio[0]);497         jp.add(jradio[1]);498         mybox[1].add(jp);499 500         /*501          * group =new ButtonGroup(); group.add(jradio[0]); group.add(jradio[1]);502          * mybox[1].add(group); 此处需要改变的 东西,改成combox来进行,或者直接改成一样的文本框也许503          */504         mybox[1].add(Box.createVerticalStrut(5));505         mybox[1].add(jt[2]);506         mybox[1].add(Box.createVerticalStrut(8));507         mybox[1].add(jt[3]);508         mybox[1].add(Box.createVerticalStrut(8));509         mybox[1].add(jt[4]);510         mybox[1].add(Box.createVerticalStrut(8));511 512         // 按钮部分513         jp1 = new JPanel();514         jp1.add(jb[0], BorderLayout.EAST);515         jp1.add(jb[1], BorderLayout.WEST);516         mybox[2] = Box.createHorizontalBox();517         mybox[2].add(mybox[0]);518         mybox[2].add(Box.createHorizontalStrut(10));519         mybox[2].add(mybox[1]);520         Connect(name);521         this.add(mybox[2], BorderLayout.NORTH);522         this.add(jp1, BorderLayout.SOUTH);523         init();524     }525 526     // 显示板块部分527     public void init() {528         this.setTitle("个人信息板块");529         this.setBackground(Color.magenta);530         this.setBounds(400, 300, 300, 300);531         this.setVisible(true);532 533         // this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);534     }535 536     public void Connect(String name) {537         ResultSet rs = null;538         // 驱动加载539         try {540             Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");541             ct = DriverManager542                     .getConnection(543                             "jdbc:microsoft:sqlserver://localhost:1434;databaseName=Demo_1",544                             "sa", "869261636123");545             String sql = new String("Select * from Stu where stuId=?");546             ps = ct.prepareStatement(sql);547             ps.setString(1, name);548             rs = ps.executeQuery();549             while (rs.next()) {550                 int cnt = 1;551                 for (int i = 1; i <= 6; i++) {552                     if (4 == i) {553                         // 整数转化为String554                         jt[i - cnt].setText(String.valueOf(rs.getInt(i)));555                     } else {556                         String st = null;557                         try {558                             st = new String((rs.getString(i)).getBytes("gbk"),559                                     "gb2312");560                         } catch (UnsupportedEncodingException e) {561                             // TODO Auto-generated catch block562                             e.printStackTrace();563                         }564                         if (i == 3) {565                             if (st.equals("男")) {566                                 jradio[0].setSelected(true);567                                 this.setSex("男");568                             } else if (st.equals("女")) {569                                 jradio[1].setSelected(true);570                                 this.setSex("女");571                             } else572                                 JOptionPane.showMessageDialog(this, "性别处出现乱码");573                             cnt++;574                         } else575                             jt[i - cnt].setText(st);576                     }577                 }578 579             }580 581         } catch (ClassNotFoundException | SQLException e) {582             // TODO Auto-generated catch block583             e.printStackTrace();584         } finally {585             try {586                 if (rs != null)587                     rs.close();588                 if (ps != null)589                     ps.close();590                 if (ct != null)591                     ct.close();592             } catch (SQLException e) {593                 // TODO Auto-generated catch block594                 e.printStackTrace();595             }596         }597     }598 599     @Override600     public void actionPerformed(ActionEvent e) {601         // TODO Auto-generated method stub602         // 将数据写入数据库603         if (e.getSource() == jb[0]) {604             // 对写入的数据进行处理605             try {606                 // 驱动加载607                 Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");608                 ct = DriverManager609                         .getConnection(610                                 "jdbc:microsoft:sqlserver://localhost:1434;databaseName=Demo_1",611                                 "sa", "869261636123");612                 ps = ct.prepareStatement("Update stu Set Stuname=?,StuSex=?,StuAge=?,StuJg=?,StuDept=? where StuId=?");613                 // 回来修改........614                 // 名字615                 ps.setString(1, jt[1].getText().toString().trim());616                 // 性别617                 ps.setString(2, this.getSex().trim());618                 // 年龄619                 String tem = jt[2].getText().toString().trim();620                 int value =http://www.mamicode.com/ Integer.valueOf(tem).intValue();621                 ps.setInt(3, value);622                 // 籍贯623                 ps.setString(4, jt[3].getText().toString().trim());624                 // 系别625                 ps.setString(5, jt[4].getText().toString().trim());626                 // 学号627                 ps.setString(6, jt[0].getText().toString().trim());628                 // 更新629                 int i = ps.executeUpdate();630                 if (1 == i)631                     JOptionPane.showMessageDialog(this, "添加成功!");632                 else633                     JOptionPane.showMessageDialog(this, "添加失败!");634 635             } catch (Exception e1) {636                 // TODO Auto-generated catch block637                 e1.printStackTrace();638             } finally {639                 // 关闭数据库的一些调用函数640                 try {641                     if (ps != null)642                         ps.close();643                     if (ct != null)644                         ct.close();645                 } catch (SQLException e1) {646                     // TODO Auto-generated catch block647                     e1.printStackTrace();648                 }649             }650             this.dispose();651         } else if (e.getSource() == jb[0])652             // 释放点该窗口, 退出该版面653             this.dispose();654         else if (jradio[0].isSelected()) {655             // 将名字改为男656             this.setSex("男");657         } else if (jradio[1].isSelected()) {658             // 将名字改为女659             this.setSex("女");660         }661     }662 663     public String getSex() {664         return this.sex;665     }666 667     public void setSex(String sex) {668         this.sex = sex;669     }670 }671 672 // 删除界面类673 class Delete extends JDialog implements ActionListener {674 675     // 设置为确认和取消两个按钮676     JButton[] button = new JButton[2];677     JPanel jp;678     JTable jt;679     JScrollPane jsp = null;680     // 设置为一个删除的表单681     MyJTable mytable;682     // SQL几个常用的变量类型683 684     Connection ct = null;685     PreparedStatement ps = null;686     ResultSet rs = null;687     private String name;688 689     public Delete(Frame ower, String name, boolean Model) {690 691         super(ower, Model);692         this.setName(name);693         jp = new JPanel();694         button[0] = new JButton("确定");695         button[0].addActionListener(this);696         button[1] = new JButton("取消");697         button[1].addActionListener(this);698         jp.add(button[0]);699         jp.add(button[1]);700 701         // 设置一个我的列表的界面702         mytable = new MyJTable(name);703         jt = new JTable(mytable);704         JScrollPane jsp = new JScrollPane(jt);705         this.add(jp, BorderLayout.SOUTH);706         this.add(jsp, BorderLayout.NORTH);707         this.setTitle("删除界面");708         this.setSize(400, 250);709         this.setResizable(false);710         this.setVisible(true);711 712     }713 714     public String getName() {715         return name;716     }717 718     public void setName(String name) {719         this.name = name;720     }721 722     @Override723     public void actionPerformed(ActionEvent e) {724         // TODO Auto-generated method stub725         if (e.getSource() == button[0]) {726 727             try {728                 // 加载上jdbc_odbc驱动729                 Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");730                 // 连接sql731                 ct = DriverManager732                         .getConnection(733                                 "jdbc:microsoft:sqlserver://localhost:1434;databaseName=Demo_1",734                                 "sa", "869261636123");735                 String sql = new String("delete  from Stu where Stuname=?");736                 ps = ct.prepareStatement(sql);737                 ps.setString(1, this.getName().trim());738 739                 int i = ps.executeUpdate();740                 if (1 == i)741                     JOptionPane.showMessageDialog(this, "删除成功");742                 else743                     JOptionPane.showMessageDialog(this, "删除失败了啦!");744 745             } catch (ClassNotFoundException | SQLException e1) {746                 // TODO Auto-generated catch block747                 e1.printStackTrace();748             } finally {749                 try {750                     if (ps != null)751                         ps.close();752                     if (ct != null)753                         ct.close();754                 } catch (SQLException e1) {755                     // TODO Auto-generated catch block756                     e1.printStackTrace();757                 }758             }759         }760 761         this.dispose();762     }763 }
View Code

java版本的学生管理系统