码迷,mamicode.com
首页 > 数据库 > 详细

java-Mysql学生管理系统

时间:2019-05-27 19:14:09      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:Dimension   getc   todo   rom   generate   panel   seo   from   catch   

Window1//主方法
package stu_zizhu1;

import java.awt.Button;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Window1 {
public static void main(String[] args) {
JFrame jf=new JFrame("学生管理系统");

    JButton b1=new JButton("添加");
    JButton b2=new JButton("查找");
    JButton b3=new JButton("修改");
    JButton b4=new JButton("删除");
    JButton b5=new JButton("浏览");
    FlowLayout flow = new FlowLayout(FlowLayout.LEFT,10,10);
    JPanel jp=new JPanel(flow);
    jp.add(b1);
    jp.add(b2);
    jp.add(b3);
    jp.add(b4);
    jp.add(b5);
    
    jf.add(jp);
     
    jp.setBackground(Color.red);
    jf.setSize(500,500);
    jf.setLocation(500,500);
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jf.setVisible(true);
    
    b1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Add1 a1=new Add1();
        }
    });
    b2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Find1 find1=new Find1();
        }
    });
    b3.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Modify1 modify1=new Modify1();
        }
    });
    b4.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Delete1 delete1=new Delete1();
        }
    });
    b5.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Look1 look1=new Look1();
        }
    });

}
    

}

package stu_zizhu1;

import java.awt.Button;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.DriverManager;
import java.sql.SQLException;

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

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;

public class Add1 extends JFrame{

//定义标签
JLabel jlid=new JLabel("ID:");
JLabel jlname=new JLabel("Name:");
JLabel jlage=new JLabel("Age:");
JLabel jlsex=new JLabel("Sex:");
JLabel jlscore=new JLabel("Score:");
JLabel jlbirthday=new JLabel("Birthday:");
//定义文本框

JTextField jfid=new JTextField("",20);
JTextField jfname=new JTextField("",20);
JTextField jfage=new JTextField("",20);
JTextField jfsex=new JTextField("",20);
JTextField jfscore=new JTextField("",20);
JTextField jfbirthday=new JTextField("",20);

//定义按钮(本人再次严重犯错JButton被我写成Button 真的是头疼)
JButton tianjia=new JButton("添加");
JButton chongzhi =new JButton("重置");
JButton fanhui=new JButton("返回");

public  Add1() {
    //定义面板
    
    //jpid面板添加标签(jlid)&&文本框(jfid)
    JPanel jpid=new JPanel();
    jpid.add(jlid);
    jpid.add(jfid);
    
    JPanel jpname=new JPanel();
    jpname.add(jlname);
    jpname.add(jfname);
    
    JPanel jpage=new JPanel();
    jpage.add(jlage);
    jpage.add(jfage);
    
    JPanel jpsex=new JPanel();
    jpsex.add(jlsex);
    jpsex.add(jfsex);
    
    JPanel jpscore=new JPanel();
    jpscore.add(jlscore);
    jpscore.add(jfscore);
    
    JPanel jpbirthday=new JPanel();
    jpbirthday.add(jlbirthday);
    jpbirthday.add(jfbirthday);
    
    JPanel jpbutton=new JPanel(new GridLayout(1,3));
    jpbutton.add(tianjia);
    jpbutton.add(chongzhi);
    jpbutton.add(fanhui);
    
    //按钮添加监听器
    tianjia.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            Connection conn=null;
            PreparedStatement prestat=null;
            Statement stat =null;
            String sql ="insert into student1(id,name,age,sex,score,birthday) "+"values(?,?,?,?,?,?)";//少加一个问好?
            try{
                Class.forName("Driver");
                System.out.println("JBDC 加载成功!");
            }catch(Exception a){
                System.out.println("JBDC 狗带!");
                a.printStackTrace();
            }
            try {
                conn= (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/yonghu?useSSL=false","root","123456");
                prestat=(PreparedStatement) conn.prepareStatement(sql);
                
                prestat.setString(1,jfid.getText());
                prestat.setString(2,jfname.getText());
                prestat.setString(3,jfage.getText());
                prestat.setString(4,jfsex.getText());
                prestat.setString(5,jfscore.getText());
                prestat.setString(6,jfbirthday.getText());
                
                prestat.executeUpdate();
            } catch (SQLException s) {
                // TODO Auto-generated catch block
                s.printStackTrace();
            }finally {
                try {
                    conn.close();
                } catch (SQLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
            
        }
        
    });
    fanhui.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            Window1 window1=new Window1();
        }
        
    });
    chongzhi.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            jfid.setText("");
            jfname.setText("");
            jfage.setText("");
            jfsex.setText("");
            jfscore.setText("");
            jfbirthday.setText("");
            
        }
        
    });


    this.setTitle("添加学生信息");
    this.setLayout(new GridLayout(9,1));
    this.add(jpid);
    this.add(jpname);
    this.add(jpage);
    this.add(jpsex);
    this.add(jpscore);
    this.add(jpbirthday);
    this.add(jpbutton);
    this.setLocation(400,300);
    this.setSize(350,300);
    this.setVisible(true);
    
}

}
package stu_zizhu1;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

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

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

