接口,实现类

这篇博客介绍了如何在Java中使用DAO和Service层来处理数据操作。它定义了一个CustomerDAO接口及其实现类CustomerDAOImpl,包含了插入、查询等数据库操作。同时,创建了CustomerService接口和实现类CustomerServiceImpl,它们调用了DAO层的方法来实现用户登录、注册等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

建立一个dao接口,建立一个dao实现类

public interface CustomerDao {

//插入方法,传一个Customer对象进去
    public void insert(Customer custname);

//根据名字查询
    public Customer selectByName(String custname);

//根据用户名和密码查询
    public Customer selectByNamePwd(String custname,String pwd);

//查询全部
    List<Customer> selectAll();
}

 

建立一个dao实现类

public class CustomerDAOImpl implements CustomerDao{
    public void insert(Customer cust) {
        Connection conn = JDBCConnectionFactory.getConnection();
        String sql = "insert into customer values(?,?,?,?)";
        try {
            PreparedStatement ps= conn.prepareStatement(sql);
            ps.setString(1, cust.getCustname());
            ps.setString(2, cust.getPwd());
            ps.setInt(3,cust.getAge());
            ps.setString(4,cust.getAddress());
            ps.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        
    }

    public Customer selectByName(String custname) {
        Customer cust = null;
        Connection conn = JDBCConnectionFactory.getConnection();
        String sql = "select * from customer where custname=?";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, custname);
            ResultSet rs = ps.executeQuery();
            if(rs.next()){
                cust = new Customer(rs.getString(1),rs.getString(2),rs.getInt(3),rs.getString(4));
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return cust;
    }

    public Customer selectByNamePwd(String custname, String pwd) {
        // TODO Auto-generated method stub
        Customer cust = null;
        Connection conn = JDBCConnectionFactory.getConnection();
        String sql = "select * from customer where custname=? and pwd = ?";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, custname);
            ps.setString(2, pwd);
            ResultSet rs = ps.executeQuery();
            if(rs.next()){
                cust = new Customer(rs.getString(1),rs.getString(2),rs.getInt(3),rs.getString(4));
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        return cust;
    }

    public List<Customer> selectAll() {
        List<Customer> list = new ArrayList<Customer>();
        Connection conn = JDBCConnectionFactory.getConnection();
        String sql = "select custname,age,address from customer";
        try {
            Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery(sql);
            while(rs.next()){
                list.add(new Customer(rs.getString(1),null,rs.getInt(2),rs.getString(3)));
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        return list;
    }

}
 

建立一个service接口

public interface CustomerService {

//登录
    public boolean login(String  custname,String pwd);

//注册
    public boolean register(Customer cust)throws RegisterException;

//查看个人信息
    public Customer viewPersonal(String custname);
    public List<Customer> viewAll();
}
 

建立一个service实现类

public class CustomerServiceImpl implements CustomerService{
    private CustomerDao dao;
    public boolean login(String custname, String pwd) {
        Customer cust = dao.selectByNamePwd(custname, pwd);
        if(cust!=null){
            return true;
        }else{
            return false;
        }
        // TODO Auto-generated method stub
    
    }

    public boolean register(Customer cust) throws RegisterException {
        // TODO Auto-generated method stub
        Customer c = dao.selectByName(cust.getCustname());
        if(c==null){
            dao.insert(cust);
            return true;
        }
        return false;
    }

    public Customer viewPersonal(String custname) {
        // TODO Auto-generated method stub
        return dao.selectByName(custname);
        
    }

    public List<Customer> viewAll() {
        // TODO Auto-generated method stub
        return dao.selectAll();
    }

}
service层调用dao层接口

dao接口写方法,接口实现类 继承接口,写具体实现方法

service接口写方法,接口实现类的方法中调用dao层方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值