Java实训(GUI和mysql8.0)——可视化学生管理系统

记录下这学期java实训(懒得保存了,干脆写成文章)
链接:https://pan.baidu.com/s/1Tm2diXuhxDx6C7rCOJ0ARA
提取码:eada

里面有源码工程文件和相关工具
mysql8.0官网下载


主窗口

package 学籍管理系统视窗;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.CardLayout;
import java.awt.Toolkit;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;

import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;
import javax.swing.ImageIcon;
import javax.swing.KeyStroke;
import java.awt.event.KeyEvent;
import java.awt.event.InputEvent;

public class 主窗口 extends JFrame {
    

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    主窗口 frame = new 主窗口();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     * @throws SQLException 
     */
    public 主窗口() throws SQLException {
        //this.setLocationRelativeTo(null);
        setBounds(100, 100, 800, 500);
        this.setLocationRelativeTo(null);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        CardLayout card=new CardLayout();
        contentPane.setLayout(card);
        欢迎面板 欢迎面板=new 欢迎面板();
        contentPane.add("0",欢迎面板);
        修改面板 修改面板=new 修改面板();
        contentPane.add("2",修改面板);
        录入面板 录入面板=new 录入面板();
        contentPane.add("1",录入面板);
        删除面板 删除面板=new 删除面板();
        contentPane.add("3",删除面板);
        查询面板  查询面板=new 查询面板();
        contentPane.add("4",查询面板);
        查看面板  查看面板=new 查看面板();
        contentPane.add("5",查看面板);
        
        
        setTitle("\u5E7F\u5DDE\u822A\u6D77\u5B66\u9662\u5B66\u751F\u7BA1\u7406\u7CFB\u7EDF");
        setIconImage(Toolkit.getDefaultToolkit().getImage("E:\\eclipse64\u4F4D\\workplace\\\u5B66\u7C4D\u7BA1\u7406\u7CFB\u7EDF\u89C6\u7A97\\image\\668eca61e8886291e5af08b33983448e.jpg"));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        
        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);
        
        JMenu menu = new JMenu("\u64CD\u4F5C\u83DC\u5355");
        menu.setIcon(new ImageIcon("E:\\eclipse64\u4F4D\\workplace\\\u5B66\u7C4D\u7BA1\u7406\u7CFB\u7EDF\u89C6\u7A97\\image\\\u622A\u56FE20190624192426.png"));
        
        menuBar.add(menu);
        
        JMenuItem menuItem = new JMenuItem("\u5F55\u5165\u5B66\u751F\u4FE1\u606F");
        menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, InputEvent.ALT_MASK));
        menuItem.setIcon(new ImageIcon("E:\\eclipse64\u4F4D\\workplace\\\u5B66\u7C4D\u7BA1\u7406\u7CFB\u7EDF\u89C6\u7A97\\image\\\u622A\u56FE20190624192512.png"));
        menuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                card.show(contentPane, "1");
            }
        });
        
        menu.add(menuItem);
        
        JMenuItem menuItem_1 = new JMenuItem("\u4FEE\u6539\u5B66\u751F\u4FE1\u606F");
        menuItem_1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, InputEvent.ALT_MASK));
        menuItem_1.setIcon(new ImageIcon("E:\\eclipse64\u4F4D\\workplace\\\u5B66\u7C4D\u7BA1\u7406\u7CFB\u7EDF\u89C6\u7A97\\image\\\u622A\u56FE20190624192548.png"));
        menuItem_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                card.show(contentPane, "2");
            }
        });
        
        menu.add(menuItem_1);
        
        JMenuItem menuItem_2 = new JMenuItem("\u5220\u9664\u5B66\u751F\u4FE1\u606F");
        menuItem_2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E, InputEvent.ALT_MASK));
        menuItem_2.setIcon(new ImageIcon("E:\\eclipse64\u4F4D\\workplace\\\u5B66\u7C4D\u7BA1\u7406\u7CFB\u7EDF\u89C6\u7A97\\image\\\u622A\u56FE20190624192731.png"));
        menuItem_2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                card.show(contentPane, "3");
            }
        });
        menu.add(menuItem_2);
        
        JMenuItem menuItem_3 = new JMenuItem("\u67E5\u8BE2\u5B66\u751F\u4FE1\u606F");
        menuItem_3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, InputEvent.ALT_MASK));
        menuItem_3.setIcon(new ImageIcon("E:\\eclipse64\u4F4D\\workplace\\\u5B66\u7C4D\u7BA1\u7406\u7CFB\u7EDF\u89C6\u7A97\\image\\\u622A\u56FE20190624192757.png"));
        menuItem_3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                card.show(contentPane, "4");
            }
        });
        menu.add(menuItem_3);
        
        JMenuItem menuItem_4 = new JMenuItem("\u663E\u793A\u6240\u6709\u4FE1\u606F");
        menuItem_4.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, InputEvent.ALT_MASK));
        menuItem_4.setIcon(new ImageIcon("E:\\eclipse64\u4F4D\\workplace\\\u5B66\u7C4D\u7BA1\u7406\u7CFB\u7EDF\u89C6\u7A97\\image\\\u622A\u56FE20190624192820.png"));
        menuItem_4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                card.show(contentPane, "5");
            }
        });
        menu.add(menuItem_4);
    
        
    
        
        
    }
}

