JAVA课程设计——农村人口管理系统 D4 功能实现(户头增删改查)

上一篇,操作员页面设计

 现在我们就来实现一下对户头的增删改查。

目录

添加户头

 增加户头页面设计:

增加户头功能设计

Host

显示户头

 1.显示户头的页面设计

2.显示户头功能设计

删除户头

1.删除户头页面设计

2.删除户头功能设计

更新户头

1.更新户头界面设计

2.更新户头功能设计


添加户头

 

 增加户头页面设计:

也就是AddHost函数里面具体内容:

AddHost(){
            setSize(650,300);
            setBorder(BorderFactory.createTitledBorder("增加户头"));
            t1 = new JTextField(10);
            l1=new Label("请输入身份证号");
            t2 = new JTextField(10);
            l2=new Label("请输入姓名");
            t3 = new JTextField(10);
            l3=new Label("请输入性别");
            t4 = new JTextField(10);
            l4=new Label("请输入家庭住址");
            t5 = new JTextField(10);
            l5=new Label("请输入宅基地大小");
            t6 = new JTextField(10);
            l6=new Label("请输入手机号");
            t7 = new JTextField(10);
            l7=new Label("请输入贫困类");



            setLayout(new GridLayout(5,2));
            JPanel p1 = new JPanel();
            JPanel p2 = new JPanel();
            JPanel p3 = new JPanel();
            JPanel p4 = new JPanel();
            JPanel p5 = new JPanel();
            JPanel p6 = new JPanel();
            JPanel p7 = new JPanel();



            p1.add(l1);p2.add(l2);p3.add(l3);p4.add(l4);p5.add(l5);p6.add(l6);p7.add(l7);
            p1.add(t1);p2.add(t2);p3.add(t3);p4.add(t4);p5.add(t5);p6.add(t6);p7.add(t7);
            add(p1);add(p2);add(p3);add(p4);add(p5);add(p6);add(p7);

            JPanel p8 = new JPanel();
            bOK = new JButton("增加");
            bOK.addActionListener(this);
            p8.add(bOK); add(p8);
        }

 当我们把页面布置完毕,现在就是实现点击监听方法,怎么样才能把文本框的内容输入到数据库中:

增加户头功能设计

 @Override
        public void actionPerformed(ActionEvent e) {
            Object obj = e.getSource();
            if(obj == bOK) {
                String hid = this.t1.getText();
                String hname = this.t2.getText();
                String hsex = this.t3.getText();
                String h_haddress = this.t4.getText();
                String hraea = this.t5.getText();
                String h_phone = this.t6.getText();
                String hpleve = this.t7.getText();
                Connection connection=myDataBase.getConnection();//与数据库建立联系
                String sql = "insert into t_host(hid,hname,hsex,h_haddress,harear,h_phone,hpleve) values("+hid+",'"+hname+"','"+hsex+"','"+h_haddress+"','"+hraea+"','"+h_phone+"','"+hpleve+"')";
                try {
                    PreparedStatement ps=connection.prepareStatement(sql);
                    System.out.println(sql);
                    ps.execute(sql);//执行Sql语句
                    boolean i=ps.execute("select * from t_host where hid=("+hid+")");//判断之前的数据有没有插入到数据库
                    myDataBase.close(ps,connection);
                    if (i)
                        JOptionPane.showMessageDialog(null,"添加成功!","系统信息",JOptionPane.INFORMATION_MESSAGE);
                }catch(Exception e1) {
                    System.err.println(e1.getMessage());
                    JOptionPane.showMessageDialog(null,"添加失败!","系统信息",JOptionPane.INFORMATION_MESSAGE);
                }

            }
        }

上面的添加 功能我当初是作为一个测试来写,所以它的功能就写在了添加的页面设计中去,后期为了方便管理和美观下面三个功能,我单独拿出来,把具体实现,放在了OperatorDao中。

