基于JavaSwing+MySql的学生信息管理系统 - Go语言中文社区

基于JavaSwing+MySql的学生信息管理系统


最近的生活感想

因为最近在写一个和QQ差不多的聊天软件,所以最近时间比较紧。昨天学校临时发了一个作业。花了大概半天的时间写了一个界面版本的操作数据库的学生信息管理 系统,因为时间很少所以界面就随便写了写,代码也没有优化。大家将就看吧。先上界面图片!

登录界面

登录界面

主界面

主界面

查询所有学生信息

查询所有学生信息

根据学生学号查询学生信息

查询学生信息

根据学号修改学生信息,需要验证学生学号是否存在

根据学号修改学生信息

根据学号删除学生信息

这里写图片描述

插入新的学生信息

插入新的学生信息

* 因为时间仓促,大部分界面都是使用NetBeans直接拉出来的。代码写的很乱,逻辑很简单,主要就是操作数据库。下面我只拿我觉得有点儿困难的地方代码展示出来。*

  1. 最重要的逻辑代码类,执行各种SQL语句,后端!!!
package com.langxikeji.JDBC;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Vector;

public class M_Student {

    static Connection conn = Connections.getConnection();



    //加入一条或者多条学生信息

    public static void Insert_Stu(String name,String gender,int age,String school){

        try {
            PreparedStatement ps=conn.prepareStatement(SQLpool.Insert_Stu);

            ps.setString(1, name);

            ps.setString(2, gender);

            ps.setInt(3, age);

            ps.setString(4, school);

            ps.executeUpdate();

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    //先查找
    public static boolean Check_ById(int id){

        try {
            PreparedStatement ps=conn.prepareStatement(SQLpool.Check_ById);


            ps.setInt(1, id);

            ResultSet rs=ps.executeQuery();

            while(rs.next()){
                return true;
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return false;

    }

    //在修改
    public static void Updata_ById(int id,String name,String gender,int age,String school){
        try {
            PreparedStatement ps=conn.prepareStatement(SQLpool.Updata_ById);

            ps.setInt(1, id);

            ps.setString(2, name);

            ps.setString(3, gender);

            ps.setInt(4, age);

            ps.setString(5, school);

            int rs=ps.executeUpdate();

            System.out.println(rs);

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }
    //根据学号删除学生信息
    public static boolean Del_ById(int id){

        try {
            PreparedStatement ps=conn.prepareStatement(SQLpool.Del_ById);

            ps.setInt(1, id);

            int rs=ps.executeUpdate();

            while(rs>0){
                return true;
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        return false;

    }
    //根据学号查看学生信息
    public static String [][] Stu_ById(int id){

        Vector<Student>listbyid=new Vector<>();

        String [][]ById=new String [1][5];

        try {
            PreparedStatement ps=conn.prepareStatement(SQLpool.Check_ById);

            ps.setInt(1, id);

            ResultSet rs=ps.executeQuery();

            while (rs.next()) {
                Student st = new Student();

                st.setId(rs.getInt(1));

                st.setName(rs.getString(2));

                st.setGender(rs.getString(3));

                st.setAge(rs.getInt(4));

                st.setSchool(rs.getString(5));

                listbyid.add(st);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        for(int i=0;i<listbyid.size();i++){

            ById[0][0]=String.valueOf(listbyid.get(i).getId());

            ById[0][1]=listbyid.get(i).getName();

            ById[0][2]=listbyid.get(i).getGender();

            ById[0][3]=String.valueOf(listbyid.get(i).getAge());

            ById[0][4]=listbyid.get(i).getSchool();
        }
        return ById;
    }

    //查询到所有学生的信息,返回一个二维数组
    public static String[][] All_Stu() {

        //二维数组初始化
        String[][] v_info = null ;

        List<Student> list = new ArrayList<>();

        //控制行数
        int rowNum = 0;

        try {
            PreparedStatement ps = conn.prepareStatement(SQLpool.Check_All);

            ResultSet rs = ps.executeQuery();

            while (rs.next()) {
                rowNum++;
                Student st = new Student();

                st.setId(rs.getInt(1));

                st.setName(rs.getString(2));

                st.setGender(rs.getString(3));

                st.setAge(rs.getInt(4));

                st.setSchool(rs.getString(5));

                list.add(st);

            }

            v_info=new String[rowNum][5];

            for(int j=0;j<list.size();j++){

                //拿到集合里面的每一个对象
                Student st=list.get(j);

                v_info[j][0]=String.valueOf(st.getId());
                v_info[j][1]=st.getName();
                v_info[j][2]=st.getGender();
                v_info[j][3]=String.valueOf(st.getAge());
                v_info[j][4]=st.getSchool();

            }


        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return v_info;
    }
}

2.主界面显示,主要的界面显示逻辑代码。

package com.langxikeji.JDBC;

import java.awt.Color;
import java.awt.Dialog.ModalExclusionType;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class V_Student implements ActionListener {

    private JFrame St_frame;

    private SimpleDateFormat sdf;

    private JTextField nowtime;
    // 菜单项组件
    JMenuItem All_ST, ByIdFromSt, ByIdDelSt, ByIdUpData, Insert_St;
    // 初始画布
    private JPanel panel = new JPanel();
    // 查找画布
    private V_Check_Panel Checkpanel = new V_Check_Panel();
     //根据ID查找画布
    private V_ById_Panel Byidpanel = new V_ById_Panel();
     //删除ID信息画布
    private V_DelById_Panel Delpanel = new V_DelById_Panel();
     //背景图片画面
    private Draw_BG Drawpanel=new Draw_BG();
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    V_Student window = new V_Student();
                    window.St_frame.setVisible(true);
                    window.St_frame.setResizable(false);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public V_Student() {
        initialize();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

         //根据按键的指令重绘不同的页面
        if (e.getSource() == All_ST) {

            panel.removeAll();

            panel.add(Checkpanel);

            panel.validate();

            panel.repaint();
        } else if (e.getSource() == ByIdFromSt) {

            panel.removeAll();

            panel.add(Byidpanel);

            panel.validate();

            panel.repaint();
        } else if (e.getSource() == ByIdDelSt) {

            panel.removeAll();

            panel.add(Delpanel);

            panel.validate();

            panel.repaint();
        } else if (e.getSource() == ByIdUpData) {

            V_Updata_Panel.main(null);
        }else if(e.getSource() == Insert_St){

           Insert_Stu.main(null);

        }

    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {

        St_frame = new JFrame();
        St_frame.setTitle("u5B66u751Fu4FE1u606Fu7BA1u7406u7CFBu7EDF");
        St_frame.setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE);
        St_frame.setBounds(100, 100, 738, 514);

        St_frame.setLocationRelativeTo(null);
        St_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        St_frame.getContentPane().setLayout(null);

        panel.setBounds(0, 64, 722, 361);
        Drawpanel.setBounds(0, 0, 722, 361);

        panel.add(Drawpanel);
        panel.setVisible(true);

        panel.setLayout(null);


        St_frame.getContentPane().add(panel);
        //实现右下角的时间显示
        Timer t = new Timer();

        t.schedule(new MyTask(), 1000, 1000);

        JMenuBar menuBar = new JMenuBar();
        menuBar.setBounds(0, 0, 722, 31);
        St_frame.getContentPane().add(menuBar);

        JMenu mnNewMenu = new JMenu("u67E5u8BE2u4FE1u606F");
        mnNewMenu.setHorizontalAlignment(SwingConstants.LEFT);
        mnNewMenu.setFont(new Font("微软雅黑", Font.PLAIN, 18));
        menuBar.add(mnNewMenu);

        All_ST = new JMenuItem(
                "u67E5u770Bu6240u6709u5B66u751Fu4FE1u606F");
        All_ST.setHorizontalAlignment(SwingConstants.LEFT);
        All_ST.addActionListener(this);
        mnNewMenu.add(All_ST);

        ByIdFromSt = new JMenuItem(
                "u6839u636Eu5B66u53F7u67E5u770Bu5B66u751Fu4FE1u606F");

        ByIdFromSt.addActionListener(this);
        mnNewMenu.add(ByIdFromSt);

        JMenu mnNewMenu_1 = new JMenu("u4FEEu6539u4FE1u606F");
        mnNewMenu_1.setFont(new Font("微软雅黑", Font.PLAIN, 18));
        menuBar.add(mnNewMenu_1);

        ByIdDelSt = new JMenuItem(
                "u6839u636Eu5B66u53F7u5220u9664u5B66u751Fu4FE1u606F");
        ByIdDelSt.addActionListener(this);
        mnNewMenu_1.add(ByIdDelSt);

        ByIdUpData = new JMenuItem(
                "u6839u636Eu5B66u53F7u66F4u65B0u5B66u751Fu4FE1u606F");
        ByIdUpData.addActionListener(this);
        mnNewMenu_1.add(ByIdUpData);

        Insert_St = new JMenuItem(
                "u52A0u5165u4E00u6761u6216u591Au6761u5B66u751Fu4FE1u606F");
        Insert_St.addActionListener(this);
        mnNewMenu_1.add(Insert_St);

        JMenu mnNewMenu_2 = new JMenu(
                "u4FEEu6539u7BA1u7406u5458u4FE1u606F");
        mnNewMenu_2.setFont(new Font("微软雅黑", Font.PLAIN, 18));
        menuBar.add(mnNewMenu_2);

        JMenuItem mntmNewMenuItem_5 = new JMenuItem(
                "u4FEEu6539u7BA1u7406u5458u8D44u6599");
        mnNewMenu_2.add(mntmNewMenuItem_5);

        JMenuItem mntmNewMenuItem_6 = new JMenuItem(
                "u4FEEu6539u7BA1u7406u5458u5BC6u7801");
        mnNewMenu_2.add(mntmNewMenuItem_6);

        JMenu exit = new JMenu("u9000u51FAu7CFBu7EDF");
        exit.setFont(new Font("微软雅黑", Font.PLAIN, 18));
         //退出按钮事件
        exit.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub
                System.exit(0);
            }

        });

        menuBar.add(exit);

        JLabel label = new JLabel("u6B22u8FCEu767Bu9646!!!");
        label.setFont(new Font("华文行楷", Font.PLAIN, 22));
        label.setBounds(578, 35, 122, 31);
        St_frame.getContentPane().add(label);

        JLabel label_1 = new JLabel("u5F53u524Du65F6u95F4uFF1A");
        label_1.setForeground(Color.RED);
        label_1.setBounds(490, 435, 75, 15);
        St_frame.getContentPane().add(label_1);

        nowtime = new JTextField();
        nowtime.setForeground(Color.RED);
        nowtime.setEditable(false);
        nowtime.setText("yyyy-MM-dd HH:mm:ss");
        nowtime.setBounds(566, 432, 134, 21);
        St_frame.getContentPane().add(nowtime);
        nowtime.setColumns(10);

    }

    class MyTask extends TimerTask {

        @Override
        public void run() {
            // TODO Auto-generated method stub

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            String s = sdf.format(new Date());

            nowtime.setText(s);

        }
    }

}

代码写的太乱了,最主要的就是这两个类。下面是代码地址:


https://github.com/poc9999/Student-information-management-system

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/weixin_38179324/article/details/77856485
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-04-19 13:40:50
  • 阅读 ( 1033 )
  • 分类:数据库

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