欢迎面板

package 学籍管理系统视窗;

import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.Font;

public class 欢迎面板 extends JPanel {

    /**
     * Create the panel.
     */
    public 欢迎面板() {
        setBackground(Color.CYAN);
        setLayout(null);
        
        JLabel lblNewLabel = new JLabel("\u6B22\u8FCE\u4F7F\u7528\u5E7F\u5DDE\u822A\u6D77\u5B66\u9662\u5B66\u751F\u7BA1\u7406\u7CFB\u7EDF");
        lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 24));
        lblNewLabel.setBounds(34, 90, 387, 62);
        add(lblNewLabel);
        
        JLabel label = new JLabel("\u8BF7\u4F60\u6309\u5DE6\u4E0A\u89D2\u83DC\u5355\u680F\u63D0\u793A\u64CD\u4F5C");
        label.setBounds(100, 178, 233, 18);
        add(label);
        

    }
}

录入面板

package 学籍管理系统视窗;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.SQLException;

import javax.swing.ButtonGroup;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;


import 数据库.Test;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class 录入面板 extends JPanel {
    private JTextField textField;
    private JTextField textField_1;
    private JTextField textField_2;
    
    private JLabel lblNewLabel;


    /**
     * Create the panel.
     */
    public 录入面板() {
        setLayout(null);
         lblNewLabel = new JLabel("\u5B66\u53F7");
        lblNewLabel.setBounds(72, 55, 30, 18);
        add(lblNewLabel);
        
        JLabel label = new JLabel("\u59D3\u540D");
        label.setBounds(72, 24, 30, 18);
        add(label);
        
        textField = new JTextField("请输入学号");
        textField.setBounds(116, 52, 130, 24);
        textField.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                textField.setText("");
            }
        });
        add(textField);
        textField.setColumns(10);
        
        textField_1 = new JTextField("请输入姓名");
        textField_1.setBounds(116, 21, 130, 24);
        textField_1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                textField_1.setText("");
            }
        });
        add(textField_1);
        textField_1.setColumns(10);
        
        JLabel label_1 = new JLabel("\u6027\u522B");
        label_1.setBounds(72, 89, 30, 18);
    add(label_1);
        
        
        JRadioButton radioButton = new JRadioButton("\u7537");
        radioButton.setBounds(126, 85, 43, 27);
        add(radioButton);
        
        JRadioButton radioButton_1 = new JRadioButton("\u5973");
        radioButton_1.setBounds(175, 85, 55, 27);
        add(radioButton_1);
        
        //创建ButtonGroup对象
                ButtonGroup btgSex = new ButtonGroup();
                btgSex.add(radioButton);
                btgSex.add(radioButton_1);
        
        JLabel label_2 = new JLabel("\u5B66\u9662");
        label_2.setBounds(72, 120, 30, 18);
        add(label_2);
        
        ComboBoxModel aModel1=new  DefaultComboBoxModel(new String[]{"航海技术","航海技术1","航海技术2"});
         ComboBoxModel aModel2=new  DefaultComboBoxModel(new String[]{"轮机工程学","轮机工程学1","轮机工程学2"});
         ComboBoxModel aModel3=new  DefaultComboBoxModel(new String[]{"计算机科学与技术","电子商务(本科)","软件技术","计算机应用技术","电子商务"});
         ComboBoxModel aModel4=new  DefaultComboBoxModel(new String[]{"英语","日语","外语学"});
            JComboBox comboBox_1 = new JComboBox();
            comboBox_1.setBounds(110, 151, 168, 24);
            comboBox_1.setToolTipText("");
            add(comboBox_1);
            
            
         JComboBox comboBox = new JComboBox();
         comboBox.setBounds(110, 117, 164, 24);
        comboBox.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                if(e.getStateChange()==ItemEvent.SELECTED)
                {
                 int a=comboBox.getSelectedIndex();
                 if(a==0)
                 {
                     comboBox_1.setModel(aModel1);
                     comboBox_1.setSelectedIndex(-1);
                 
                 }
                 else if(a==1)
                 {
                     comboBox_1.setModel(aModel2);
                     comboBox_1.setSelectedIndex(-1);
                  
                 }
                 else if(a==2)
                 {
                     comboBox_1.setModel(aModel3);
                     comboBox_1.setSelectedIndex(-1);
                 }
                 else if(a==3)
                 {
                     comboBox_1.setModel(aModel4);
                     comboBox_1.setSelectedIndex(-1);
                 }
                 
                }
            }
        });
        comboBox.setModel(new DefaultComboBoxModel(new String[] {"海运学院", "轮机工程学院", "信息与通信工程学院", "外语学院"}));
        comboBox.setSelectedIndex(-1);
        add(comboBox);
        
        JButton button = new JButton("\u5F55\u5165");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                
                String sex="";
                if(radioButton.isSelected()) {
                    sex=radioButton.getText();
                }else {
                    sex=radioButton_1.getText();
                }
            
                if(textField_1.getText().equals("")||textField_1.getText().equals("请输入姓名")) {
                    JOptionPane.showMessageDialog(null,"请输入姓名","错误",JOptionPane.ERROR_MESSAGE);
                    return;
                }
                
                if(textField.getText().equals("")||textField.getText().equals("请输入学号")) {
                    JOptionPane.showMessageDialog(null,"请输入学号","错误",JOptionPane.ERROR_MESSAGE);
                    return;
                }
                
                if(comboBox.getSelectedIndex()==-1) {
                    JOptionPane.showMessageDialog(null,"请选择学院","错误",JOptionPane.ERROR_MESSAGE);
                    return;
                }
                if(comboBox_1.getSelectedIndex()==-1) {
                    JOptionPane.showMessageDialog(null,"请选择专业","错误",JOptionPane.ERROR_MESSAGE);
                    return;
                }
                
                if(textField_2.getText().equals("")) {
                    JOptionPane.showMessageDialog(null,"请输入班级","错误",JOptionPane.ERROR_MESSAGE);
                    return;
                }
                
                
                //保存数据
                String sno=textField.getText();
                String name=textField_1.getText();
                String ssex;
                if(radioButton.isSelected()) {
                     ssex="男";
                }else
                     ssex="女";
                
                String department=comboBox.getSelectedItem().toString();
                String major=comboBox_1.getSelectedItem().toString();
                String myclass=textField_2.getText();
                //调用自己链接数据库操作方法test
                Connection sin=Test.getconnectin();
                boolean flag;
                flag=Test.chongfu(sin,sno);
                System.out.println(flag);
                if(flag==false) {
                Object str[]={sno,name,ssex,department,major,myclass};
                
                Test.insert(sin, str);
                JOptionPane.showMessageDialog(null,"录入成功","提示",JOptionPane.INFORMATION_MESSAGE);
                radioButton.setSelected(false);
                radioButton_1.setSelected(false);
                textField.setText("请输入学号");
                textField_1.setText("请输入姓名");
                textField_2.setText("");
                comboBox.setSelectedIndex(-1);
                comboBox_1.setSelectedIndex(-1);
                }
                else
                    JOptionPane.showMessageDialog(null,"失败,学号重复","提示",JOptionPane.INFORMATION_MESSAGE);
                
                
            
            }
        });
        button.setBounds(203, 232, 63, 27);
        add(button);
        
        JButton button_1 = new JButton("\u91CD\u7F6E");
        button_1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                radioButton.setSelected(false);
                radioButton_1.setSelected(false);
                textField.setText("请输入学号");
                textField_1.setText("请输入姓名");
                textField_2.setText("");
                comboBox.setSelectedIndex(-1);
                comboBox_1.setSelectedIndex(-1);
                JOptionPane.showMessageDialog(null,"重置成功","提示",JOptionPane.INFORMATION_MESSAGE);
            
            }
        });
        button_1.setBounds(106, 232, 63, 27);
        add(button_1);
        
        JLabel label_3 = new JLabel("\u4E13\u4E1A");
        label_3.setBounds(72, 154, 30, 18);
        add(label_3);
        
        JLabel label_4 = new JLabel("\u73ED\u7EA7");
        label_4.setBounds(72, 191, 30, 18);
        add(label_4);
        
        textField_2 = new JTextField();
        textField_2.setBounds(110, 188, 164, 24);
        add(textField_2);
        textField_2.setColumns(10);
        
    }

    }
        


