为了账号安全,请及时绑定邮箱和手机立即绑定

java mysql jdbc 增删改查

标签:
Java
    public boolean doCreate(Member member) throws Exception {
        String sql = "insert into member01(mid,name,age,phone,birthday,note) values(?,?,?,?,?,?)";
        this.preparedStatement = this.connection.prepareStatement(sql);
        this.preparedStatement.setString(1, member.getMid());
        this.preparedStatement.setString(2, member.getName());
        this.preparedStatement.setInt(3, member.getAge());
        this.preparedStatement.setString(4, member.getPhone());
        this.preparedStatement.setDate(5, new java.sql.Date(member.getBrithday().getTime()));
        this.preparedStatement.setString(6, member.getNote());
        return this.preparedStatement.executeUpdate() > 0;
    }
    public boolean doUpdate(Member member) throws Exception {
        String sql = "update member01 set name=?,age=?,phone=?,birthday=?,note=? where mid=?";
        this.preparedStatement = this.connection.prepareStatement(sql);
        this.preparedStatement.setString(1, member.getName());
        this.preparedStatement.setInt(2, member.getAge());
        this.preparedStatement.setString(3, member.getPhone());
        this.preparedStatement.setDate(4, new java.sql.Date(member.getBrithday().getTime()));
        this.preparedStatement.setString(5, member.getNote());
        this.preparedStatement.setString(6, member.getMid());
        return this.preparedStatement.executeUpdate() > 0;
    }
    public boolean doRemoveBatch(Set<String> ids) throws Exception {
        StringBuffer buffer = new StringBuffer();
        buffer.append("delete from member01 where mid in(");
        Iterator<String> iterator = ids.iterator();
        while (iterator.hasNext()) {
            buffer.append("'").append(iterator.next()).append(")");
        }
        buffer.delete(buffer.length() - 1, buffer.length()).append("");
        this.preparedStatement = this.connection.prepareStatement(buffer.toString());
        return this.preparedStatement.executeUpdate() == ids.size();
    }
    public Member findById(String id) throws Exception {
        Member member = null;
        String sql = "select mid,name,age,phone,birthday,note from member01 where mid=?";
        this.preparedStatement = this.connection.prepareStatement(sql);
        this.preparedStatement.setString(1, id);
        ResultSet resultSet = this.preparedStatement.executeQuery();
        if (resultSet.next()) {
            member = new Member();
            member.setMid(resultSet.getString(1));
            member.setName(resultSet.getString(2));
            member.setAge(resultSet.getInt(3));
            member.setPhone(resultSet.getString(4));
            member.setBrithday(resultSet.getDate(5));
            member.setNote(resultSet.getString(6));
        }
        return member;
    }
    public List<Member> findByAll() throws Exception {
        List<Member> members = new ArrayList<>();
        String sql = "select mid,name,age,phone,birthday,note from member01";
        this.preparedStatement = this.connection.prepareStatement(sql);
        ResultSet resultSet = this.preparedStatement.executeQuery();
        while (resultSet.next()) {
            Member member = new Member();
            member.setMid(resultSet.getString(1));
            member.setName(resultSet.getString(2));
            member.setAge(resultSet.getInt(3));
            member.setPhone(resultSet.getString(4));
            member.setBrithday(resultSet.getDate(5));
            member.setNote(resultSet.getString(6));
            members.add(member);
        }
        return members;
    }
点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 1
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消