当前位置:网站首页>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
边栏推荐
- Super wow fast row, you are worth learning!
- The difference between SQL Server char nchar varchar and nvarchar
- Interpretation of Apache linkage parameters in computing middleware
- Surpass palm! Peking University Master proposed diverse to comprehensively refresh the NLP reasoning ranking
- Magic methods and usage in PHP (PHP interview theory questions)
- 华为哈勃化身硬科技IPO收割机
- Ctfshow web entry information collection
- "Sequelae" of the withdrawal of community group purchase from the city
- Mongdb learning notes
- Mysql---- function
猜你喜欢

Super wow fast row, you are worth learning!

可视化任务编排&拖拉拽 | Scaleph 基于 Apache SeaTunnel的数据集成

Ctfshow web entry command execution

Reasons and solutions for redis cache penetration and cache avalanche

Database learning - Database Security

P1451 求细胞数量/1329:【例8.2】细胞

Drive brushless DC motor based on Ti drv10970

Interpretation of Apache linkage parameters in computing middleware

Behind the ultra clear image quality of NBA Live Broadcast: an in-depth interpretation of Alibaba cloud video cloud "narrowband HD 2.0" technology

Stop B makes short videos, learns Tiktok to die, learns YouTube to live?
随机推荐
useMemo,memo,useRef等相关hooks详解
Common interview questions about swoole
Huawei Hubble incarnation hard technology IPO harvester
Can I pass the PMP Exam in 20 days?
Brief introduction of machine learning framework
Creation and use of thymeleaf template
华为哈勃化身硬科技IPO收割机
Shanghai under layoffs
CPU design practice - Chapter 4 practical task 2 using blocking technology to solve conflicts caused by related problems
爱可可AI前沿推介(7.5)
危机重重下的企业发展,数字化转型到底是不是企业未来救星
NBA赛事直播超清画质背后:阿里云视频云「窄带高清2.0」技术深度解读
Under the crisis of enterprise development, is digital transformation the future savior of enterprises
Visual task scheduling & drag and drop | scalph data integration based on Apache seatunnel
MongDB学习笔记
STM32+BH1750光敏传感器获取光照强度
easyOCR 字符識別
Stop B makes short videos, learns Tiktok to die, learns YouTube to live?
Common MySQL interview questions
手写promise与async await