public class Find1 extends JFrame{
JLabel jlid=new JLabel("ID:");
JTextField jtfid=new JTextField("",20);

JLabel jlname=new JLabel("Name:");
JLabel jlage=new JLabel("Age:");
JLabel jlsex=new JLabel("Sex:");
JLabel jlscore=new JLabel("Score:");
JLabel jlbirthday=new JLabel("Birthday:");


//JLabel jid=new JLabel();
JLabel jname=new JLabel();
JLabel jage=new JLabel();
JLabel jsex=new JLabel();
JLabel jscore=new JLabel();
JLabel jbirthday=new JLabel();
//此处按钮一定要设置成JButton
JButton query=new JButton("查询");
JButton ret=new JButton("返回");
//此处不可有void
    public   Find1() {
    JPanel jpid=new JPanel();
    jpid.add(jlid);
    jpid.add(jtfid);
    
    JPanel jpname=new JPanel();
    jpname.add(jlname);
    jpname.add(jname);
    
    JPanel jpage=new JPanel();
    jpage.add(jlage);
    jpage.add(jage);
    
    JPanel jpsex=new JPanel();
    jpsex.add(jlsex);
    jpsex.add(jsex);
    
    JPanel jpscore=new JPanel();
    jpscore.add(jlscore);
    jpscore.add(jscore);
    
    JPanel jpbirthday=new JPanel();
    jpbirthday.add(jlbirthday);
    jpbirthday.add(jbirthday);
    
    JPanel button1=new JPanel(new GridLayout(1,1));
    button1.add(query);
    button1.add(ret);
    
    query.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e1) {
            Connection conn=null;
            Statement state=null;
            ResultSet res=null;
            //此处犯错 忘记写id
            String sql="select id,name,age,sex,score,birthday from student1;";
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                System.out.println("123");
                e.printStackTrace();
            }
            
            try {
                conn=(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/yonghu?useSSL=false","root","123456");
                state=(Statement) conn.createStatement();
                res=state.executeQuery(sql);
                while(res.next()) {
                    if(res.getString(1).equals(jtfid.getText())) {
                        jname.setText(res.getString(2));
                        jage.setText(res.getString(3));
                        jsex.setText(res.getString(4));
                        jscore.setText(res.getString(5));
                        jbirthday.setText(res.getString(6));
                        
                    }
                }
            } catch (SQLException e) {
                System.out.println("1234");
                e.printStackTrace();
            }finally {
                try {
                    conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        
    });
    
    ret.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            Window1 window1=new Window1();
            
        }
        
    });
    this.setTitle("学生查询系统 ");
    this.setLayout(new GridLayout(9,1));//9组建
    this.add(jpid);
    this.add(jpname);
    this.add(jpage);
    this.add(jpsex);
    this.add(jpscore);
    this.add(jpbirthday);
    this.add(button1);
    this.setSize(300, 400);
    this.setLocation(300, 400);
    this.setVisible(true);
    
    
    
    
    
}

}
package stu_zizhu1;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

