Apache DButils使用注意事项--with modifiers “public“

报错信息

Exception in thread "main" java.sql.SQLException: Cannot create 
com.hsp.chapter25.Apache.Actor: class org.apache.commons.dbutils.BeanProcessor cannot 
access a member of class com.hsp.chapter25.Apache.Actor with modifiers "public" Query: 
select * from stu where id > ? Parameters: [1]

源代码

在queryRunner.query中给 模板类时报错
可以看到我是将Actor.class放在一起的,根据提示,应该是分开写用pulic修饰

package com.hsp.chapter25.Apache;

import com.hsp.chapter25.Druid.JDBCByDruid;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class Apache {
    public static void main(String[] args) throws SQLException {
        Connection connection = JDBCByDruid.getConnection();
        QueryRunner queryRunner = new QueryRunner();
        String sql = "select * from stu where id > ?";
        List<Actor> query = queryRunner.query(connection, sql, new BeanListHandler<>(Actor.class), 1);
        for (Actor actor : query) {
            System.out.println(actor);
        }
        JDBCByDruid.close(null, null, connection);
    }
}
class Actor {
    private Integer id;
    private String name;

    public Actor() {//一定要给一个无参构造器[反射需要]
    }

    public Actor(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Actor{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

修改

分开写
1、Apache.class

package com.hsp.chapter25.Apache;

import com.hsp.chapter25.Druid.JDBCByDruid;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class Apache {
    public static void main(String[] args) throws SQLException {
        Connection connection = JDBCByDruid.getConnection();
        QueryRunner queryRunner = new QueryRunner();
        String sql = "select * from stu where id > ?";
        List<Actor> query = queryRunner.query(connection, sql, new BeanListHandler<>(Actor.class), 1);
        for (Actor actor : query) {
            System.out.println(actor);
        }
        JDBCByDruid.close(null, null, connection);
    }
}

2、Actor.java
一定要设置设置属性和对应的get,set方法
如果设置了有参构造器,对应的无参构造器也要写,否则报错
默认不写构造器也是可以的

写有参构造器,无参构造器也要写

package com.hsp.chapter25.Apache;

public class Actor {
    private Integer id;
    private String name;

    public Actor() {
    }

    public Actor(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Actor{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

在这里插入图片描述

不写构造器,也是OK的

package com.hsp.chapter25.Apache;

public class Actor {
    private Integer id;
    private String name;

    @Override
    public String toString() {
        return "Actor{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值