当前位置:网站首页>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
边栏推荐
- Install and configure Jenkins
- JS bright blind your eyes date selector
- Using tensorboard to visualize the training process in pytoch
- Ecotone technology has passed ISO27001 and iso21434 safety management system certification
- Coding devsecops helps financial enterprises run out of digital acceleration
- The difference between abstract classes and interfaces in PHP (PHP interview theory question)
- 亿咖通科技通过ISO27001与ISO21434安全管理体系认证
- Cartoon: programmers don't repair computers!
- Magic methods and usage in PHP (PHP interview theory questions)
- Aike AI frontier promotion (7.5)
猜你喜欢

IPv6与IPv4的区别 网信办等三部推进IPv6规模部署
![P6183 [USACO10MAR] The Rock Game S](/img/f4/d8c8763c27385d759d117b515fbf0f.png)
P6183 [USACO10MAR] The Rock Game S

Ten billion massage machine blue ocean, difficult to be a giant

华为哈勃化身硬科技IPO收割机

Microframe technology won the "cloud tripod Award" at the global Cloud Computing Conference!

Change multiple file names with one click

百亿按摩仪蓝海,难出巨头

美团优选管理层变动:老将刘薇调岗,前阿里高管加盟

Interview shock 62: what are the precautions for group by?
![[12 classic written questions of array and advanced pointer] these questions meet all your illusions about array and pointer, come on!](/img/d2/c0a19c85b2011ecd07c9944d996c4d.png)
[12 classic written questions of array and advanced pointer] these questions meet all your illusions about array and pointer, come on!
随机推荐
Easyocr character recognition
Visual task scheduling & drag and drop | scalph data integration based on Apache seatunnel
mapper. Comments in XML files
Does maxcompute have SQL that can query the current storage capacity (KB) of the table?
Bugku easy_ nbt
Surpass palm! Peking University Master proposed diverse to comprehensively refresh the NLP reasoning ranking
Redis distributed lock principle and its implementation with PHP (2)
Stm32+bh1750 photosensitive sensor obtains light intensity
爱可可AI前沿推介(7.5)
Mysql---- function
Aike AI frontier promotion (7.5)
Using tensorboard to visualize the training process in pytoch
Photoshop插件-动作相关概念-ActionList-ActionDescriptor-ActionList-动作执行加载调用删除-PS插件开发
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
Select sort and bubble sort
CODING DevSecOps 助力金融企业跑出数字加速度
IPv6与IPv4的区别 网信办等三部推进IPv6规模部署
Bugku's steganography
mapper.xml文件中的注释
Ecotone technology has passed ISO27001 and iso21434 safety management system certification