public class Look1 extends JFrame{
Connection conn =null;
PreparedStatement pre=null;
ResultSet res=null;

JScrollPane jps=null;
Vector columnsname=null;
Vector rowData=null;

public  Look1() {
    JPanel jp1=new JPanel(); 
    jp1.setBackground(Color.gray);
    jp1.setSize(500, 500);
    JLabel jb=new JLabel("欢迎使用学生管理系统");
    jb.setBounds(0, 0, 50, 50);
    jp1.add(jb);
    
    
    JPanel jp=new JPanel();
    //JLabel jb1=new JLabel("欢迎使用学生管理系统");
    jp.setSize(400, 400);
    jp.setBackground(Color.green);
    //jp.add(jb1);
    jp.add(jp1);
    //定义列名
    columnsname=new Vector();
    columnsname.add("id");
    columnsname.add("name");
    columnsname.add("age");
    columnsname.add("sex");
    columnsname.add("score");
    columnsname.add("birthday");
    rowData =new Vector();
    try {
        Class.forName("com.mysql.jdbc.Driver");
        conn=(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/yonghu?useSSL=false","root","123456");
        pre=(PreparedStatement) conn.prepareStatement("select *from student1");
        res=pre.executeQuery();
        while(res.next()) {
            Vector hang =new Vector();
            hang.add(res.getString(1));
            hang.add(res.getString(2));
            hang.add(res.getString(3));
            hang.add(res.getString(4));
            hang.add(res.getString(5));
            hang.add(res.getString(6));
            rowData.add(hang);
            
        }
        System.out.println("load  ok!");
    } catch (Exception e) {
        System.out.println("go die");
        e.printStackTrace();
    }finally {
        try {
            res.close();
            pre.close();
            conn.close();
            System.out.println("close ok");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
    JTable jtable=new JTable(rowData,columnsname);
    jps=new JScrollPane(jtable);
    
    this.add(jps);
    this.setTitle("学生浏览系统");
    this.setLayout(new GridLayout(3,4));
    
    this.add(jp);
    
    
    this.setSize(400, 500);
    this.setLocation(300,400);
    this.setVisible(true);
    this.setResizable(false);
    
}

}
package stu_zizhu1;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

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

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;

import First.Window;

public class Modify1 extends JFrame{
//定义标签
JLabel jlid=new JLabel("ID:");
JLabel jlname=new JLabel("Name:");
JLabel jlage=new JLabel("Age:");
JLabel jlsex=new JLabel("Sex:");
JLabel jlscore=new JLabel("Score:");
JLabel jlbirthday=new JLabel("Birthday:");
//定义文本框

JTextField jfid=new JTextField("",20);
JTextField jfname=new JTextField("",20);
JTextField jfage=new JTextField("",20);
JTextField jfsex=new JTextField("",20);
JTextField jfscore=new JTextField("",20);
JTextField jfbirthday=new JTextField("",20);

//定义按钮(本人再次严重犯错JButton被我写成Button 真的是头疼)
JButton change=new JButton("修改");
JButton chongzhi =new JButton("重置");
JButton fanhui=new JButton("返回");

public  Modify1() {
    //定义面板

    //jpid面板添加标签(jlid)&&文本框(jfid)
    JPanel jpid=new JPanel();
    jpid.add(jlid);
    jpid.add(jfid);

    JPanel jpname=new JPanel();
    jpname.add(jlname);
    jpname.add(jfname);

    JPanel jpage=new JPanel();
    jpage.add(jlage);
    jpage.add(jfage);

    JPanel jpsex=new JPanel();
    jpsex.add(jlsex);
    jpsex.add(jfsex);

    JPanel jpscore=new JPanel();
    jpscore.add(jlscore);
    jpscore.add(jfscore);

    JPanel jpbirthday=new JPanel();
    jpbirthday.add(jlbirthday);
    jpbirthday.add(jfbirthday);

    JPanel jpbutton=new JPanel(new GridLayout(1,3));
    jpbutton.add(change);
    jpbutton.add(chongzhi);
    jpbutton.add(fanhui);
    change.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            //String id1=jfid.getText();
            String name1=jfname.getText();//此处犯错
            String age1=jfage.getText();
            String sex1=jfsex.getText();
            String score1=jfscore.getText();
            String birthday1=jfbirthday.getText();
            
            Connection conn=null;
            Statement state=null;

// PreparedStatement prestate=null;
ResultSet res=null;

            String sql="select id,name,age,sex,score,birthday from student1;";
            
            try{
                Class.forName("com.mysql.jdbc.Driver");
                
            }catch(Exception d){
                System.out.println("jdbc fall");
                d.printStackTrace();
            }
            try{
                conn=(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/yonghu?useSSL=false","root","123456");
                state=(Statement) conn.createStatement();
                res=state.executeQuery(sql);
                while (res.next())
                {
                    //change
                    if (res.getString(1).equals(jfid.getText()))
                    {
                    try{
                        Class.forName("com.mysql.jdbc.Driver");
                    }catch(Exception d){
                        System.out.println("jdbc fall");
                        d.printStackTrace();
                    }
                        String sql1="update student1 set name='"+name1+"'where id='"+jfid.getText()+"'";
                        String sql2="update student1 set age='"+age1+"'where id='"+jfid.getText()+"'";
                        String sql3="update student1 set sex='"+sex1+"'where id='"+jfid.getText()+"'";
                        String sql4="update student1 set score='"+score1+"'where id='"+jfid.getText()+"'";
                        String sql5="update student1 set birthday='"+birthday1+"'where id='"+jfid.getText()+"'";
                        try {
                            conn=(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/yonghu?useSSL=false","root","123456");
                            state=(Statement) conn.createStatement();
                            state.execute(sql1);
                            state.execute(sql2);
                            state.execute(sql3);
                            state.execute(sql4);
                            state.execute(sql5);
                        } catch (SQLException g) {
                            // TODO Auto-generated catch block
                            g.printStackTrace();
                        }try{
                            state.close();
                            conn.close();
                        }catch(SQLException ar){
                            ar.printStackTrace();
                    }

                        break;
                    }
                    
                //change end
                }
            }catch (SQLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            
            
        }
            finally{
                try{
                    conn.close();
                }catch(SQLException ar){
                    ar.printStackTrace();
                }
        
            }
            



        }

    });
    fanhui.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            Window window = new Window();           
        }           
    });
    chongzhi.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            jfid.setText("");
            jfname.setText("");
            jfage.setText("");
            jfsex.setText("");
            jfscore.setText("");
            jfbirthday.setText("");

        }

    });
    this.setTitle("修改学生信息");
    this.setLayout(new GridLayout(9,1));
    this.add(jpid);
    this.add(jpname);
    this.add(jpage);
    this.add(jpsex);
    this.add(jpscore);
    this.add(jpbirthday);
    this.add(jpbutton);
    this.setLocation(400,300);
    this.setSize(350,300);
    this.setVisible(true);

}

}

java-Mysql学生管理系统

标签:Dimension   getc   todo   rom   generate   panel   seo   from   catch   

原文地址:https://www.cnblogs.com/Pythons/p/10932308.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!