当前位置:网站首页>Apache DButils使用注意事项--with modifiers “public“
Apache DButils使用注意事项--with modifiers “public“
2022-07-31 04:56:00 【Alex抱着爆米花】
报错信息
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;
}
}

边栏推荐
- Go language study notes - dealing with timeout problems - Context usage | Go language from scratch
- 110道 MySQL面试题及答案 (持续更新)
- [Cloud Native] DevOps (5): Integrating Harbor
- Go中间件
- View source and switch mirrors in two ways: npm and nrm
- 【C语言】操作符详解
- PCL 计算点云坐标最值及其索引
- input输入框展示两位小数之precision
- Minio上传文件ssl证书不受信任
- Go语学习笔记 - 处理超时问题 - Context使用 | 从零开始Go语言
猜你喜欢

【R语言】【3】apply,tapply,lapply,sapply,mapply与par函数相关参数

110道 MySQL面试题及答案 (持续更新)

A complete introduction to JSqlParse of Sql parsing and conversion

Blockbuster | foundation for platinum, gold, silver gave nameboards donors

Lua,ILRuntime, HybridCLR(wolong)/huatuo hot update comparative analysis

12个MySQL慢查询的原因分析

专访 | 阿里巴巴首席技术官程立:云+开源共同形成数字世界的可信基础

mysql使用on duplicate key update批量更新数据

Three oj questions on leetcode

MySQL database installation (detailed)
随机推荐
开源社区三十年 | 2022开放原子全球开源峰会开源社区三十年专题活动圆满召开
MySQL优化之慢日志查询
npm、nrm两种方式查看源和切换镜像
Go语学习笔记 - 处理超时问题 - Context使用 | 从零开始Go语言
Gaussian distribution and its maximum likelihood estimation
centos7安装mysql5.7步骤(图解版)
Interview | Cheng Li, CTO of Alibaba: Cloud + open source together form a credible foundation for the digital world
Puzzle Game Level Design: Reverse Method--Explaining Puzzle Game Level Design
ABC D - Distinct Trio (Number of k-tuples
Visual studio shortcuts that improve efficiency, summary (updated from time to time)
Unity resources management series: Unity framework how to resource management
CentOS7 安装MySQL 图文详细教程
Summary of MySQL common interview questions (recommended collection!!!)
扫雷小游戏——C语言
Centos7 install mysql5.7 steps (graphical version)
1. 获取数据-requests.get()
.NET-6.WinForm2.NanUI learning and summary
ES 源码 API调用链路源码分析
SQL行列转换
[Cloud Native] DevOps (5): Integrating Harbor