zoukankan      html  css  js  c++  java
  • 我写的学生信息管理系统,只是练习技术,,,例子比较别扭,哈哈

    登陆界面:(试下用我们学校门口为界面,爽呀!!)

    关于:

    控制面板:

    添加界面:

    (跟修改差不多)

      1 //添加学生对话框
      2 package com.model2;
      3 
      4 import java.awt.BorderLayout;
      5 import java.awt.Frame;
      6 import java.awt.GridLayout;
      7 import java.awt.event.ActionEvent;
      8 import java.awt.event.ActionListener;
      9 import java.beans.Statement;
     10 import java.sql.Connection;
     11 import java.sql.DriverManager;
     12 import java.sql.PreparedStatement;
     13 import java.sql.ResultSet;
     14 
     15 import javax.swing.JButton;
     16 import javax.swing.JDialog;
     17 import javax.swing.JLabel;
     18 import javax.swing.JOptionPane;
     19 import javax.swing.JPanel;
     20 import javax.swing.JTextField;
     21 
     22 public class StuAddDialog extends JDialog implements ActionListener
     23 {
     24     //定义需要的控件
     25     JLabel jl1,jl2,jl3,jl4,jl5,jl6;
     26     JButton jb1,jb2;
     27     JTextField jtf1,jtf2,jtf3,jtf4,jtf5,jtf6;
     28     JPanel jp1,jp2,jp3;
     29     
     30     //owner他的父窗口,title窗口名,modal指定是否模态窗口
     31     public StuAddDialog(Frame owner,String title,boolean modal)
     32     {    
     33         super(owner,title,modal);//调用父类方法,达到模式对话框效果
     34         jl1 =new JLabel("学号");
     35         jl2 =new JLabel("姓名");
     36         jl3 =new JLabel("性别");
     37         jl4 =new JLabel("年龄");
     38         jl5 =new JLabel("籍贯");
     39         jl6 =new JLabel("系别");
     40         
     41         jtf1=new JTextField();
     42         jtf2=new JTextField();
     43         jtf3=new JTextField();
     44         jtf4=new JTextField();
     45         jtf5=new JTextField();
     46         jtf6=new JTextField();
     47         
     48         jb1=new JButton("添加");
     49         jb1.addActionListener(this);
     50         jb2=new JButton("取消");
     51         
     52         jp1=new JPanel();
     53         jp2=new JPanel();
     54         jp3=new JPanel();
     55         
     56         //设置布局
     57         jp1.setLayout(new GridLayout(6,1));
     58         jp2.setLayout(new GridLayout(6,1));
     59         
     60         
     61         //添加组件
     62         jp1.add(jl1);
     63         jp1.add(jl2);
     64         jp1.add(jl3);
     65         jp1.add(jl4);
     66         jp1.add(jl5);
     67         jp1.add(jl6);
     68         
     69         jp2.add(jtf1);
     70         jp2.add(jtf2);
     71         jp2.add(jtf3);
     72         jp2.add(jtf4);
     73         jp2.add(jtf5);
     74         jp2.add(jtf6);
     75         
     76         jp3.add(jb1);
     77         jp3.add(jb2);
     78         
     79         this.add(jp1,BorderLayout.WEST);
     80         this.add(jp2,BorderLayout.CENTER);
     81         this.add(jp3,BorderLayout.SOUTH);
     82         
     83         
     84         
     85         //展现
     86         this.setSize(300,200);
     87         this.setVisible(true);
     88     }
     89 
     90     @Override
     91     public void actionPerformed(ActionEvent e)
     92     {
     93         if(e.getSource()==jb1)
     94         {
     95             //想添加
     96             StuModel temp=new StuModel();
     97             String sql="insert into stu values(?,?,?,?,?,?)";
     98             String []paras={jtf1.getText(),jtf2.getText(),jtf3.getText(),jtf4.getText(),jtf5.getText(),jtf6.getText()};
     99             if(!temp.updStu(sql,paras))
    100             {
    101                 //提示
    102                 JOptionPane.showMessageDialog(this,"添加失败");
    103                 
    104             }
    105             //关闭
    106             this.dispose();
    107                 
    108         }
    109         
    110     }
    111     
    112     
    113 }
     1 package com.test1;
     2 
     3 import java.awt.GridLayout;
     4 import java.awt.event.ActionEvent;
     5 import java.awt.event.ActionListener;
     6 
     7 import javax.swing.*;
     8 
     9 public class StuLogin extends JFrame implements ActionListener
    10 {
    11     
    12     public static void main(String[] args)
    13     {
    14         StuLogin sl=new StuLogin();
    15     }
    16     
    17     
    18     JPanel jp1,jp2,jp3;
    19     JLabel jl1,jl2,jl3;
    20     JButton jb1,jb2;
    21     JTextField jtf;
    22     JTextField jpf;
    23     public StuLogin()
    24     {
    25         jl1=new JLabel(new ImageIcon("image/GDOU.jpg"));
    26         
    27         jp1=new JPanel();
    28         jp2=new JPanel(new GridLayout(2,3));
    29         jb1=new JButton("关于版权");
    30         jb1.addActionListener(this);
    31         jb2=new JButton("点击进入》》");
    32         jb2.addActionListener(this);
    33         jl2=new JLabel("账号:",JLabel.CENTER);
    34         jl3=new JLabel("密码:",JLabel.CENTER);
    35         jtf=new JTextField();
    36         jpf=new JPasswordField();
    37         
    38         jp1.add(jl1);
    39         jp2.add(jl2);
    40         jp2.add(jtf);
    41         jp2.add(jb1);
    42         jp2.add(jl3);
    43         jp2.add(jpf);
    44         jp2.add(jb2);
    45         
    46         this.add(jp1,"North");
    47         this.add(jp2,"South");
    48         this.setSize(520,320);
    49         this.setIconImage((new ImageIcon("image/toux.png")).getImage());
    50         this.setTitle("广东海洋大学学生管理系统");
    51         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    52         this.setVisible(true);
    53     }
    54     @Override
    55     public void actionPerformed(ActionEvent arg0)
    56     {
    57         if(arg0.getSource()==jb2)
    58         {
    59             String userId=(String)jtf.getText();
    60             String passWd=(String)jpf.getText();
    61             
    62             if(userId.equals("1")&&passWd.equals("123456"))
    63             {
    64                 StuManage test3=new StuManage();
    65                 this.dispose();
    66             }else {
    67                 JOptionPane.showMessageDialog(this,"账号或密码错误!");
    68             }
    69             
    70         }else if(arg0.getSource()==jb1)
    71         {
    72             JOptionPane.showMessageDialog(this,"冼敏兵版权所有,冒仿必究!\n解释权归项目开发小组");
    73         }
    74         
    75     }
    76     
    77 }
    /*
     * 这是stu表的模型
     */
    package com.model2;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.util.Vector;
    
    import javax.swing.table.*;
    
    public class StuModel extends AbstractTableModel
    {
        
        //改表列名
        public String getColumnName(int arg0)
        {
            // TODO Auto-generated method stub
            return (String)this.columnNames.get(arg0);
        }
    
        Vector rowDate,columnNames;
        
        
        //定义连接数据库需要的
        PreparedStatement pS=null;
        Connection ct=null;
        ResultSet rS=null;
        String url="jdbc:microsoft:sqlserver://192.168.165.234:1433;databaseName=spdb";
        String user="sa";
        String passwd="123";
        String driver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
        //添加学生(增、删、改)
        public boolean updStu(String sql,String []paras)
        {
            boolean b=true;
            try
            {
                //加载驱动,得到连接,创建ps,执行操作
                Class.forName(driver);
                ct=DriverManager.getConnection(url,user,passwd);
                pS=ct.prepareStatement(sql);
                //给ps的?赋值
                for(int i=0;i<paras.length;i++)
                {
                    pS.setString(i+1,paras[i]);
                }
                if(pS.executeUpdate()!=1)
                {
                    b=false;
                }
                
            } catch (Exception e)
            {    b=false;
                e.printStackTrace();
            }finally
            {
                try
                {
                    if(rS!=null) rS.close();
                    if(pS!=null) pS.close();
                    if(ct!=null) ct.close();
                    
                } catch (Exception e2)
                {
                    e2.printStackTrace();
                    // TODO: handle exception
                }
            }
            return b;
        }
        public void init(String sql)
        {
            if(sql.equals(""))
            {
                sql="select * from stu";
            }
            //中间
            columnNames=new Vector();
            //设置列名
            columnNames.add("学号");
            columnNames.add("名字");
            columnNames.add("性别");
            columnNames.add("年龄");
            columnNames.add("籍贯");
            columnNames.add("系别");
            
            rowDate=new Vector();
            //rowDate可以存放多行
            
            
            try
            {
                //加载驱动
                Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
                ct=DriverManager.getConnection("jdbc:microsoft:sqlserver://192.168.1.103:1433;databaseName=spdb","sa","123");
                pS=ct.prepareStatement(sql);
                rS=pS.executeQuery();
                
                while(rS.next())
                {
                    Vector hang =new Vector();
                    hang.add(rS.getString(1));
                    hang.add(rS.getString(2));
                    hang.add(rS.getString(3));
                    hang.add(rS.getInt(4));
                    hang.add(rS.getString(5));
                    hang.add(rS.getString(6));
                
                    rowDate.add(hang);
                }
            } catch (Exception e)
            {
                e.printStackTrace();
            }finally
            {
                try
                {
                    if(rS!=null) rS.close();
                    if(pS!=null) pS.close();
                    if(ct!=null) ct.close();
                    
                } catch (Exception e2)
                {
                    e2.printStackTrace();
                    // TODO: handle exception
                }
            }
        }
        
        
        //通过传递的sql语句来获得数据模型
        public StuModel (String sql)
        {
            this.init(sql);
        }
        
        //构造函数,用于初始数据模型
        public StuModel()
        {
            this.init("");
        }
        //得到共有多少列
        public int getColumnCount()
        {
            // TODO Auto-generated method stub
            return this.columnNames.size();
        }
    
        //共有多少行
        public int getRowCount()
        {
            // TODO Auto-generated method stub
            return this.rowDate.size();
        }
    
        //得到某行某列数据
        public Object getValueAt(int row, int column)
        {
            // TODO Auto-generated method stub
            return ((Vector)this.rowDate.get(row)).get(column);
        }
    
    }
      1 /*
      2  * 这是stu表的模型
      3  */
      4 package com.test1;
      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 
     12 import javax.swing.table.*;
     13 
     14 public class StuModel extends AbstractTableModel
     15 {
     16     
     17     //改表列名
     18     public String getColumnName(int arg0)
     19     {
     20         // TODO Auto-generated method stub
     21         return (String)this.columnNames.get(arg0);
     22     }
     23 
     24     Vector rowDate,columnNames;
     25     
     26     
     27     //定义连接数据库需要的
     28     PreparedStatement pS=null;
     29     Connection ct=null;
     30     ResultSet rS=null;
     31     
     32     public void init(String sql)
     33     {
     34         if(sql.equals(""))
     35         {
     36             sql="select * from stu";
     37         }
     38         //中间
     39         columnNames=new Vector();
     40         //设置列名
     41         columnNames.add("学号");
     42         columnNames.add("名字");
     43         columnNames.add("性别");
     44         columnNames.add("年龄");
     45         columnNames.add("籍贯");
     46         columnNames.add("系别");
     47         
     48         rowDate=new Vector();
     49         //rowDate可以存放多行
     50         
     51         
     52         try
     53         {
     54             //加载驱动
     55             Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
     56             ct=DriverManager.getConnection("jdbc:microsoft:sqlserver://192.168.1.103:1433;databaseName=spdb","sa","123");
     57             pS=ct.prepareStatement(sql);
     58             rS=pS.executeQuery();
     59             
     60             while(rS.next())
     61             {
     62                 Vector hang =new Vector();
     63                 hang.add(rS.getString(1));
     64                 hang.add(rS.getString(2));
     65                 hang.add(rS.getString(3));
     66                 hang.add(rS.getInt(4));
     67                 hang.add(rS.getString(5));
     68                 hang.add(rS.getString(6));
     69             
     70                 rowDate.add(hang);
     71             }
     72         } catch (Exception e)
     73         {
     74             e.printStackTrace();
     75         }finally
     76         {
     77             try
     78             {
     79                 if(rS!=null) rS.close();
     80                 if(pS!=null) pS.close();
     81                 if(ct!=null) ct.close();
     82                 
     83             } catch (Exception e2)
     84             {
     85                 e2.printStackTrace();
     86                 // TODO: handle exception
     87             }
     88         }
     89     }
     90     
     91     public void addStu(String sql)
     92     {
     93         //根据用户输入的语句完成添加任务,然后必须重新获得新的模型
     94     }
     95     //通过传递的sql语句来获得数据模型
     96     public StuModel (String sql)
     97     {
     98         this.init(sql);
     99     }
    100     
    101     //构造函数,用于初始数据模型
    102     public StuModel()
    103     {
    104         this.init("");
    105     }
    106     //得到共有多少列
    107     public int getColumnCount()
    108     {
    109         // TODO Auto-generated method stub
    110         return this.columnNames.size();
    111     }
    112 
    113     //共有多少行
    114     public int getRowCount()
    115     {
    116         // TODO Auto-generated method stub
    117         return this.rowDate.size();
    118     }
    119 
    120     //得到某行某列数据
    121     public Object getValueAt(int row, int column)
    122     {
    123         // TODO Auto-generated method stub
    124         return ((Vector)this.rowDate.get(row)).get(column);
    125     }
    126 
    127 }
      1 package com.test1;
      2 
      3 import javax.swing.*;
      4 
      5 import java.sql.Connection;
      6 import java.sql.DriverManager;
      7 import java.sql.PreparedStatement;
      8 import java.sql.ResultSet;
      9 import java.util.*;
     10 import java.awt.*;
     11 import java.awt.event.*;
     12 
     13 public class StuManage extends JFrame implements ActionListener
     14 {
     15 
     16     //定义空件
     17     JPanel jp1,jp2;
     18     JLabel jl1;
     19     JButton jb1,jb2,jb3,jb4;
     20     JTable jt;
     21     JScrollPane jsp;
     22     JTextField jtf;
     23     
     24     StuModel sm;//定义一个
     25     
     26     //定义连接数据库需要的
     27         PreparedStatement pS=null;
     28         Connection ct=null;
     29         ResultSet rS=null;
     30     
     31     //public static void main(String[] args)
     32     //{
     33         //StuManage test3=new StuManage();
     34 
     35     //}
     36 
     37         public StuManage()
     38         {
     39             
     40             jp1=new JPanel();
     41             jtf=new JTextField(10);
     42             jb1=new JButton("查询");
     43             jb1.addActionListener(this);//以上方法方便用在同一个类李,通常用:jb1.setAction(arg0);
     44             jl1=new JLabel("请输入名字");
     45             
     46             //把各个控件加入jp1
     47             jp1.add(jl1);
     48             jp1.add(jtf);
     49             jp1.add(jb1);
     50             
     51             jp2=new JPanel();
     52             
     53             jb2=new JButton("添加");
     54             jb2.addActionListener(this);
     55             jb3=new JButton("修改");
     56             jb3.addActionListener(this);
     57             jb4=new JButton("删除");
     58             jb4.addActionListener(this);
     59             //把各个按钮加入jp2
     60             jp2.add(jb2);
     61             jp2.add(jb3);
     62             jp2.add(jb4);
     63             
     64         
     65             //创建一个数据模型对象
     66             sm=new StuModel();
     67             
     68             //初始化JTable
     69             jt=new JTable(sm);
     70             //初始化jsp
     71             jsp=new JScrollPane(jt);
     72             //把jsp放入jframe
     73             this.add(jsp);
     74             this.add(jp1,"North");
     75             this.add(jp2,"South");
     76             this.setSize(400,300);
     77             this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     78             this.setVisible(true);
     79             
     80         }
     81 
     82         @Override
     83         public void actionPerformed(ActionEvent arg0)
     84         {
     85             // 判断哪个按钮被点击
     86             if(arg0.getSource()==jb1)
     87             {
     88                  System.out.println("用户想查询");
     89                 //因为表的数据已经封装所以比较简单
     90                  String name=this.jtf.getText().trim();//trim()方法过滤空字符窜
     91                  //写sql语句
     92                  String sql="select * from stu where stuName='"+name+"'";
     93                  //构建新的数据模型类并更新
     94                 sm= new StuModel(sql);
     95                  jt.setModel(sm);
     96             }
     97             //当用户点击添加
     98             else if(arg0.getSource()==jb2)
     99             {
    100                 StuAddDialog sa=new StuAddDialog(this,"添加学生",true);
    101                 //刷新
    102                 sm=new StuModel();
    103                  jt.setModel(sm);
    104             }else if(arg0.getSource()==jb3)
    105             {
    106                 int rowNum=this.jt.getSelectedRow();
    107                 if(rowNum==-1)
    108                 {
    109                     //提示,this表示从这里框
    110                     JOptionPane.showMessageDialog(this,"请选择要修改的记录");
    111                     return;
    112                 }else {
    113                     new StuUpdDialog(this,"修改学生",true, sm, rowNum);
    114                     sm=new StuModel();
    115                      jt.setModel(sm);
    116                 }
    117             }
    118             else if(arg0.getSource()==jb4)
    119             {
    120                 //删除记录
    121                 //getSelectRow()返回用户点钟的行,如果一行都没选,就返回-1
    122                 int rowNum=this.jt.getSelectedRow();
    123                 if(rowNum==-1)
    124                 {
    125                     //提示,this表示从这里框
    126                     JOptionPane.showMessageDialog(this,"请选择要删除的记录");
    127                     return;
    128                     
    129                 }
    130                 //得到学生编号
    131                 String stuId=(String)sm.getValueAt(rowNum,0);
    132                 System.out.println("id="+stuId);
    133                 
    134                 //连接数据库,完成删除任务
    135                 try
    136                 {
    137                     Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
    138                     ct=DriverManager.getConnection("jdbc:microsoft:sqlserver://192.168.167.164:1433;databaseName=spdb","sa","123");
    139                     pS=ct.prepareStatement("delete from stu where stuId=?");
    140                     pS.setString(1,stuId);
    141                     pS.executeUpdate();
    142                 } catch (Exception e)
    143                 {
    144                     e.printStackTrace();
    145                     // TODO: handle exception
    146                 }finally
    147                 {
    148                     try
    149                     {
    150                         if(rS!=null) rS.close();
    151                         if(pS!=null) pS.close();
    152                         if(ct!=null) ct.close();
    153                             
    154                     } catch (Exception e2)
    155                     {
    156                         e2.printStackTrace();
    157                         // TODO: handle exception
    158                     }
    159                 }
    160                 //刷新
    161                 sm=new StuModel();
    162                  jt.setModel(sm);
    163             }
    164         }
    165 }

    以上代码模块分别为:

    StuAddDialog,StuLogin,StuModel,StuModel,StuManage//拷贝代码上去试试。。

  • 相关阅读:
    thinkphp框架 url 去除index.php
    读yaml文件警告: YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated, as the default Loader is unsafe. Please read https://msg.pyyaml.org/load for full details. data = yaml.load(fr)
    Appium 坐标定位元素
    Appium操作app弹窗
    AirtestIDE 教程(二)
    AirtestIDE 教程(一)
    Appium(五) aapt 不是内部或外部命令
    Appium(四) selenium.common.exceptions.WebDriverException: Message: An unknown server-side error occurred while processing the command. Original error: You must include a platformName capability
    Appium(三) 打开 uiautomatorviewer.bat 闪退问题
    Appium(二) 下载安装 appium-python-client
  • 原文地址:https://www.cnblogs.com/xmb7/p/2682271.html
Copyright © 2011-2022 走看看