所以在实现下面三个功能时,我们先建立一个类,这个类的字段和属性要根据你设计的数据库的字段和属性来设置。

Host

public class Host {
    private String hid;
    private String hname;
    private String hsex;
    private String h_haddress;
    private Double harear;
    private Integer h_phone;
    private Integer hnumber;
    private String hpleve;

    public String getHid() {
        return hid;
    }
    public void setHid(String hid) {
        this.hid = hid;
    }
    public String getHname() {
        return hname;
    }

    public void setHname(String hanme) {
        this.hname = hanme;
    }

    public String getHsex() {
        return hsex;
    }

    public void setHsex(String hsex) {
        this.hsex = hsex;
    }

    public String getH_haddress() {
        return h_haddress;
    }

    public void setH_haddress(String h_haddress) {
        this.h_haddress = h_haddress;
    }

    public Double getHarear() {
        return harear;
    }

    public void setHarear(Double harear) {
        this.harear = harear;
    }

    public Integer getH_phone() {
        return h_phone;
    }

    public void setH_phone(Integer h_phone) {
        this.h_phone = h_phone;
    }

    public Integer getHnumber() {
        return hnumber;
    }

    public void setHnumber(Integer hnumber) {
        this.hnumber = hnumber;
    }

    public String getHpleve() {
        return hpleve;
    }

    public void setHpleve(String hpleve) {
        this.hpleve = hpleve;
    }


    @Override
    public String toString() {
        return "Host{" +
                "hid='" + hid + '\'' +
                ", hanme='" + hname + '\'' +
                ", hsex='" + hsex + '\'' +
                ", h_haddress='" + h_haddress + '\'' +
                ", harear=" + harear +
                ", h_phone=" + h_phone +
                ", hnumber=" + hnumber +
                ", hpleve='" + hpleve + '\'' +
                '}';
    }

显示户头

 1.显示户头的页面设计

 ShowHost内部代码

 ShowHost(List<Host> list) {
        setSize(650, 600);
        setBorder(BorderFactory.createTitledBorder("显示户头"));

        String[] index = {"身份证号", "姓名", "性别", "家庭住址", "在基地大小", "电话", "家庭人口", "贫苦等级"};
        Object[][] data = new Object[list.size()][index.length];
        //向data添加数据
        for (int i = 0; i < list.size(); i++) {
            Host host = list.get(i);
            data[i][0] = host.getHid();
            data[i][1] = host.getHname();
            data[i][2] = host.getHsex();
            data[i][3] = host.getH_haddress();
            data[i][4] = host.getHarear();
            data[i][5] = host.getH_phone();
            data[i][6] = host.getHnumber();
            data[i][7] = host.getHpleve();
        }
        //添加一个表格
        DefaultTableModel dtm = new DefaultTableModel(data, index);
        JTable table = new JTable(dtm);
        table.setBackground(Color.PINK);
        //设置table的高度和宽度
        table.setPreferredScrollableViewportSize(new Dimension(650, 500));
        table.setFillsViewportHeight(true);
        //5,给表格设置滚动条
        JScrollPane jScrollPane = new JScrollPane();
        jScrollPane.setViewportView(table);
        //添加滚条
        add(jScrollPane, BorderLayout.SOUTH);
    }

2.显示户头功能设计

1.这是在OperatorDao中的功能。接下来的功能也是类似的。

