当前位置:网站首页>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;
}
}
边栏推荐
- What are the advantages and disadvantages of Unity shader forge and the built-in shader graph?
- Unity教程:URP渲染管线实战教程系列【1】
- 扫雷小游戏——C语言
- 产学研用 共建开源人才生态 | 2022开放原子全球开源峰会教育分论坛圆满召开
- DVWA靶场环境搭建
- Create componentized development based on ILRuntime hot update
- Visual studio shortcuts that improve efficiency, summary (updated from time to time)
- Solved (the latest version of selenium framework element positioning error) NameError: name 'By' is not defined
- On Governance and Innovation | 2022 OpenAtom Global Open Source Summit OpenAnolis sub-forum was successfully held
- 从零开始,一镜到底,纯净系统搭建除草机(Grasscutter)
猜你喜欢
Unity Tutorial: URP Rendering Pipeline Practical Tutorial Series [1]
Musk talks to the "virtual version" of Musk, how far is the brain-computer interaction technology from us
MySQL optimization: from ten seconds to three hundred milliseconds
The Vue project connects to the MySQL database through node and implements addition, deletion, modification and query operations
MySQL优化之慢日志查询
Numpy中np.meshgrid的简单用法示例
【wpf】wpf中的那些模板之深度解析
mysql stored procedure
行业落地呈现新进展 | 2022开放原子全球开源峰会OpenAtom OpenHarmony分论坛圆满召开
Blockbuster | foundation for platinum, gold, silver gave nameboards donors
随机推荐
MySQL常见面试题汇总(建议收藏!!!)
MySQL开窗函数
SQL行列转换
MySQL优化:从十几秒优化到三百毫秒
Centos7 install mysql5.7
PCL calculates the point cloud coordinate maximum and its index
Moment Pool Cloud quickly installs packages such as torch-sparse and torch-geometric
【云原生】DevOps(五):集成Harbor
STM32HAL库修改Hal_Delay为us级延时
The input input box displays the precision of two decimal places
DVWA靶场环境搭建
信息系统项目管理师核心考点(五十五)配置管理员(CMO)的工作
sql语句-如何以一个表中的数据为条件据查询另一个表中的数据
参考代码系列_1.各种语言的Hello World
Unity资源管理系列:Unity 框架如何做好资源管理
ERROR 1064 (42000) You have an error in your SQL syntax; check the manual that corresponds to your
visual studio 那些提高效率的快捷键,总结(不时更新)
DVWA installation tutorial (understand what you don't understand · in detail)
Unity URP渲染管线摄像机核心机制剖析
Minio上传文件ssl证书不受信任