修改面板

package 学籍管理系统视窗;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JTextField;


import 数据库.Test;

import javax.swing.ButtonGroup;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JRadioButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class 修改面板 extends JPanel {
    
    private JTextField textField;
    private JTextField textField_1;
    private JTextField textField_2;

    /**
     * Create the panel.
     */
    public 修改面板() {
        setLayout(null);
        
        JLabel label = new JLabel("\u8F93\u5165\u8981\u4FEE\u6539\u4FE1\u606F\u7684\u5B66\u53F7\uFF1A");
        label.setFont(new Font("宋体", Font.PLAIN, 16));
        label.setBounds(42, 18, 177, 18);
        add(label);
        
        textField = new JTextField();
        textField.setBounds(213, 15, 144, 24);
        add(textField);
        textField.setColumns(10);
        
        
        
        
        JLabel label_1 = new JLabel("\uFF08\u65B0\uFF09\u59D3\u540D\uFF1A");
        label_1.setBounds(42, 76, 120, 18);
        add(label_1);
        
        textField_1 = new JTextField();
        textField_1.setBounds(133, 73, 120, 24);
        add(textField_1);
        textField_1.setColumns(10);
        
        JRadioButton radioButton = new JRadioButton("\u7537");
        radioButton.setBounds(107, 119, 51, 27);
        add(radioButton);
        
        JRadioButton radioButton_1 = new JRadioButton("\u5973");
        radioButton_1.setBounds(168, 119, 51, 27);
        add(radioButton_1);
        ButtonGroup btgSex = new ButtonGroup();
        btgSex.add(radioButton);
        btgSex.add(radioButton_1);
        
        
        ComboBoxModel aModel1=new  DefaultComboBoxModel(new String[]{"航海技术","航海技术1","航海技术2"});
         ComboBoxModel aModel2=new  DefaultComboBoxModel(new String[]{"轮机工程学","轮机工程学1","轮机工程学2"});
         ComboBoxModel aModel3=new  DefaultComboBoxModel(new String[]{"计算机科学与技术","电子商务(本科)","软件技术","计算机应用技术","电子商务"});
         ComboBoxModel aModel4=new  DefaultComboBoxModel(new String[]{"英语","日语","外语学"});
            JComboBox comboBox_1 = new JComboBox();
            comboBox_1.setToolTipText("");
            comboBox_1.setBounds(133, 207, 185, 24);
            this.add(comboBox_1);
            
            
         JComboBox comboBox = new JComboBox();
        comboBox.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                if(e.getStateChange()==ItemEvent.SELECTED)
                {
                 int a=comboBox.getSelectedIndex();
                 if(a==0)
                 {
                     comboBox_1.setModel(aModel1);
                     comboBox_1.setSelectedIndex(-1);
                 
                 }
                 else if(a==1)
                 {
                     comboBox_1.setModel(aModel2);
                     comboBox_1.setSelectedIndex(-1);
                  
                 }
                 else if(a==2)
                 {
                     comboBox_1.setModel(aModel3);
                     comboBox_1.setSelectedIndex(-1);
                 }
                 else if(a==3)
                 {
                     comboBox_1.setModel(aModel4);
                     comboBox_1.setSelectedIndex(-1);
                 }
                 
                }
            }
        });
        comboBox.setModel(new DefaultComboBoxModel(new String[] {"\u6D77\u8FD0\u5B66\u9662", "\u8F6E\u673A\u5DE5\u7A0B\u5B66\u9662", "\u4FE1\u606F\u4E0E\u901A\u4FE1\u5DE5\u7A0B\u5B66\u9662", "\u5916\u8BED\u5B66\u9662"}));
        comboBox.setSelectedIndex(-1);
        comboBox.setBounds(133, 167, 182, 24);
        this.add(comboBox);
        
        JLabel lblNewLabel = new JLabel("\u65B0\u5B66\u9662\uFF1A");
        lblNewLabel.setBounds(72, 169, 71, 18);
        add(lblNewLabel);
        
        JLabel label_2 = new JLabel("\u65B0\u4E13\u4E1A\uFF1A");
        label_2.setBounds(70, 210, 72, 18);
        add(label_2);
        
        JLabel label_3 = new JLabel("\u65B0 \u73ED\u7EA7");
        label_3.setBounds(72, 247, 72, 18);
        add(label_3);
        
        textField_2 = new JTextField();
        textField_2.setBounds(133, 244, 185, 24);
        add(textField_2);
        textField_2.setColumns(10);
        
        JButton button = new JButton("\u4FEE\u6539");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                
                
                
                String sno=textField.getText();
                String name=textField_1.getText();
                String ssex;
                if(radioButton.isSelected()) {
                     ssex="男";
                }else
                     ssex="女";
                
                String department=comboBox.getSelectedItem().toString();
                String major=comboBox_1.getSelectedItem().toString();
                String myclass=textField_2.getText();
                //调用自己链接数据库操作方法test
                Object str[]={sno,name,ssex,department,major,myclass};
                Connection sin=Test.getconnectin();
                Test.update(sin, str);
                radioButton.setSelected(false);
                radioButton_1.setSelected(false);
                textField.setText("");
                textField_1.setText("");
                textField_2.setText("");
                comboBox.setSelectedIndex(-1);
                comboBox_1.setSelectedIndex(-1);
                JOptionPane.showMessageDialog(null,"修改成功","提示",JOptionPane.NO_OPTION );
                
                
            }
        });
        button.setBounds(82, 282, 114, 27);
        add(button);
        
        JButton button_1 = new JButton("\u91CD\u7F6E");
        button_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                radioButton.setSelected(false);
                radioButton_1.setSelected(false);
                textField.setText("");
                textField_1.setText("");
                textField_2.setText("");
                comboBox.setSelectedIndex(-1);
                comboBox_1.setSelectedIndex(-1);
                JOptionPane.showMessageDialog(null,"重置成功!","信息",JOptionPane.INFORMATION_MESSAGE );
            }
        });
        button_1.setBounds(204, 282, 114, 27);
        add(button_1);
        
        JButton btnNewButton = new JButton("\u67E5\u8BE2");
        btnNewButton.setToolTipText("");
        btnNewButton.setBounds(364, 15, 79, 27);
        add(btnNewButton);
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                
                 Connection sin=Test.getconnectin();
                    String sno=textField.getText();
                    ResultSet rs=null;
                    PreparedStatement stmt=null;
                    boolean flag=false;
                    try {
                        //牛逼查找
                         stmt=sin.prepareStatement("select * from student where sno=?");
                        stmt.setObject(1,sno);
                        rs=stmt.executeQuery();
                        while(rs.next()){
                        String msg=rs.getString("sno")+"\n"+rs.getString("name")+"\n"+rs.getString("sex")+"\n"+rs.getString("department")+"\n"+rs.getString("subject")+"\n"+rs.getString("myclass")+"\n";
                        //JOptionPane.showMessageDialog(null,msg,"查询结果:",JOptionPane.NO_OPTION );
                        flag=true;
                        //修改
                        textField_1.setText(rs.getString("name"));
                        String ssex=null;
                        ssex=rs.getString("sex");
                        if(ssex.equalsIgnoreCase("男")) {
                            radioButton.setSelected(true);

                        }else {
                            radioButton_1.setSelected(true);
                        }
                        textField_2.setText(rs.getString("myclass"));
                        comboBox.setSelectedItem(rs.getString("department"));
                        comboBox_1.setSelectedItem(rs.getString("subject"));
                        
                        }
                        if(flag==false) {
                            radioButton.setSelected(false);
                            radioButton_1.setSelected(false);
                            
                            textField_1.setText("");
                            textField_2.setText("");
                            comboBox.setSelectedIndex(-1);
                            comboBox_1.setSelectedIndex(-1);
                            JOptionPane.showMessageDialog(null,"查无此人","查询结果",JOptionPane.NO_OPTION );
                            
                            
                        }
                            stmt.close();//关闭数据库
                            sin.close();
                        } catch (SQLException h) {
                            // TODO Auto-generated catch block
                            h.printStackTrace();
                        }
                    }
            
        });

    }
}

