package com.itheima.dbutils;
import com.itheima.domain.Product;
import com.itheima.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ColumnListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import java.sql.SQLException;
import java.util.List;
/**
* 实现查询
* 1: QueryRunner构造方法
* QueryRunner(DataSource ds) 传递接口实现类
* 接口的实现类,数据源,连接池
* QueryRunner自动从连接池取出连接使用
*
* 2: JavaBean对象
* 一个类包含私有成员变量,包含get/set方法,包含无参数构造
* 称为JavaBean对象
*
* QueryRunner类的方法 query执行查询 Select语句
* query(String sql, ResultSetHandler<T> rsh, Object... params)
* 参数 sql :传递执行的select语句
* 参数 rsh: 是接口ResultSetHandler的实现类对象
* ResultSetHandler:表示的是查询后的结果集
* 接口实现类:
* BeanHandler : 查询数据表的第一行数据,存储到JavaBean对象
* BeanListHandler : 查询数据表,数据的每一行存储到JavaBean对象,多个JavaBean对象,存储到List集合
* ColumnListHandler : 查询数据表中的一个列的数据,存储到List集合
* ScalarHandler : 适合单值查询,结果集只有一个值 count(*)
*
*
* 参数params: SQL语句中问号占位符参数
*
*/
public class DBUtilsDemo02 {
public static void main(String[] args) throws SQLException {
scalarHandler();
}
/**
* ScalarHandler : 适合单值查询,结果集只有一个值 count(*)
* 查询的结果集只有一个值,例如聚合函数 avg max min sum
* select pname from product where pid = 1;
*/
public static void scalarHandler()throws SQLException{
//创建QueryRunner对象,构造方法,传递DataSource接口实现类
QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
//查询表中共有多少条数据
String sql = "select count(pid) from product";
Long object = qr.query(sql,new ScalarHandler<Long>());
System.out.println(object);
}
/**
* ColumnListHandler : 查询数据表中的一个列的数据,存储到List集合
*/
public static void columnListHandler()throws SQLException{
//创建QueryRunner对象,构造方法,传递DataSource接口实现类
QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
//拼写查询语句
String sql = "select * from product ";
//执行查询语句,传递的对象是ColumnListHandler,要一个列的数据
//ColumnListHandler(String columnName)传递参数,是列名
List<Object> list = qr.query(sql,new ColumnListHandler<Object>("pname"));
for(Object o : list){
System.out.println(o);
}
}
/**
* BeanListHandler : 查询数据表,数据的每一行存储到JavaBean对象,
* 多个JavaBean对象,存储到List集合
* 查询不到数据,集合是存在的,长度=0
*/
public static void beanListHandler()throws SQLException{
//创建QueryRunner对象,构造方法,传递DataSource接口实现类
QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
//拼写查询语句
String sql = "select * from product ";
// 方法query执行SQL语句,传递结果集处理对象 BeanListHandler(传递JavaBean类的class对象)
List<Product> list = qr.query(sql,new BeanListHandler<Product>(Product.class));
for(Product product :list){
System.out.println(product);
}
}
/**
* BeanHandler : 查询数据表的第一行数据,存储到JavaBean对象
* JavaBean对象是 Product
*/
public static void beanHandler()throws SQLException{
//创建QueryRunner类对象,有参数构造方法,传递DataSource接口实现类
QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
//拼写查询语句
String sql = "select * from product ";
//方法query执行SQL语句
//传递结果集BeanHandler(Class<T> type) 传递JavaBean类的class文件对象
Product product = qr.query(sql,new BeanHandler<Product>(Product.class));
System.out.println(product);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
03-MySQL数据库源码
共286个文件
png:94个
html:68个
jpg:41个
需积分: 0 1 下载量 154 浏览量
2023-04-26
22:59:47
上传
评论
收藏 30.16MB ZIP 举报
温馨提示
03-MySQL数据库源码
资源推荐
资源详情
资源评论
收起资源包目录
03-MySQL数据库源码 (286个子文件)
stylesheet.css 11KB
titlebar.gif 10KB
background.gif 2KB
titlebar_end.gif 849B
tab.gif 291B
BaseResultSetHandler.html 318KB
index-all.html 200KB
AsyncQueryRunner.html 80KB
QueryRunner.html 76KB
SqlNullCheckedResultSet.html 58KB
AbstractQueryRunner.html 43KB
ResultSetHandler.html 42KB
DbUtils.html 34KB
BeanProcessor.html 28KB
KeyedHandler.html 27KB
BasicRowProcessor.html 25KB
BeanMapHandler.html 25KB
ProxyFactory.html 24KB
ResultSetIterator.html 21KB
AbstractKeyedHandler.html 20KB
QueryLoader.html 19KB
MapListHandler.html 17KB
AsyncQueryRunner.QueryCallableStatement.html 17KB
BeanListHandler.html 17KB
StringTrimmedResultSet.html 17KB
ColumnListHandler.html 17KB
AsyncQueryRunner.UpdateCallableStatement.html 17KB
AbstractListHandler.html 16KB
MapHandler.html 16KB
RowProcessor.html 16KB
BeanHandler.html 16KB
AsyncQueryRunner.BatchCallableStatement.html 16KB
ArrayListHandler.html 16KB
GenerousBeanProcessor.html 15KB
ScalarHandler.html 15KB
ArrayHandler.html 15KB
RowProcessor.html 14KB
overview-tree.html 13KB
deprecated-list.html 12KB
ResultSetHandler.html 10KB
package-summary.html 10KB
package-summary.html 10KB
package-tree.html 9KB
package-use.html 9KB
package-tree.html 9KB
help-doc.html 9KB
BeanProcessor.html 7KB
AbstractQueryRunner.html 7KB
QueryRunner.html 7KB
BasicRowProcessor.html 7KB
ProxyFactory.html 6KB
QueryLoader.html 6KB
package-use.html 6KB
package-summary.html 6KB
package-tree.html 5KB
allclasses-frame.html 5KB
AsyncQueryRunner.UpdateCallableStatement.html 5KB
overview-summary.html 5KB
constant-values.html 5KB
AsyncQueryRunner.BatchCallableStatement.html 5KB
AsyncQueryRunner.QueryCallableStatement.html 5KB
GenerousBeanProcessor.html 4KB
BaseResultSetHandler.html 4KB
ResultSetIterator.html 4KB
AsyncQueryRunner.html 4KB
allclasses-noframe.html 4KB
DbUtils.html 4KB
package-use.html 4KB
index.html 3KB
package-frame.html 2KB
package-frame.html 2KB
overview-frame.html 991B
package-frame.html 979B
day19.iml 2KB
day19_transaction.iml 1001B
day18.iml 712B
druid-1.1.6.jar 2.56MB
druid-1.1.6.jar 2.56MB
lombok-1.18.8.jar 1.65MB
lombok-1.18.8.jar 1.65MB
mysql-connector-java-5.1.46.jar 981KB
mysql-connector-java-5.1.46.jar 981KB
mysql-connector-java-5.1.46.jar 981KB
c3p0-0.9.1.2.jar 596KB
c3p0-0.9.1.2.jar 596KB
commons-dbutils-1.6.jar 76KB
commons-dbutils-1.6.jar 76KB
DBUtilsDemo02.java 4KB
JdbcDemo.java 3KB
JdbcUtils.java 2KB
AccountService.java 2KB
DBUtilsDemo.java 2KB
JdbcDemo06.java 2KB
JdbcTransaction.java 2KB
JdbcDemo02.java 2KB
JdbcDemo03.java 2KB
DruidUtils.java 2KB
DruidUtils.java 2KB
JdbcDemo04.java 2KB
AccountDao.java 1KB
共 286 条
- 1
- 2
- 3
资源评论
狂浪生刚
- 粉丝: 0
- 资源: 12
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 电力场景设备漏油检测数据集VOC+YOLO格式338张1类别.7z
- 基于yolov8+pyqt5实现精美界面支持图片视频和摄像检测源码.zip
- 用C语言为母亲节献上一份特别的祝福.zip
- LCD1602液晶显示屏的深入探索与实用指南.zip
- 基于Matlab人脸肤色定理的教师人数统计+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab霍夫曼变换的表盘读数识别+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab火灾烟雾检测源码带GUI界面+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab的恶劣天气交通标志识别系统+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于MATLAB的霍夫曼变换的表盘示数识别+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab的车道线识别系统 +源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功