当前位置:网站首页>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
边栏推荐
猜你喜欢
CPU design related notes
12 MySQL interview questions that you must chew through to enter Alibaba
Bugku's steganography
Common redis data types and application scenarios
Coding devsecops helps financial enterprises run out of digital acceleration
P1451 calculate the number of cells / 1329: [example 8.2] cells
Drive brushless DC motor based on Ti drv10970
Aike AI frontier promotion (7.5)
【jvm】运算指令
Live broadcast preview | how to implement Devops with automatic tools (welfare at the end of the article)
随机推荐
【jvm】运算指令
MySQL之CRUD
Surpass palm! Peking University Master proposed diverse to comprehensively refresh the NLP reasoning ranking
STM32+BH1750光敏传感器获取光照强度
【华为机试真题详解】欢乐的周末
Type declaration of all DOM elements in TS
How to solve the problem of garbled code when installing dependency through NPM or yarn
CODING DevSecOps 助力金融企业跑出数字加速度
Creation and use of thymeleaf template
美团优选管理层变动:老将刘薇调岗,前阿里高管加盟
[C question set] of Ⅷ
Talk about your understanding of microservices (PHP interview theory question)
Your childhood happiness was contracted by it
计算中间件 Apache Linkis参数解读
Database learning - Database Security
Cartoon: programmers don't repair computers!
Machine learning notes - gray wolf optimization
[detailed explanation of Huawei machine test] happy weekend
Using tensorboard to visualize the training process in pytoch
Run faster with go: use golang to serve machine learning