  //获取数据以结果集返回,查找户头数据,
    public static List<Host> getData() {
        String sql = "select * from t_host";
        PreparedStatement ps;
        LinkedList<Host> list = null;
        try {
            Connection connection = myDataBase.getConnection();
            ps = connection.prepareStatement(sql);
            ResultSet result = ps.executeQuery();//获得结果集
            list = new LinkedList<>();
            while (result.next()) {
                Host host = new Host();
                host.setHid(result.getString("hid"));
                host.setHname(result.getString("hname"));
                host.setHsex(result.getString("hsex"));
                host.setH_haddress(result.getString("h_haddress"));
                host.setHarear(result.getDouble("harear"));
                host.setH_phone(result.getInt("h_phone"));
                host.setHnumber(result.getInt("hnumber"));
                host.setHpleve(result.getString("hpleve"));
                list.add(host);
            }
            myDataBase.close(ps, connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

2.在OperatorJF也就是管理员内部加上这个。添加户头在上一篇介绍操作员界面时举过例子,所以不在概述,后面的删除和修改也是和下面类似,我将不在 一 一举例。

 //显示户头操作面板

        jm2.addActionListener(e -> {
            jc.removeAll();
            jc.repaint();//重新绘制
            jc.add(new ShowHost(OperatorDao.getData()));//添加新面板
            jc.revalidate();//重新布局
        });

删除户头

1.删除户头页面设计

public class DeleteHost extends Jcenter{
    DeleteHost(){
        setSize(650,300);
        setBorder(BorderFactory.createTitledBorder("删除户头"));
        Label la=new Label("温馨提示删除户头前,请先将该户头下的全部家庭成员修改或者移除");
        add(la);
        JPanel p1=new JPanel();
        Label ll=new Label("输入要删除的户主身份证号:");
        JTextField f1=new JTextField(4);
        p1.add(ll);
        p1.add(f1);
        add(p1);
        Button bOK=new Button("确认");
        add(bOK);
        bOK.addActionListener(e -> {
            String hid=f1.getText();
            OperatorDao.deleteById(hid);
        });
    }
}

2.删除户头功能设计

//通过Id删除数据
    public static void deleteById(String hid) {
        String sql = "delete from t_host where hid=?";//类似于一个占位符
        PreparedStatement ps;
        try {
            Connection connection = myDataBase.getConnection();
            ps = connection.prepareStatement(sql);
            ps.setString(1, hid);//参数标记从 1 开始编号, 通常指的是第一个“?”
            ps.executeUpdate();
            int f = ps.executeUpdate();
            if (f > 0) {
                JOptionPane.showMessageDialog(null, "没有删除数据");
            } else {
                JOptionPane.showMessageDialog(null, "成功删除数据");
            }
            ps.close();
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

更新户头

更新户头我设计的是用ID查找对应户头信息,然后把被查找到的户头信息显示在文本框中,再对文本框中内容进行修改,再提交。(之前有位读者,跟我反映了一下身份证号无法修改的问题,我现在把之前的问题解决了一下,如果还有其他的小毛病欢迎大家私聊我)

1.更新户头界面设计

public class UpdateHost extends Jcenter  {
    JButton bOK;
    JTextField t1,t2,t3,t4,t5,t6,t7,t8;
    Label l1,l2,l3,l4,l5,l6,l7,l8;
    UpdateHost(){
        setSize(650,300);
        setBorder(BorderFactory.createTitledBorder("修改户头"));

        Label ll=new Label("请输入要修改的户主身份证号:");
        JTextField jt=new JTextField(10);
        Button bc=new Button("查找");
        JPanel jp=new JPanel();
        jp.add(ll);jp.add(jt);jp.add(bc);
        add(jp);

        t1 = new JTextField(10);
        l1=new Label("身份证号");
        t2 = new JTextField(10);
        l2=new Label("姓名");
        t3 = new JTextField(10);
        l3=new Label("性别");
        t4 = new JTextField(10);
        l4=new Label("家庭住址");
        t5 = new JTextField(10);
        l5=new Label("宅基地大小");
        t6 = new JTextField(10);
        l6=new Label("手机号");
        t7=new JTextField(10);
        l7=new Label("家庭人口");
        t8 = new JTextField(10);
        l8=new Label("贫困等级" );

        setLayout(new GridLayout(5,2));
        JPanel p1 = new JPanel();
        JPanel p2 = new JPanel();
        JPanel p3 = new JPanel();
        JPanel p4 = new JPanel();
        JPanel p5 = new JPanel();
        JPanel p6 = new JPanel();
        JPanel p7 = new JPanel();
        JPanel p8 = new JPanel();

        p1.add(l1);p2.add(l2);p3.add(l3);p4.add(l4);p5.add(l5);p6.add(l6);p7.add(l7);p8.add(l8);
        p1.add(t1);p2.add(t2);p3.add(t3);p4.add(t4);p5.add(t5);p6.add(t6);p7.add(t7);p8.add(t8);
        add(p1);add(p2);add(p3);add(p4);add(p5);add(p6);add(p7);add(p8);

        JPanel p9 = new JPanel();
        bOK = new JButton("确认");
        p9.add(bOK); add(p9);

        bc.addActionListener(e->{ //通过ID查找户头然后,将查找到的信息写在文本框中
            Connection connection = myDataBase.getConnection();
            PreparedStatement ps;
            try {
                ps = connection.prepareStatement("select * from t_host where hid=?");
                ps.setString(1, jt.getText());
                ResultSet result = ps.executeQuery();
                while (result.next()) {
                t1.setText(result.getString("hid"));
                t2.setText(result.getString("hname"));
                t3.setText(result.getString("hsex"));
                t4.setText(result.getString("h_haddress"));
                t5.setText(String.valueOf(result.getDouble("harear")));
                t6.setText(String.valueOf(result.getInt("h_phone")));
                t7.setText(String.valueOf(result.getInt("hnumber")));
                t8.setText(result.getString("hpleve"));
                }
                myDataBase.close(ps, connection);
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        });
        bOK.addActionListener(e -> {//修改文本框内容再提交
            String hid=t1.getText();
            String hname=t2.getText();
            String hsex=t3.getText();
            String h_haddress=t4.getText();
            Double harear= Double.valueOf(t5.getText());
            Integer h_hpone= Integer.valueOf(t6.getText());
            Integer hnumber=Integer.valueOf(t7.getText());
            String hpleve=t8.getText();
            String hidd=jt.getText();//修复之前身份证号无法更新问题,获取一个旧的Hid,用来进行查找
            Host host=new Host();
            host.setHid(hid);
            host.setHname(hname);
            host.setHsex(hsex);
            host.setH_haddress(h_haddress);
            host.setHarear(harear);
            host.setH_phone(h_hpone);
            host.setHnumber(hnumber);
            host.setHpleve(hpleve);
            try {
                OperatorDao.updateAll(host,hidd);
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        });
    }
}

2.更新户头功能设计

更新中根据id获得数据在文本框显示,这个功能也可以将其1写入到OperatorJF中,只不过本人比较摆烂,

所以就写在一起了。

  //通过Id更新数据
    public static void updateAll(Host h,String jt) throws SQLException {
        Connection connection = myDataBase.getConnection();
        PreparedStatement ps = connection.prepareStatement("update t_host set hid=?,hname=?,hsex=?,h_haddress=?,harear=?,h_phone=?,hnumber=?,hpleve=? where hid=?");
        ps.setString(1, h.getHid());
        ps.setString(2, h.getHname());
        ps.setString(3, h.getHsex());
        ps.setString(4, h.getH_haddress());
        ps.setDouble(5, h.getHarear());
        ps.setInt(6, h.getH_phone());
        ps.setInt(7, h.getHnumber());
        ps.setString(8, h.getHpleve());
        ps.setString(9,jt);//修复之前身份证号无法更新问题,第九位应该是旧的Hid,不然查找不到,也就无法更新
        int f = ps.executeUpdate();
        System.out.println(f);
        if (f > 0) {
            JOptionPane.showMessageDialog(null, "成功更新数据");
        } else {
            JOptionPane.showMessageDialog(null, "更新数据失败");
        }
    }

 好了,户头的所有功能都已实现完毕,其他的增删改查功能也是类似。

  • 5
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值