查询面板

package 学籍管理系统视窗;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;


import 数据库.Test;

import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.awt.event.ActionEvent;

public class 查询面板 extends JPanel {
    private JTextField textField;
    private JTextField textField_1;
    private JTextField textField_2;
    private JTextField textField_3;
    private JTextField textField_4;
    private JTextField textField_5;

    /**
     * Create the panel.
     */
    public 查询面板() {
        setLayout(null);
        
        JLabel label = new JLabel("\u8F93\u5165\u8981\u67E5\u8BE2\u4FE1\u606F\u7684\u5B66\u53F7\uFF1A");
        label.setBounds(37, 22, 189, 18);
        add(label);
        
        textField = new JTextField();
        textField.setBounds(201, 19, 154, 24);
        add(textField);
        textField.setColumns(10);
        
        JLabel label_1 = new JLabel("\u59D3\u540D");
        label_1.setBounds(72, 60, 40, 18);
        add(label_1);
        
        textField_1 = new JTextField();
        textField_1.setEnabled(false);
        textField_1.setBounds(112, 60, 150, 24);
        add(textField_1);
        textField_1.setColumns(10);
        
        JLabel label_2 = new JLabel("\u6027\u522B");
        label_2.setBounds(72, 100, 52, 18);
        add(label_2);
        
        textField_2 = new JTextField();
        textField_2.setEnabled(false);
        textField_2.setBounds(112, 100, 150, 24);
        add(textField_2);
        textField_2.setColumns(10);
        
        JLabel label_3 = new JLabel("\u5B66\u9662");
        label_3.setBounds(72, 140, 41, 18);
        add(label_3);
        
        textField_3 = new JTextField();
        textField_3.setEnabled(false);
        textField_3.setBounds(112, 140, 150, 24);
        add(textField_3);
        textField_3.setColumns(10);
        
        JLabel label_4 = new JLabel("\u4E13\u4E1A");
        label_4.setBounds(72, 180, 45, 18);
        add(label_4);
        
        textField_4 = new JTextField();
        textField_4.setEnabled(false);
        textField_4.setBounds(112, 180, 150, 24);
        add(textField_4);
        textField_4.setColumns(10);
        
        JLabel label_5 = new JLabel("\u73ED\u7EA7");
        label_5.setBounds(72, 220, 45, 18);
        add(label_5);
        
        textField_5 = new JTextField();
        textField_5.setEnabled(false);
        textField_5.setBounds(112, 220, 150, 24);
        add(textField_5);
        textField_5.setColumns(10);
        
        JButton btnNewButton = new JButton("\u67E5\u8BE2");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                
                 Connection sin=Test.getconnectin();
                    String sno=textField.getText();
                    ResultSet rs=null;
                    PreparedStatement stmt=null;
                    try {
                        //牛逼查找
                         stmt=sin.prepareStatement("select * from student where sno=?");
                        stmt.setObject(1,sno);
                        rs=stmt.executeQuery();
                        boolean flag=false;
                        while(rs.next()){
                        String msg=rs.getString("sno")+"\n"+rs.getString("name")+"\n"+rs.getString("sex")+"\n"+rs.getString("department")+"\n"+rs.getString("subject")+"\n"+rs.getString("myclass")+"\n";
                        JOptionPane.showMessageDialog(null,msg,"查询结果:",JOptionPane.NO_OPTION );
                        textField_1.setText(rs.getString("name"));
                        textField_2.setText(rs.getString("sex"));
                        textField_3.setText(rs.getString("department"));
                        textField_4.setText(rs.getString("subject"));
                        textField_5.setText(rs.getString("myclass"));
                        flag=true;
                        }
                        if(flag==false) {
                            JOptionPane.showMessageDialog(null,"查无此人","查询结果",JOptionPane.NO_OPTION );
                        }
                        
                            stmt.close();//关闭数据库
                            sin.close();
                        } catch (SQLException h) {
                            // TODO Auto-generated catch block
                            h.printStackTrace();
                        }
                    }
                
                    
            
               
             
           

            
        });
        btnNewButton.setBounds(366, 17, 77, 27);
        add(btnNewButton);

    }

}

