当前位置:网站首页>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
- Super wow fast row, you are worth learning!
- I want to inquire about how to ensure data consistency when a MySQL transaction updates multiple tables?
- [detailed explanation of Huawei machine test] character statistics and rearrangement
- Visual task scheduling & drag and drop | scalph data integration based on Apache seatunnel
- 1330: [example 8.3] minimum steps
- Selection and use of bceloss, crossentropyloss, sigmoid, etc. in pytorch classification
- mapper. Comments in XML files
- What are the domestic formal futures company platforms in 2022? How about founder metaphase? Is it safe and reliable?
- maxcompute有没有能查询 表当前存储容量的大小(kb) 的sql?
猜你喜欢
CODING DevSecOps 助力金融企业跑出数字加速度
Bugku's Ping
30岁汇源,要换新主人了
Microframe technology won the "cloud tripod Award" at the global Cloud Computing Conference!
"Sequelae" of the withdrawal of community group purchase from the city
Garbage collection mechanism of PHP (theoretical questions of PHP interview)
Live broadcast preview | how to implement Devops with automatic tools (welfare at the end of the article)
Ctfshow web entry command execution
Creation and optimization of MySQL index
市值蒸发超百亿美元,“全球IoT云平台第一股”赴港求生
随机推荐
MySQL之CRUD
Aike AI frontier promotion (7.5)
mapper.xml文件中的注释
Anaconda uses China University of science and technology source
JMeter performance test: serveragent resource monitoring
What are CSRF, XSS, SQL injection, DDoS attack and timing attack respectively and how to prevent them (PHP interview theory question)
Magic methods and usage in PHP (PHP interview theory questions)
可转债打新在哪里操作开户是更安全可靠的呢
Huawei Hubble incarnation hard technology IPO harvester
当代人的水焦虑:好水究竟在哪里?
What are the domestic formal futures company platforms in 2022? How about founder metaphase? Is it safe and reliable?
Common MySQL interview questions
729. My schedule I: "simulation" & "line segment tree (dynamic open point) &" block + bit operation (bucket Division) "
两个BI开发,3000多张报表?如何做的到?
I collect multiple Oracle tables at the same time. After collecting for a while, I will report that Oracle's OGA memory is exceeded. Have you encountered it?
Bugku's Ping
go学习 ------jwt的相关知识
[JVM] operation instruction
Easyocr character recognition
maxcompute有没有能查询 表当前存储容量的大小(kb) 的sql?