当前位置:网站首页>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;
}
}

边栏推荐
- Unity资源管理系列:Unity 框架如何做好资源管理
- 数字经济时代的开源数据库创新 | 2022开放原子全球开源峰会数据库分论坛圆满召开
- [C language] Detailed explanation of operators
- WPF WPF 】 【 the depth resolution of the template
- Industry landing presents new progress | 2022 OpenAtom Global Open Source Summit OpenAtom OpenHarmony sub-forum was successfully held
- Blockbuster | foundation for platinum, gold, silver gave nameboards donors
- sql语句之多表查询
- .NET-6.WinForm2.NanUI学习和总结
- CentOS7 install MySQL graphic detailed tutorial
- [py script] batch binarization processing images
猜你喜欢

From scratch, a mirror to the end, a pure system builds a grasscutter (Grasscutter)

三道leetcode上的oj题

Musk talks to the "virtual version" of Musk, how far is the brain-computer interaction technology from us

Unity教程:URP渲染管线实战教程系列【1】

MySQL优化:从十几秒优化到三百毫秒

XSS靶场(三)prompt to win

城市内涝及桥洞隧道积水在线监测系统

ERROR 2003 (HY000) Can't connect to MySQL server on 'localhost3306' (10061)Solution

mysql存储过程

专访 | 阿里巴巴首席技术官程立:云+开源共同形成数字世界的可信基础
随机推荐
Sql解析转换之JSqlParse完整介绍
On-line monitoring system for urban waterlogging and water accumulation in bridges and tunnels
ES source code API call link source code analysis
MySQL事务(transaction) (有这篇就足够了..)
Industry landing presents new progress | 2022 OpenAtom Global Open Source Summit OpenAtom OpenHarmony sub-forum was successfully held
Unity shader forge和自带的shader graph,有哪些优缺点?
[debug highlights] Expected input batch_size (1) to match target batch_size (0)
CentOS7 —— yum安装mysql
DVWA installation tutorial (understand what you don't understand · in detail)
【云原生】DevOps(五):集成Harbor
Go语学习笔记 - 处理超时问题 - Context使用 | 从零开始Go语言
DVWA shooting range environment construction
Unity框架设计系列:Unity 如何设计网络框架
Hand in hand to realize the picture preview plug-in (3)
Centos7 install mysql5.7 steps (graphical version)
ERROR 1819 (HY000) Your password does not satisfy the current policy requirements
【debug锦集】Expected input batch_size (1) to match target batch_size (0)
MySQL忘记密码怎么办
VScode+ESP32 quickly install ESP-IDF plugin
Duplicate entry 'XXX' for key 'XXX.PRIMARY' solution.