删除面板

package 学籍管理系统视窗;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;


import 数据库.Test;


public class 删除面板 extends JPanel {
    private JTextField textField;
    private JTextField textField_1;
    private JTextField textField_2;
    private JTextField textField_3;
    private JTextField textField_4;
    private JTextField textField_5;


    /**
     * Create the panel.
     */
    public 删除面板() {
setLayout(null);
        
        JLabel label = new JLabel("\u8F93\u5165\u8981\u5220\u9664\u4FE1\u606F\u7684\u5B66\u53F7\uFF1A");
        label.setBounds(37, 22, 189, 18);
        add(label);
        
        textField = new JTextField();
        textField.setBounds(201, 19, 154, 24);
        add(textField);
        textField.setColumns(10);
        
        JLabel label_1 = new JLabel("\u59D3\u540D");
        label_1.setBounds(72, 60, 40, 18);
        add(label_1);
        
        textField_1 = new JTextField();
        textField_1.setEnabled(false);
        textField_1.setBounds(112, 60, 150, 24);
        add(textField_1);
        textField_1.setColumns(10);
        
        JLabel label_2 = new JLabel("\u6027\u522B");
        label_2.setBounds(72, 100, 52, 18);
        add(label_2);
        
        textField_2 = new JTextField();
        textField_2.setEnabled(false);
        textField_2.setBounds(112, 100, 150, 24);
        add(textField_2);
        textField_2.setColumns(10);
        
        JLabel label_3 = new JLabel("\u5B66\u9662");
        label_3.setBounds(72, 140, 41, 18);
        add(label_3);
        
        textField_3 = new JTextField();
        textField_3.setEnabled(false);
        textField_3.setBounds(112, 140, 150, 24);
        add(textField_3);
        textField_3.setColumns(10);
        
        JLabel label_4 = new JLabel("\u4E13\u4E1A");
        label_4.setBounds(72, 180, 45, 18);
        add(label_4);
        
        textField_4 = new JTextField();
        textField_4.setEnabled(false);
        textField_4.setBounds(112, 180, 150, 24);
        add(textField_4);
        textField_4.setColumns(10);
        
        JLabel label_5 = new JLabel("\u73ED\u7EA7");
        label_5.setBounds(72, 220, 45, 18);
        add(label_5);
        
        textField_5 = new JTextField();
        textField_5.setEnabled(false);
        textField_5.setBounds(112, 220, 150, 24);
        add(textField_5);
        textField_5.setColumns(10);
        
        JButton btnNewButton = new JButton("\u67E5\u8BE2");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                
                 Connection sin=Test.getconnectin();
                    String sno=textField.getText();
                    ResultSet rs=null;
                    PreparedStatement stmt=null;
                    try {
                        //牛逼查找
                         stmt=sin.prepareStatement("select * from student where sno=?");
                        stmt.setObject(1,sno);
                        rs=stmt.executeQuery();
                        boolean flag=false;
                        while(rs.next()){
                            JOptionPane.showMessageDialog(null,"查询成功","提示",JOptionPane.NO_OPTION );
                        textField_1.setText(rs.getString("name"));
                        textField_2.setText(rs.getString("sex"));
                        textField_3.setText(rs.getString("department"));
                        textField_4.setText(rs.getString("subject"));
                        textField_5.setText(rs.getString("myclass"));
                        flag=true;
                        }
                        if(flag==false) {
                            JOptionPane.showMessageDialog(null,"查无此人","查询结果",JOptionPane.NO_OPTION );
                        }
                            stmt.close();//关闭数据库
                            sin.close();
                        } catch (SQLException h) {
                            // TODO Auto-generated catch block
                            h.printStackTrace();
                        }
                    }
                
                    
            
                
                
                
            
        });
        btnNewButton.setBounds(366, 17, 77, 27);
        add(btnNewButton);
        
        JButton button = new JButton("\u5220\u9664");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                
                //删除操作
                String sno=textField.getText();
                boolean flag;
                Connection sin=Test.getconnectin();
                flag=Test.chongfu(sin,sno);
                System.out.println(flag);
                if(flag) {
                
                Test.delete(sin, sno);
                JOptionPane.showMessageDialog(null,"信息删除成功!","信息",JOptionPane.INFORMATION_MESSAGE );
                textField.setText("");
                textField_1.setText("");
                textField_2.setText("");
                textField_3.setText("");
                textField_4.setText("");
                textField_5.setText("");
            }
                else
                    JOptionPane.showMessageDialog(null,"该学号信息不存在!","信息",JOptionPane.INFORMATION_MESSAGE );
            }
        });
        button.setBounds(72, 257, 113, 27);
        add(button);
        
        JButton button_1 = new JButton("\u91CD\u7F6E");
        button_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                
                textField.setText("");
                textField_1.setText("");
                textField_2.setText("");
                textField_3.setText("");
                textField_4.setText("");
                textField_5.setText("");
                JOptionPane.showMessageDialog(null,"重置成功","提示",JOptionPane.NO_OPTION );
                
            }
        });
        button_1.setBounds(217, 257, 113, 27);
        add(button_1);


    }

}

