MVC—MySQL增删改查测试

在persist包下的impl中的BandDao实施

public class BandDao implements IBandDao {
    @Override
    public Boolean insert(band band) {
        boolean ret = false;

        try {
            Connection connn =MySqlDbUtil.getConnection();
            String sql ="INSERT INTO band(name,remark,status) VALUES(?,?,?)";
            PreparedStatement stmt = connn.prepareStatement(sql);

            stmt.setString(1,band.getName());
            stmt.setString(2,band.getRemark());
            stmt.setByte(3,band.getStatus());
            int i = stmt.executeUpdate();
            stmt.close();
            if(i>0){
                ret=true;
            }
            stmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ret;
    }

    @Override
    public Boolean delete(Integer id) {
        boolean ret = false;

        try {
            Connection connn =MySqlDbUtil.getConnection();
            String sql ="DELETE FROM  band WHERE id=?";
            PreparedStatement stmt = connn.prepareStatement(sql);

            stmt.setInt(1,id);

            int i = stmt.executeUpdate();
            stmt.close();
            if(i>0){
                ret=true;
            }
            stmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ret;
    }

    @Override
    public Boolean updata(band band) {
        boolean ret = false;

        try {
            Connection connn =MySqlDbUtil.getConnection();
            String sql ="UPDATE  band SET name=?,remark=?,status=? WHERE id=?";
            PreparedStatement stmt = connn.prepareStatement(sql);

            stmt.setString(1,band.getName());
            stmt.setString(2,band.getRemark());
            stmt.setByte(3,band.getStatus());
            stmt.setInt(4,band.getId());
            int i = stmt.executeUpdate();
            stmt.close();
            if(i>0){
                ret=true;
            }
            stmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ret;
    }

    @Override
    public band findById(Integer id) {
            band band =null;
        try {
            Connection conn = MySqlDbUtil.getConnection();
            String sql  = "SELECT * from band WHERE id =?";
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setInt(1,id);
            ResultSet rs = stmt.executeQuery();
            if (rs.next()){
                band = new band();
                band.setId( rs.getInt("id"));
                band.setName(rs.getString("name"));
                band.setRemark(rs.getString("remark"));
                band.setStatus(rs.getByte("status"));


            }
            rs.close();
            stmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return band;
    }

    @Override
    public List<band> findAll() {
        List<band> bands =new ArrayList<>();

        try {
            Connection conn = MySqlDbUtil.getConnection();
            String sql  = "SELECT * FROM band ";
            PreparedStatement stmt = conn.prepareStatement(sql);
            ResultSet rs = stmt.executeQuery();
            while (rs.next()){
                band  band = new band();
                band.setId( rs.getInt("id"));
                band.setName(rs.getString("name"));
                band.setRemark(rs.getString("remark"));
                band.setStatus(rs.getByte("status"));
                bands.add(band);

            }
            rs.close();
            stmt.close();;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return bands;
    }

MVC模式的实现对数据库的增删改查 部分代码: package dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import common.DBConnection; import bean.Contact; public class ContactDAO { public List getAllContact() throws Exception{ Connection conn=DBConnection.getConntion(); PreparedStatement ps=conn.prepareStatement("select * from Contact"); ResultSet rs=ps.executeQuery(); List list = new ArrayList(); while(rs.next()){ int id = rs.getInt("id"); String name = rs.getString("name"); String phone = rs.getString("phone"); String address = rs.getString("address"); Contact c = new Contact(); c.setId(id); c.setName(name); c.setPhone(phone); c.setAddress(address); list.add(c); } rs.close(); ps.close(); conn.close(); return list; } public void addContact(String name,String phone,String address) throws Exception{ String sql = "insert into contact(id,name,phone,address) values(seq_contact.nextval,?,?,?)"; Connection con = DBConnection.getConntion(); PreparedStatement pstmt = con.prepareStatement(sql); pstmt.setString(1, name); pstmt.setString(2, phone); pstmt.setString(3, address); pstmt.executeUpdate(); } public void delContact(int id) throws Exception{ String sql = "delete from contact where id=?"; Connection con = DBConnection.getConntion(); PreparedStatement pstmt = con.prepareStatement(sql); pstmt.setInt(1, id); pstmt.executeUpdate(); } public Contact getContactById(int id) throws Exception{ String sql = "select * from Contact where id=?"; Connection con = DBConnection.getConntion(); PreparedStatement pstmt = con.prepareStatement(sql); pstmt.setInt(1, id); ResultSet rs = pstmt.executeQuery(); Contact c = null; while(rs.next()){ // int id = rs.getInt("id"); String name=rs.getString("name"); String p
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值