当前位置:网站首页>queryRunner. Query method
queryRunner. Query method
2022-07-05 15:16:00 【Add gold development】
public Map<String, Object> getUserByUserId_MAP(int userId){
Map<String, Object> map = null;
// Get the connection
Connection conn = DBConnectionUtil.getConnection();
// establish SQL Execution tools
QueryRunner queryRunner = new QueryRunner();
// Splicing SQL
String sql = "select * from user where userId = ?";
Object params[] = {userId};
try {
map = queryRunner.query(conn, sql, new MapHandler(), params);
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBConnectionUtil.closeConnection(conn);
}
return map;
}
apache.commons.dbutils.QueryRunner.query Methodical handler Parameters
For the first time to use apache.commons.dbutils.QueryRunner People who visit the database will have a question ..
String url = "jdbc:mysql://localhost:3306/test"; Connection con = DriverManager.getConnection(url, "root", "pwd"); String sql = "select * from myTable"; QueryRunner qr = new QueryRunner(); qr.query(con, sql, handler); //con It's a database connection , sql Is a query statement , handler What is it? ?
QueryRunner.query The return value of the method is a Object object , The Object Object holds the data obtained from the database ,
It is the type of ( Types that can be explicitly converted ) Is called by query Methods handler Parameters determine .
ResultSetHandler handler = new ArrayHandler(); Object obj = qr.query(con, sql, handler); Object[] arr = (Object[])obj; ResultSetHandler handler = new ArrayListHandler(); Object obj = qr.query(con, sql, handler); List list = (List)obj;
in other words , Save data Object The explicit conversion type of the object is handler Parameters determine .
Here is handler Comparison table of parameters and return object types :
AbstractListHandler -- Back to multiple lines List The abstract class of
ArrayHandler -- Return a line of Object[]
ArrayListHandler -- return List, Each line is Object[]
BeanHandler -- Return to the first Bean object
BeanListHandler -- return List, Each line is Bean
ColumnListHandler -- Returns a list of List
KeyedHandler -- return Map, See code for details
MapHandler -- Returns a single Map
MapListHandler -- return List, Each line is Map
ScalarHandler -- Returns the first value of the column
边栏推荐
- P6183 [USACO10MAR] The Rock Game S
- Cartoon: programmers don't repair computers!
- MySQL----函数
- Un week - end heureux
- Talk about your understanding of microservices (PHP interview theory question)
- Want to ask the big guy, is there any synchronization from Tencent cloud Mysql to other places? Binlog saved by Tencent cloud MySQL on cos
- 超越PaLM!北大碩士提出DiVeRSe,全面刷新NLP推理排行榜
- mapper. Comments in XML files
- [detailed explanation of Huawei machine test] happy weekend
- The difference between SQL Server char nchar varchar and nvarchar
猜你喜欢

Change multiple file names with one click

Bugku's steganography

MySQL之CRUD

DVWA range clearance tutorial
![P6183 [USACO10MAR] The Rock Game S](/img/f4/d8c8763c27385d759d117b515fbf0f.png)
P6183 [USACO10MAR] The Rock Game S

超越PaLM!北大硕士提出DiVeRSe,全面刷新NLP推理排行榜

Ecotone technology has passed ISO27001 and iso21434 safety management system certification

数据库学习——数据库安全性

Thymeleaf uses background custom tool classes to process text

Select sort and bubble sort
随机推荐
Un week - end heureux
1330: [example 8.3] minimum steps
Behind the ultra clear image quality of NBA Live Broadcast: an in-depth interpretation of Alibaba cloud video cloud "narrowband HD 2.0" technology
Calculate weight and comprehensive score by R entropy weight method
I want to inquire about how to ensure data consistency when a MySQL transaction updates multiple tables?
爱可可AI前沿推介(7.5)
Huawei Hubble incarnation hard technology IPO harvester
计算中间件 Apache Linkis参数解读
B站做短视频,学抖音死,学YouTube生?
Redis distributed lock principle and its implementation with PHP (1)
我这边同时采集多个oracle表,采集一会以后,会报oracle的oga内存超出,大家有没有遇到的?
漫画:程序员不是修电脑的!
Reconnaissance des caractères easycr
Stop B makes short videos, learns Tiktok to die, learns YouTube to live?
The difference between SQL Server char nchar varchar and nvarchar
【jvm】运算指令
GPS原始坐标转百度地图坐标(纯C代码)
华为哈勃化身硬科技IPO收割机
Dark horse programmer - software testing -10 stage 2-linux and database -44-57 why learn database, description of database classification relational database, description of Navicat operation data, de
Change multiple file names with one click