数据库类

package 数据库;

import java.sql.*;

import javax.swing.JOptionPane;
public class Test {
public final static Connection getconnectin(){
     
    Connection con=null;
    //String driver = "com.mysql.jdbc.Driver"; 8.0不需要加载驱动
    String url = "jdbc:mysql://localhost:3306/gip?useUnicode=true&characterEncoding=UTF8&useSSL=false&serverTimezone=Asia/Shanghai";
    String user = "root";
    String password = "123456";
    try{
        //Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    //  Class.forName(driver);
        con=DriverManager.getConnection(url,user,password);
        con.setCatalog("gip");
        
    }
    catch(SQLException sqle){
           System.out.println(sqle+"lianjiecuowu");
                }
            catch(Exception e){
                System.out.println(e+"diergecuowu");
            
            }
    return con;
}
//添加数据
public final static void insert(Connection con,Object obj[]){
    PreparedStatement stmt=null;
    
    try {
        
        
    stmt=con.prepareStatement("insert into student(sno,name,sex,department,subject,myclass) values(?,?,?,?,?,?)");
        
        stmt.setString(1,obj[0].toString());
        stmt.setString(2,obj[1].toString());
        stmt.setString(3,obj[2].toString());
        stmt.setString(4,obj[3].toString());
        stmt.setString(5,obj[4].toString());
        stmt.setString(6,obj[5].toString());
        stmt.execute();
        
        
        
    } catch (SQLException e) {
        JOptionPane.showMessageDialog(null,"shibai","操作提示",JOptionPane.NO_OPTION );
    }
}
//查询数据
public final static ResultSet query(Connection con){
    
    ResultSet rs=null;
    PreparedStatement stmt=null;
    try {
        stmt=con.prepareStatement("select * from student");
         rs=stmt.executeQuery();
        //stmt.close();
        //con.close();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return rs;
}
//判断学号重复
public final static boolean chongfu(Connection con,String a){
    
    ResultSet rs=null;
    PreparedStatement stmt=null;
    boolean flag=true;;
    try {
        stmt=con.prepareStatement("select sno from student");
         rs=stmt.executeQuery();
         while(rs.next())
         {
             if(a.equalsIgnoreCase(rs.getString("sno")))
            
                 return true;
            
             //System.out.println(rs.getString("sno"));
         }
        //stmt.close();
        //con.close();
         
         
         
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return false;
}

//删除数据
public final static void delete(Connection con,String s){
    try {
        PreparedStatement stmt;
         stmt=con.prepareStatement("delete from student where sno=?");
        stmt.setObject(1,s);
        stmt.execute();
        
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    finally{
        
    }
}

//更新修改数据
public final static void update(Connection con,Object obj[]){
    PreparedStatement stmt=null;
    try {
        stmt=con.prepareStatement("update student set name=?,sex=?,department=?,subject=?,myclass=? where sno=?");
        
        stmt.setString(6,obj[0].toString());
        stmt.setString(1,obj[1].toString());
        stmt.setString(2,obj[2].toString());
        stmt.setString(3,obj[3].toString());
        stmt.setString(4,obj[4].toString());
        stmt.setString(5,obj[5].toString());
        stmt.execute();
        
        
        
    } catch (SQLException e) {
        JOptionPane.showMessageDialog(null,"shibai","操作提示",JOptionPane.NO_OPTION );
    }
}


}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,864评论 6 494
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,175评论 3 387
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 159,401评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,170评论 1 286
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,276评论 6 385
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,364评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,401评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,179评论 0 269
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,604评论 1 306
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,902评论 2 328
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,070评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,751评论 4 337
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,380评论 3 319
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,077评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,312评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,924评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,957评论 2 351

推荐阅读更多精彩内容

  • 这篇文章是为了介绍自己自学用过的Java视频资料。全部来自传智播客旗下的黑马程序员。也感谢传智播客放出这么多优质的...
    bravo1988阅读 2,993评论 1 5
  • 本文首发于微信公众号「刘望舒」 相关系列文章 ...
    刘望舒阅读 3,280评论 0 8
  • 这个月就要离开北京了。 感慨万千,貌似自从18岁以后的每一个日日夜夜,人生总是在不断迁徙和漂泊中度过的。从南方三四...
    Badheater阅读 206评论 0 0
  • 生活时时刻刻让我们不知所措。我很迷茫,我真的不知道我要停在何处。 尤其假期,我该去往何方。心理纠结的不行。唉,我的...
    莫111阅读 79评论 0 0
  • 基础命令 一:文件 1.创建文件 2.删除文件 rm + 文件名 有提示的删除 rm -rf 文件名 无提示的删...
    Nolie_6f62阅读 124评论 0 1