JAVA+MYSQL可视化学生信息管理系统

  • Post author:
  • Post category:java


掌握数据库连接Java程序可视化界面的流程,有效编写Java可视化界面程序及其功能(增删改查),了解数据库内容编写和连接的流程。

定义变量:Sno(String),Sname(String),_Class(String),Age(int),Sex(String)

public class HomeWork {
    private static PreparedStatement stmt=null;
    private static ResultSet rs= null;
    private static Connection conn = null;
    private static String Sno,Sname,_Class,Sex,sql;
    private static int Age;
    private static void createAndShowGUI() {
        JFrame f=new JFrame("菜单");
        f.setLayout(new GridLayout(6,1));
        f.setSize(400,300);
        f.setLocation(300,200);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 Button btn1=new Button("添加学生信息");
        Button btn2=new Button("修改学生信息");
        Button btn3=new Button("删除学生信息");
        Button btn4=new Button("按性别查找学生信息");
        Button btn5=new Button("按年龄查找学生信息");
        Button btn6=new Button("按班级查找学生信息");
        f.add(btn1);f.add(btn2);f.add(btn3);
        f.add(btn4);f.add(btn5);f.add(btn6);
        JPanel panel1=new JPanel(new GridLayout(6,2));
        JLabel lab11=new JLabel("请输入学号:");
        JLabel lab12=new JLabel("请输入姓名:");
        JLabel lab13=new JLabel("请输入班级:");
        JLabel lab14=new JLabel("请输入年龄:");
        JLabel lab15=new JLabel("请输入性别:");
        JTextField input11=new JTextField(10);
        JTextField input12=new JTextField(10);
        JTextField input13=new JTextField(10);
        JTextField input14=new JTextField(10);
        JTextField input15=new JTextField(10);
        Button btn11=new Button("提交");
        panel1.add(lab11);    panel1.add(input11);
        panel1.add(lab12);    panel1.add(input12);
        panel1.add(lab13);    panel1.add(input13);
        panel1.add(lab14);    panel1.add(input14);
        panel1.add(lab15);    panel1.add(input15);
        panel1.add(btn11);
        JPanel panel2=new JPanel(new GridLayout(6,2));
        JLabel lab21=new JLabel("请输入学号:");
        JLabel lab22=new JLabel("请输入姓名:");
        JLabel lab23=new JLabel("请输入班级:");
        JLabel lab24=new JLabel("请输入年龄:");
        JLabel lab25=new JLabel("请输入性别:");
        JTextField input21=new JTextField(10);
        JTextField input22=new JTextField(10);
        JTextField input23=new JTextField(10);
        JTextField input24=new JTextField(10);
        JTextField input25=new JTextField(10);
        Button btn21=new Button("提交");
        panel2.add(lab21);    panel2.add(input21);
        panel2.add(lab22);    panel2.add(input22);
        panel2.add(lab23);    panel2.add(input23);
        panel2.add(lab24);    panel2.add(input24);
        panel2.add(lab25);    panel2.add(input25);
        panel2.add(btn21);
        JPanel panel3=new JPanel(new GridLayout(2,2));
        JLabel lab31=new JLabel("请输入学号:");
        JTextField input31=new JTextField(10);
        Button btn31=new Button("提交");
        panel3.add(lab31);    panel3.add(input31);panel3.add(btn31);
        JPanel panel4=new JPanel(new GridLayout(2,2));
        JLabel lab41=new JLabel("请输入性别:");
        JTextField input41=new JTextField(10);
        Button btn41=new Button("提交");
        panel4.add(lab41);    panel4.add(input41);panel4.add(btn41);
        JPanel panel5=new JPanel(new GridLayout(3,2));
        JLabel lab51=new JLabel("请输入年龄上限:");
        JLabel lab52=new JLabel("请输入年龄下限:");
        JTextField input51=new JTextField(10);
        JTextField input52=new JTextField(10);
        Button btn51=new Button("提交");
        panel5.add(lab51);    panel5.add(input51);
        panel5.add(lab52);    panel5.add(input52);
        panel5.add(btn51);
        JPanel panel6=new JPanel(new GridLayout(2,2));
        JLabel lab61=new JLabel("请输入班级:");
        JTextField input61=new JTextField(10);
        Button btn61=new Button("提交");
        panel6.add(lab61);    panel6.add(input61);panel6.add(btn61);
        //为f窗体的6个按钮创建窗体,并将面板添加到窗体中
        JFrame f1=new JFrame("添加学生信息");
        f1.setLayout(new FlowLayout());
        f1.setSize(300,200);
        f1.setLocation(300,200);
        f1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); //只关闭子窗口
        JFrame f2=new JFrame("修改学生信息");
        f2.setLayout(new FlowLayout());
        f2.setSize(300,200);
        f2.setLocation(300,200);
        f2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        JFrame f3=new JFrame("删除学生信息");
        f3.setLayout(new FlowLayout());
        f3.setSize(300,200);
        f3.setLocation(300,200);
        f3.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        JFrame f4=new JFrame("按性别查找学生信息");
        f4.setLayout(new FlowLayout());
        f4.setSize(300,200);
        f4.setLocation(300,200);
        f4.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        JFrame f5=new JFrame("按年龄查找学生信息");
        f5.setLayout(new FlowLayout());
        f5.setSize(300,200);
        f5.setLocation(300,200);
        f5.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        JFrame f6=new JFrame("按班级查找学生信息");
        f6.setLayout(new FlowLayout());
        f6.setSize(300,200);
        f6.setLocation(300,200);
        f6.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        f1.add(panel1); f2.add(panel2); f3.add(panel3);
        f4.add(panel4); f5.add(panel5); f6.add(panel6);
        JTextArea showArea1=new JTextArea(30,35);
        JTextArea showArea2=new JTextArea(30,35);
        JTextArea showArea3=new JTextArea(30,35);
        JScrollPane scrollPane1=new JScrollPane(showArea1);
        JScrollPane scrollPane2=new JScrollPane(showArea2);
        JScrollPane scrollPane3=new JScrollPane(showArea3);
        showArea1.setEditable(false);
        showArea2.setEditable(false);
        showArea3.setEditable(false);
        JFrame f41=new JFrame("查询结果");
        f41.setLayout(new FlowLayout());
        f41.setSize(400,200);
        f41.setLocation(300,200);
        f41.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        JFrame f51=new JFrame("查询结果");
        f51.setLayout(new FlowLayout());
        f51.setSize(400,200);
        f51.setLocation(300,200);
        f51.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        JFrame f61=new JFrame("查询结果");
        f61.setLayout(new FlowLayout());
        f61.setSize(400,200);
        f61.setLocation(300,200);
        f61.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        f41.add(scrollPane1); f51.add(scrollPane2); f61.add(scrollPane3);
        JDialog dialog1=new JDialog(f1,"添加结果");
        dialog1.setSize(220, 150); 
        dialog1.setLocation(350, 250);
        dialog1.setLayout(new FlowLayout());    
        JLabel lab111=new JLabel("添加成功!");
        dialog1.add(lab111);
        JDialog dialog2=new JDialog(f2,"修改结果");
        dialog2.setSize(220, 150);
        dialog2.setLocation(350, 250);
        dialog2.setLayout(new FlowLayout());
        JLabel lab222=new JLabel("修改成功!");
        dialog2.add(lab222);
        JDialog dialog3=new JDialog(f3,"删除结果");
        dialog3.setSize(220, 150);
        dialog3.setLocation(350, 250);
        dialog3.setLayout(new FlowLayout());
        JLabel lab333=new JLabel("删除成功!");
        dialog3.add(lab333);
        btn1.addActionListener(e->{
            f1.setVisible(true);
        });
        btn11.addActionListener(e->{
            PreparedStatement stmt=null;
            try {
                Connection conn=HomeWork.Mysql_conn();
                Sno=input11.getText();
                Sname=input12.getText();
                _Class=input13.getText();
                Age=Integer.parseInt(input14.getText());
                Sex=input15.getText();
                sql="insert into java values(?,?,?,?,?)";
                stmt=conn.prepareStatement(sql);
                stmt.setString(1, Sno);
                stmt.setString(2, Sname);
                stmt.setString(3, _Class);
                stmt.setInt(4, Age);
                stmt.setString(5, Sex);
                stmt.executeUpdate();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            dialog1.setVisible(true);
        });
        btn2.addActionListener(e->{
            f2.setVisible(true);
        });
        btn21.addActionListener(e->{
            PreparedStatement stmt=null;
            try {
                Connection conn=HomeWork.Mysql_conn();
                sql="update java set Sname=?,_Class=?,Age=?,Sex=? where Sno=?";
                stmt=conn.prepareStatement(sql);
                Sno=input21.getText();
                stmt=conn.prepareStatement(sql);
                Sname=input22.getText();
                _Class=input23.getText();
                Age=Integer.parseInt(input24.getText());
                Sex=input25.getText();
                stmt.setString(1, Sname);
                stmt.setString(2, _Class);
                stmt.setInt(3, Age);
                stmt.setString(4, Sex);
                stmt.setString(5, Sno);
                stmt.executeUpdate();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            dialog2.setVisible(true);
        });
        btn3.addActionListener(e->{
            f3.setVisible(true);
        });
        btn31.addActionListener(e->{
            PreparedStatement stmt=null;
            try {
                Connection conn=HomeWork.Mysql_conn();
                sql="delete from java where Sno=?";
                stmt=conn.prepareStatement(sql);
                Sno=input31.getText();
                stmt=conn.prepareStatement(sql);
                stmt.setString(1, Sno);
                stmt.executeUpdate();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            dialog3.setVisible(true);
        });
        btn4.addActionListener(e->{
            f4.setVisible(true);
        });
        btn41.addActionListener(e->{
            PreparedStatement stmt=null;
            ResultSet rs= null;
            try {
                Connection conn=HomeWork.Mysql_conn();
                Sex=input41.getText();
                sql="select * from java where Sex=?";
                stmt=conn.prepareStatement(sql);
                stmt.setString(1, Sex);
                rs = stmt.executeQuery();
                showArea1.append("    学号     姓名     班级    年龄    性别 "+"\n");
                while(rs.next()){
                    showArea1.append(rs.getString("Sno")+" | "+rs.getString("Sname")+" | "+rs.getString("_Class")+" | "+rs.getInt("Age")+" | "+rs.getString("Sex"));
                    showArea1.append("\n");
                }
                f41.setVisible(true);
                scrollPane1.setVisible(true);
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        });
        btn5.addActionListener(e->{
            f5.setVisible(true);
        });
        btn51.addActionListener(e->{
            PreparedStatement stmt=null;
            ResultSet rs= null;
            try {
                Connection conn=HomeWork.Mysql_conn();
                int max=Integer.parseInt(input51.getText());
                int min=Integer.parseInt(input52.getText());
                sql="select * from java where Age between ? and ?";
                stmt=conn.prepareStatement(sql);
                stmt.setInt(1, min);
                stmt.setInt(2, max);
                rs = stmt.executeQuery();
                showArea2.append("    学号     姓名     班级    年龄    性别 "+"\n");
                while(rs.next()){
                    showArea2.append(rs.getString("Sno")+" | "+rs.getString("Sname")+" | "+rs.getString("_Class")+" | "+rs.getInt("Age")+" | "+rs.getString("Sex"));
                    showArea2.append("\n");
                }
                f51.setVisible(true);
                scrollPane2.setVisible(true);
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        });
        btn6.addActionListener(e->{
            f6.setVisible(true);
        });
        btn61.addActionListener(e->{
            PreparedStatement stmt=null;
            ResultSet rs= null;
            try {
                Connection conn=HomeWork.Mysql_conn();
                String pro=input61.getText();
                pro=pro+"%";
                sql="select * from java where _Class like ? ";
                stmt=conn.prepareStatement(sql);
                stmt.setString(1, pro);
                rs = stmt.executeQuery();
                showArea3.append("    学号     姓名     班级    年龄    性别 "+"\n");
                while(rs.next()){
                    showArea3.append(rs.getString("Sno")+" | "+rs.getString("Sname")+" | "+rs.getString("_Class")+" | "+rs.getInt("Age")+" | "+rs.getString("Sex"));
                    showArea3.append("\n");
                }
                f61.setVisible(true);
                scrollPane3.setVisible(true);
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        });
}
public static Connection Mysql_conn() throws SQLException{
        PreparedStatement stmt=null;
        ResultSet rs= null;
        Connection conn = null;
        String url = "jdbc:mysql://localhost:3306/mysqljava?characterEncoding=utf8";
        String username = "root";
        String password = "2091921381";
        conn = DriverManager.getConnection(url, username, password);
        return conn;
    }
    public static void main(String[] args){
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            SwingUtilities.invokeLater(HomeWork::createAndShowGUI);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if(rs!=null) {
                try {
                    stmt.close();
                }catch(SQLException e) {
                    e.printStackTrace();
                }
                rs=null;}
            if(stmt!=null) {
                try {
                    stmt.close();
                }catch(SQLException e) {
                    e.printStackTrace();}
                stmt=null;
            }
            if(conn!=null) {
                try {
                    conn.close();
                }catch(SQLException e) {
                    e.printStackTrace();
                }
                conn=null;}
        }
    }
}

Java连接数据库应用包:

Java连接数据库的DB Browser的connection:



版权声明:本文为qq_55807078原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。