完善模糊查询,添加分页功能

一.完善模糊查询

因为打的代码越来越多,为了后期看起来不会很乱,所以今天我们把seach方法删除了
其他改动的比较多
文件创建如下
在这里插入图片描述
实现效果如下
在这里插入图片描述在搜索框里输入a,表里有a的都会显示出现

在这里插入图片描述
我把代码都贴在下面
IConsumerDao中代码

public interface IConsumerDao {
    Consumer findConsumerByname(String name);
    List<Consumer> findAll();
    void delete(int id);
    void add(String name,String password);
    Consumer findConsumerById(int id);
    void update(int id,String name,String password);
    List<Consumer> findByPage(String name,int start,int size);
    int selectCount();
}

ConsumerDaoImpl中代码

public class ConsumerDaoImpl implements IConsumerDao {
    public Consumer findConsumerByname(String name) {
        ResultSet resultSet = null;
        PreparedStatement statement = null;
        Connection connection = null;
        Consumer consumer = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "select * from consumer where name=?";
            statement = connection.prepareStatement(sql);
            statement.setString(1, name);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                consumer = new Consumer();
                consumer.setId(resultSet.getInt(1));
                consumer.setName(name);
                consumer.setPassword(resultSet.getString(3));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeAll(resultSet, statement, connection);
        }
        return consumer;
    }

    @Override
    public List<Consumer> findAll() {
        ResultSet resultSet = null;
        PreparedStatement statement = null;
        Connection connection = null;
        List<Consumer> consumers = new ArrayList<>();
        try {
            connection = DBUtil.getConnection();
            String sql = "select * from consumer";
            statement = connection.prepareStatement(sql);

            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                Consumer consumer = new Consumer();
                consumer.setId(resultSet.getInt(1));
                consumer.setName(resultSet.getString(2));
                consumer.setPassword(resultSet.getString(3));
                consumers.add(consumer);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeAll(resultSet, statement, connection);
        }
        return consumers;
    }

    @Override
    public void delete(int id) {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = DBUtil.getConnection();
            statement = connection.prepareStatement("delete from consumer where id=?");
            statement.setInt(1, id);
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeAll(null, statement, connection);
        }
    }

    @Override
    public void add(String name, String password) {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = DBUtil.getConnection();
            statement = connection.prepareStatement("insert into consumer(name,password) values (?,?)");
            statement.setString(1, name);
            statement.setString(2, password);
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeAll(nul
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值