DAO模型的增删改查



import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;


public class DAO {
//添加咖啡信息


    public void add(Login xiugai) {
        String sql = "insert into denglu1 values("
                + xiugai.getSupid() + ",'"
                + xiugai.getSupname() + "','"
                + xiugai.getMima() + "')";
        try (Connection con = SQL.getConnection();
                Statement stem = con.createStatement();) {
            int result = stem.executeUpdate(sql);
            if (result > 0) {
                System.out.println("添加成功");
            } else {
                System.out.println("添加失败");
            }


        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
    //修改已有信息


public void update(Login xiugai) {
        String sql1 = "update denglu1 set id="
                + xiugai.getSupid()
                //+ ",where name='" + xiugai.getSupname()
                + ",password='" + xiugai.getMima()
                 + "',where name='" + xiugai.getSupname()
                + "'";
        try (Connection con = SQL.getConnection();
                Statement stem = con.createStatement();) {
            int result = stem.executeUpdate(sql1);
            if (result > 0) {
                System.out.println("修改成功");
            } else {
                System.out.println("修改失败");
            }


        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }


    //删除指定信息
    public void remove(String xiugaiName) {
        String sql2 = "delete from denglu1 where name='" + xiugaiName + "'";
        try (Connection con = SQL.getConnection();
                Statement stem = con.createStatement();) {
            int result = stem.executeUpdate(sql2);
            if (result > 0) {
                System.out.println("删除成功");
            } else {
                System.out.println("删除失败");
            }


        } catch (SQLException ex) {
            ex.printStackTrace();


        }
    }
//查看所有信息


 public List<Login> findAll() {
        List<Login> denglu1 = new ArrayList<>();
        String sql3 = "select * from denglu1";
        try (Connection con = SQL.getConnection();
                Statement stem = con.createStatement();
                ResultSet re = stem.executeQuery(sql3)) {
            while (re.next()) {
                Login xiu = new Login();
                xiu.setSupid(re.getInt("id"));
                xiu.setSupname(re.getString("name"));


                xiu.setMima(re.getString("password"));
                denglu1.add(xiu);
            }


        } catch (SQLException ex) {


        }
        return denglu1;
    }


    //按照名称查询指定信息
    public Login find(String xiugaiName) {
        String sql4 = "select id,name,password from denglu1 where name='" + xiugaiName + "'";
        Login xiu = null;
        try (Connection con = SQL.getConnection();
                Statement stem = con.createStatement();) {
            ResultSet re = stem.executeQuery(sql4);
            while (re.next()) {
                xiu = new Login();
                xiu.setSupname(re.getString("name"));
                xiu.setSupid(re.getInt("id"));
                xiu.setMima(re.getString("password"));


            }


        } catch (SQLException ex) {


        }
        return xiu;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值