1. 程式人生 > >基於JavaSwing+MySql的學生資訊管理系統

基於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);

        }
    }

}

程式碼寫的太亂了,最主要的就是這兩個類。下面是程式碼地址: