期末实训项目

实训内容

连接数据库MySQL和Java来公共实现一款可视化的学生管理系统。让该系统拥有登录界面,管理界面。

系统目录

需要创建的目录类
在这里插入图片描述

主体

大概代码展示
在这里插入图片描述
loginframe部分
在这里插入图片描述
代码展示

public class Main extends JFrame implements ActionListener{
    public static void main(String[] args) throws IOException {
        new Main();
        final int WIDTH=800;
        final int HEIGHT=500;
    }

    private JLabel queryLab = null;
    private JTextField queryTxt = null;
    private JButton queryBtn = null;
    private JButton allBtn = null;
    private JTable resultTb = null;
    private JScrollPane jsp = null;
    private JButton addBtn = null;
    private JButton deleteBtn = null;
    private JButton updateBtn = null;
    private JPanel top = null;
    private JPanel bottom = null;

    private Model sm = null;

    public Main() throws IOException {
        super("学生管理系统");
        
        queryLab = new JLabel("请输入姓名:");
        queryTxt = new JTextField(10);
        queryBtn = new JButton("查询");
        allBtn = new JButton("全部");

        queryBtn.addActionListener(this);
        queryBtn.setActionCommand("query");
        allBtn.addActionListener(this);
        allBtn.setActionCommand("all");

        addBtn = new JButton("添加");
        deleteBtn = new JButton("删除");
        updateBtn = new JButton("修改");

        addBtn.addActionListener(this);
        addBtn.setActionCommand("add");
        deleteBtn.addActionListener(this);
        deleteBtn.setActionCommand("delete");
        updateBtn.addActionListener(this);
        updateBtn.setActionCommand("update");

        top = new JPanel();
        top.add(queryLab);
        top.add(queryTxt);
        top.add(queryBtn);
        top.add(allBtn);

        bottom = new JPanel();
        bottom.add(addBtn);
        bottom.add(deleteBtn);
        bottom.add(updateBtn);

        sm = new Model();
        String sql = "select * from stu";
        sm.queryStu(sql, null);
        resultTb = new JTable(sm);
        jsp = new JScrollPane(resultTb);

        this.add(top,BorderLayout.NORTH);
        this.add(jsp,BorderLayout.CENTER);
        this.add(bottom,BorderLayout.SOUTH);

        this.setBounds(500,250,500,300);
//        this.setIconImage(ImageIO.read(new File("D:\\yuandaima\\shishuo\\src\\studentManage\\img\\登录.png")));
        this.setVisible(true);
        this.setResizable(false);
    }

    //监听
    @Override
    public void actionPerformed(ActionEvent e)
    {
        // TODO Auto-generated method stub
        if(e.getActionCommand().equals("query")) {

            String name = queryTxt.getText().trim();
            if(name.length() != 0) {

                String sql = "select * from stu where stuName=?";
                String []paras = {name};

                jtableUpdate(sql, paras);
            } else {

                JOptionPane.showMessageDialog(this, "姓名输入不能为空");
            }
        } else if(e.getActionCommand().equals("add")) {

            new Add(this, "添加学生信息", true);
            String sql = "select * from stu";
            jtableUpdate(sql, null);
        } else if(e.getActionCommand().equals("all")) {

            String sql = "select * from stu";
            jtableUpdate(sql, null);
        } else if(e.getActionCommand().equals("delete")) {

            int rowNum = this.resultTb.getSelectedRow();
            if(rowNum == -1) {
                JOptionPane.showMessageDialog(this, "请选择一行");
                return ;
            }

            String stuId = (String)sm.getValueAt(rowNum, 0);

            String sql = "delete from stu where stuId=?";
            String []paras = {stuId};
            Model tmp = new Model();
            tmp.cudStu(sql, paras);

            sql = "select * from stu";
            jtableUpdate(sql, null);
        } else if(e.getActionCommand().equals("update")) {

            int rowNum = this.resultTb.getSelectedRow();
            if(rowNum == -1) {
                JOptionPane.showMessageDialog(this, "请选择一行");
                return ;
            }
            new Set(this, "修改学生信息", true, sm, rowNum);
            String sql = "select * from stu";
            jtableUpdate(sql, null);
        }
    }

    public void jtableUpdate(String sql, String[] paras)
    {

        sm = new Model();
        sm.queryStu(sql, paras);
        resultTb.setModel(sm);
    }

}


运行结果

登录界面
在这里插入图片描述

实训总结

MySQL在连接Java时出现连接不上,代码敲出来,运行不出结果,代码老显示红色,并且很大内容需要依托老师的代码源。登录进学生管理系统后,有些程序无法运行,增加或者修改学生信息出错等,都是需要后面继续完善加强的。与Python的学生管理系统不同,Java和数据库的连接,使整个学生管理系统需要掌握的知识点增加,对代码运用熟练度要求都相对增加,在做学生管理系统时展现出来的困难都是需要在后面学习中解决的。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值