zoukankan      html  css  js  c++  java
  • mysq数据库实战小型管理系统


    前后用了一天多时间断断续续独立完成编码,成功跑起来的时候激动坏了, 第一次用了软件工程课上学到的方法工具来规划整个小项目,最后抽象出八个类  ,使用经典的工厂类设计模式。

    ps:睡觉,,,


    1: 工厂类
    2:关闭工具类
    3:增删改查类
    4:增加界面类
    5:删除界面类
    6:查询界面类
    7:选择操作界面类
    8:登陆界面类




    工厂类:

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    
    public class FactoryClass {
        public Connection getConnection()  {
            Connection con = null ;
            String url = "org.gjt.mm.mysql.Driver" ;
            try
            {
             Class.forName(url) ;
            }catch(ClassNotFoundException cfe)  {
                cfe.printStackTrace();
            };
            try  {
                     con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student","root","include") ;
            }catch(SQLException se)  {
                se.printStackTrace();
            }
            return con ;
        }
    
    }



    操作工具类

    import java.sql.PreparedStatement;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.sql.Connection;
    import java.sql.ResultSet;
    
    
    public class JDBCUtils {
        /**关闭结果集,语句块,连接
         *
         * @param rs
         * @param st
         * @param con
         */
        public static void close(ResultSet rs,PreparedStatement st,Connection con)  {
            try  {
                rs.close() ;
                st.close() ;
                con.close() ;
            }catch(SQLException se)  {
                se.printStackTrace();
            }
        }
       
        /**关闭语句块与连接
         *
         * @param st
         * @param con
         */
        public static void close(PreparedStatement st,Connection con)  {
            try  {
                st.close();
                con.close();
            }catch(SQLException se) {
                se.printStackTrace();
            }   
        }
       
        /**
         * 关闭连接
         *
         */
        public static void close(Connection con)  {
            try  {
                con.close();
            }catch(SQLException se)  {
                se.printStackTrace();
            }
        }
       
        /**
         * 关闭结果集
         */
        public static void close(ResultSet rs)  {
            try  {
                rs.close();
            }catch(SQLException se)  {
                se.printStackTrace();
            }
        }
       
        /**
         * 关闭语句块
         */
        public static void close(PreparedStatement st)  {
            try  {
                st.close();
            }catch(SQLException se)  {
                se.printStackTrace();
            }
        }
    
    }


    基础增删改查类

    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    
    public class PeopleBiz {
       
        /*展示所有成员
         *
         */
        public void showAll() throws SQLException  {
            FactoryClass factory = new FactoryClass() ;
            Connection con = factory.getConnection() ;
            String sql = "select * from people" ;
            PreparedStatement ps  = con.prepareStatement(sql) ;
            ResultSet rs = ps.executeQuery() ;
            while(rs.next())  {
                String nameResult = rs.getString(2) ;
                String passwordResult = rs.getString(3) ;
                System.out.println("姓名:"+nameResult+"   密码:"+passwordResult) ;
            }
            JDBCUtils.close(rs,ps,con);
           
        }
       
        /**
         * 插入新成员
         * @throws SQLException
         */
        public void insertData(String name,String password) throws SQLException {
            FactoryClass factory = new FactoryClass() ;
            Connection con  = factory.getConnection() ;
            String sql = "insert into people(name,password) values('"+name+"','"+password+"') ";
            PreparedStatement ps =  con.prepareStatement(sql) ;
            ps.executeUpdate() ;
            JDBCUtils.close(ps,con);
        }
       
        /**
         * 按照名字为索引删除成员
         * @throws SQLException
         */
       
        public void deleteByName(String name) throws SQLException  {
            FactoryClass factory = new FactoryClass() ;
            Connection con = factory.getConnection() ;
            String sql = "delete from people where name= '"+name+"' " ;
            PreparedStatement ps = con.prepareStatement(sql) ;
            ps.executeUpdate() ;
            JDBCUtils.close(ps,con);
           
        }
       
       
        /**
         * 更改数据库中的成员
         * @throws SQLException
         */
        public void updateDate(String name,String password) throws SQLException  {
            FactoryClass factory = new FactoryClass() ;
            Connection con = factory.getConnection() ;
            String sql = "update people set(name,password) values('"+name+"' , '"+password+"')" ;
            PreparedStatement ps = con.prepareStatement(sql) ;
            ps.executeUpdate() ;
            JDBCUtils.close(ps,con);
        }
       
        public String[] selectByName(String name) throws SQLException  {
            FactoryClass factory = new FactoryClass() ;
            Connection con = factory.getConnection() ;
            String sql  = "select * from people where name= '"+name+"' " ;
            PreparedStatement ps = con.prepareStatement(sql) ;
            ResultSet rs = ps.executeQuery() ;
            String data[] = new String[2] ;
            while(rs.next())  {
                data[0] = rs.getString(1) ;
                data[1] = rs.getString(2) ;
            }
            return data ;
        }
       
       
       
    }
       
    
    
    /*
      * 下面开始编写测试主函数Tester
     */
    /*class Tester  {
        public static void main(String args[]) throws Exception  {
            PeopleBiz p = new PeopleBiz() ;
            p.insertData("王如意", "include");
            //p.deleteByName("王") ;
            p.showAll();
        }
    }*/
    



    登陆界面类

    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.BorderFactory;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JPasswordField;
    import javax.swing.JTextField;
    import javax.swing.border.Border;
    import javax.swing.border.EtchedBorder;
    import javax.swing.border.TitledBorder;
    
    
    public class Login extends JFrame   {
       
        private JTextField nameText ;
        private JPasswordField passText ;
        public Login()  {
            this.setTitle("数据库登陆");
            this.setSize(330,230);
            this.setLocation(300,300);
            this.setResizable(false);
            this.setDefaultCloseOperation(EXIT_ON_CLOSE);
            this.setVisible(true);
            JPanel mainpanel = new JPanel() ;
            mainpanel.setLayout(null);
            Border border = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED) ;
           mainpanel.setBorder(BorderFactory.createTitledBorder(border,"输入登陆信息",TitledBorder.CENTER,TitledBorder.TOP));
            this.add(mainpanel,BorderLayout.CENTER) ;
            mainpanel.setLayout(null)      ;  
            JLabel nameLabel = new JLabel("输入用户名: ") ;
            nameLabel.setBounds(30,30,80,22);
            mainpanel.add(nameLabel) ;
            nameText = new JTextField() ;
            nameText.setBounds(115,30,120,22);
            mainpanel.add(nameText) ;
            JLabel passLabel = new JLabel("请输入密码: ") ;
            passLabel.setBounds(30,60,80,22);
            mainpanel.add(passLabel) ;
            passText = new JPasswordField() ;
            passText.setBounds(115,60,120,22) ;
            mainpanel.add(passText);
            JButton reset = new JButton("重置") ;
            JButton login = new JButton("登陆") ;
            reset.setBounds(40,90,60,20) ;
            login.setBounds(130,90,60,20);
            mainpanel.add(login) ;
            mainpanel.add(reset) ;
            reset.addActionListener(new ActionListener()  {
                public void actionPerformed(ActionEvent e)  {
                    nameText.setText("");
                    passText.setText("") ;
                }
            });
           
            login.addActionListener(new ActionListener() {
               
                @Override
                public void actionPerformed(ActionEvent e) {
                    // TODO Auto-generated method stub
                    if(nameText.getText().equals("daemon") && passText.getText().equals("include"))  {
                        JOptionPane.showMessageDialog(null, "欢迎进入系统","登陆成功",JOptionPane.INFORMATION_MESSAGE);
                        new Option() ;
                    }
                    else JOptionPane.showMessageDialog(null, "古文!!!","doubi",JOptionPane.INFORMATION_MESSAGE);
                   
                }
            });
           
               
        }
    
    }
    
    class Tester {
        public static void main(String args[])throws Exception  {
            new Login() ;
           
        }
    }




    选择操作类


    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    
    public class Option extends JFrame  {
        JButton addButton = new JButton("增加") ;
        JButton deleteButton = new JButton("删除") ;
        JButton queryButton = new JButton("查询") ;
        public Option()  {
            this.setTitle("请选择操作") ;
            this.setSize(330,230);
            this.setVisible(true);
            JPanel panel = new JPanel() ;   
            panel.add(addButton) ;
            panel.add(deleteButton) ;
            panel.add(queryButton) ;
            this.add(panel,BorderLayout.CENTER);
           
            /**
             * 下面开始为三个时间按钮添加事件响应
             */
           
            addButton.addActionListener(new ActionListener()  {
                public void actionPerformed(ActionEvent e)  {
                    new Add() ;
                }
            });
           
            deleteButton.addActionListener(new ActionListener()  {
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    // TODO Auto-generated method stub
                    new Delete() ;
                   
                }
            });
           
            queryButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    // TODO Auto-generated method stub
                    new Query() ;
                   
                }
            });
           
        }
    
    }



    以下为具体实现增删改查

    //添加成员类
    import java.awt.BorderLayout;
    import java.awt.TextField;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.sql.SQLException;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    
    public class Add extends JFrame  {
        private JTextField nametext ;
        private JTextField passtext ;
        public Add()  {
            this.setTitle("请输入要插入的成员信息");
            this.setSize(400,400);
            this.setVisible(true);
            JPanel panel = new JPanel() ;
            panel.setLayout(null);
            JLabel namelab = new JLabel("姓名") ;
            namelab.setBounds(30,30,80,22);
            panel.add(namelab) ;
            this.nametext = new JTextField() ;
            this.nametext.setBounds(115,30,120,22);
            panel.add(nametext) ;
            JLabel passlab = new JLabel("密码") ;
            passlab.setBounds(30,60,80,22);
            panel.add(passlab) ;
            passtext = new JTextField() ;
            passtext.setBounds(115,60,120,22);
            panel.add(passtext) ;
            this.add(panel,BorderLayout.CENTER) ;
            JButton ok = new JButton("插入") ;
            this.add(ok,BorderLayout.SOUTH) ;
           
            ok.addActionListener(new ActionListener()  {
                public void actionPerformed(ActionEvent e)  {
                    String nameinput = nametext.getText();
                    String passinput = passtext.getText() ;
                    boolean flag = false;
                    PeopleBiz pb = new PeopleBiz() ;
                    try {
                        pb.insertData(nameinput, passinput);
                    } catch (SQLException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    JOptionPane.showMessageDialog(null, "数据插入成功","插入结果",JOptionPane.INFORMATION_MESSAGE);
                   
                }
            });
           
        }
    }
    
    
    //删除成员类
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.sql.SQLException;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    
    public class Delete extends JFrame {
        private JTextField nametext ;
        private JButton ok ;
        public Delete()  {
            this.setTitle("输入要删除的姓名") ;
            this.setVisible(true) ;
            this.setSize(400,400);
            JPanel panel = new JPanel() ;
            panel.setLayout(null) ;
            JLabel namelab= new JLabel("姓名") ;
            namelab.setBounds(30,30,80,22);
            panel.add(namelab) ;
            nametext = new JTextField() ;
            nametext.setBounds(115,30,120,22) ;
            panel.add(nametext) ;
            this.add(panel,BorderLayout.CENTER) ;
            this.ok = new JButton("确认删除") ;
            this.add(this.ok,BorderLayout.SOUTH) ;
           
            this.ok.addActionListener(new ActionListener()  {
                public void actionPerformed(ActionEvent e)  {
                    String deletename = nametext.getText() ;
                    PeopleBiz p = new PeopleBiz() ;
                    try {
                        p.deleteByName(deletename);
                    } catch (SQLException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    JOptionPane.showMessageDialog(null, "数据删除成功","反馈信息!",JOptionPane.INFORMATION_MESSAGE);
                }
            });
           
        }
    
    }
    
    
    //查询界面类
    import java.awt.BorderLayout;
    import java.awt.TextField;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.sql.SQLException;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    
    public class Query extends JFrame  {
        private JTextField nametext ;
        private JTextField passtext ;
        public Query()  {
            this.setTitle("所有查询成员信息!");
            this.setSize(400,400);
            this.setVisible(true);
            JPanel panel = new JPanel() ;
            panel.setLayout(null);
            JLabel namelab = new JLabel("姓名") ;
            namelab.setBounds(30,30,80,22);
            panel.add(namelab) ;
            this.nametext = new JTextField() ;
            this.nametext.setBounds(115,30,120,22);
            panel.add(nametext) ;
            JLabel passlab = new JLabel("密码") ;
            passlab.setBounds(30,60,80,22);
            panel.add(passlab) ;
            passtext = new JTextField() ;
            passtext.setBounds(115,60,120,22);
            panel.add(passtext) ;
            this.add(panel,BorderLayout.CENTER) ;
            JButton ok = new JButton("查询") ;
            this.add(ok,BorderLayout.SOUTH) ;
           
            ok.addActionListener(new ActionListener()  {
                public void actionPerformed(ActionEvent e)  {
                    String nameinput = nametext.getText();
                    boolean flag = false;
                    PeopleBiz pb = new PeopleBiz() ;
                    try {
                        String res[]=pb.selectByName(nameinput);
                        passtext.setText(res[1]);
                        JOptionPane.showMessageDialog(null, "查询结果已显示在密码框!","查询结果",JOptionPane.INFORMATION_MESSAGE);
                    } catch (SQLException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    JOptionPane.showMessageDialog(null, "数据插入成功","插入结果",JOptionPane.INFORMATION_MESSAGE);
                   
                }
            });
           
        }
    }
    
    



  • 相关阅读:
    为什么用strlcpy取代strncpy
    linux系统调用和库函数调用的区别
    VC、PE和天使投资是什么意思?有什么区别?
    AWS使用心得:当初我曾错过的那些宝贵经验
    bzoj 2208: [Jsoi2010]连通数
    android学习笔记(5)Activity生命周期学习
    Nginx 进程间通信
    手写一个节点大小平衡树(SBT)模板,留着用
    JavaFX学习之道:JavaFX之TableView
    Docker 命令行和后台參数
  • 原文地址:https://www.cnblogs.com/emoji/p/4436849.html
Copyright © 2011-2022 走看看