基於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("\u5B66\u751F\u4FE1\u606F\u7BA1\u7406\u7CFB\u7EDF");
        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("\u67E5\u8BE2\u4FE1\u606F");
        mnNewMenu.setHorizontalAlignment(SwingConstants.LEFT);
        mnNewMenu.setFont(new Font("微軟雅黑", Font.PLAIN, 18));
        menuBar.add(mnNewMenu);

        All_ST = new JMenuItem(
                "\u67E5\u770B\u6240\u6709\u5B66\u751F\u4FE1\u606F");
        All_ST.setHorizontalAlignment(SwingConstants.LEFT);
        All_ST.addActionListener(this);
        mnNewMenu.add(All_ST);

        ByIdFromSt = new JMenuItem(
                "\u6839\u636E\u5B66\u53F7\u67E5\u770B\u5B66\u751F\u4FE1\u606F");

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

        JMenu mnNewMenu_1 = new JMenu("\u4FEE\u6539\u4FE1\u606F");
        mnNewMenu_1.setFont(new Font("微軟雅黑", Font.PLAIN, 18));
        menuBar.add(mnNewMenu_1);

        ByIdDelSt = new JMenuItem(
                "\u6839\u636E\u5B66\u53F7\u5220\u9664\u5B66\u751F\u4FE1\u606F");
        ByIdDelSt.addActionListener(this);
        mnNewMenu_1.add(ByIdDelSt);

        ByIdUpData = new JMenuItem(
                "\u6839\u636E\u5B66\u53F7\u66F4\u65B0\u5B66\u751F\u4FE1\u606F");
        ByIdUpData.addActionListener(this);
        mnNewMenu_1.add(ByIdUpData);

        Insert_St = new JMenuItem(
                "\u52A0\u5165\u4E00\u6761\u6216\u591A\u6761\u5B66\u751F\u4FE1\u606F");
        Insert_St.addActionListener(this);
        mnNewMenu_1.add(Insert_St);

        JMenu mnNewMenu_2 = new JMenu(
                "\u4FEE\u6539\u7BA1\u7406\u5458\u4FE1\u606F");
        mnNewMenu_2.setFont(new Font("微軟雅黑", Font.PLAIN, 18));
        menuBar.add(mnNewMenu_2);

        JMenuItem mntmNewMenuItem_5 = new JMenuItem(
                "\u4FEE\u6539\u7BA1\u7406\u5458\u8D44\u6599");
        mnNewMenu_2.add(mntmNewMenuItem_5);

        JMenuItem mntmNewMenuItem_6 = new JMenuItem(
                "\u4FEE\u6539\u7BA1\u7406\u5458\u5BC6\u7801");
        mnNewMenu_2.add(mntmNewMenuItem_6);

        JMenu exit = new JMenu("\u9000\u51FA\u7CFB\u7EDF");
        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("\u6B22\u8FCE\u767B\u9646!!!");
        label.setFont(new Font("華文行楷", Font.PLAIN, 22));
        label.setBounds(578, 35, 122, 31);
        St_frame.getContentPane().add(label);

        JLabel label_1 = new JLabel("\u5F53\u524D\u65F6\u95F4\uFF1A");
        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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章