原文地址:http://stackoverflow.com/questions/13354158/how-to-query-for-a-liststring-in-jdbctemplate

I'm using springs jdbctemplate and running a query like below:

SELECT COLNAME FROM TABLEA GROUP BY COLNAME

There are no named parameters being passed, however, column name, COLNAME, will be passed by the user.

Questions

  1. Is there a way to have placeholders, like ? for column names? For example SELECT ? FROM TABLEA GROUP BY ?

  2. If I want to simply run the above query and get a List<String> what is the best way?

Currently I'm doing:

List <Map<String, Object>> data = getJdbcTemplate().queryForList(query); for (Map m : data) System.out.println(m.get("COLNAME"));

Answers

Is there a way to have placeholders, like ? for column names? For example SELECT ? FROM TABLEA GROUP BY ?

Use dynamic query as below:

String queryString = "SELECT "+ colName+ " FROM TABLEA GROUP BY "+ colName;

If I want to simply run the above query and get a List what is the best way?

List<String> data = getJdbcTemplate().query(query, new RowMapper<String>(){ public String mapRow(ResultSet rs, int rowNum) throws SQLException { return rs.getString(1); } });

EDIT: To Stop SQL Injection, check for non word characters in the colName as :

          Pattern pattern = Pattern.compile("\\W"); if(pattern.matcher(str).find()){ //throw exception as invalid column name }

 

转载于:https://www.cnblogs.com/davidwang456/p/5056703.html

how to query for a listString in jdbctemplate?--转载相关推荐

  1. spring源码解读之 JdbcTemplate源码

    原文:https://blog.csdn.net/songjinbin/article/details/19857567 在Spring中,JdbcTemplate是经常被使用的类来帮助用户程序操作数 ...

  2. Spring JdbcTemplate Curd

    curd 1. 实现步骤 2. maven dependency 3. curd代码 database: oracle dataSource: alibaba druid 1. 实现步骤 1. 导入s ...

  3. Spring JdbcTemplate2-update和query

    基于Spring 5.2.6.RELEASE Spring JdbcTemplate共两篇 Spring JdbcTemplate1-ddl Spring JdbcTemplate2-update和q ...

  4. JdbcTemplate 查询数据实现的三种方式

    JdbcTemplate 在对数据库进行查询时,通常通过实现下面三种回调接口之一实现 org.springframework.jdbc.core.ResultSetExtractororg.sprin ...

  5. query的list()和iterate()区别 面试题

    query的list()和iterate()区别 1.返回的类型不一样,list返回List,iterate返回iterator 2.查询策略不同. 获取数据的方式不一样,list会直接查询数据库,i ...

  6. 【hdu4010】 Query on The Trees

    http://acm.hdu.edu.cn/showproblem.php?pid=4010 (题目链接) 题意 link cut tree板子 Solution link cut tree 细节 注 ...

  7. FICO辅助工具介绍【LSMW和Query】

    FICO辅助工具介绍[LSMW和Query] (2016-12-13 08:58:34) 转载▼ 标签: lsmw query sq01 sq02 sq03 分类: 权限与其他[原创] 1.1     ...

  8. Ext.get() 和 Ext.query() 组合使用 实现 最灵活的取元素方式

    想要利用ExtJS的库函数对DOM进行各类操作,就要得到Element类型的对象,但是Ext.get()取到的虽然是Element,但是参数只能是id,如果大家对jQuery的selector方式很喜 ...

  9. Java EE学习笔记(四)

    Spring的数据库开发 1.Spring JDBC 1).Spring JDBC模块的作用:Spring的JDBC模块负责数据库资源管理和错误处理,大大简化了开发人员对数据库的操作,使得开发人员可以 ...

最新文章

  1. python字符串单个替换_如何用变量替换列表中的单个字符串?
  2. ef 排序string转int_Java排序算法——基数排序(Radix Sort)
  3. python贪吃蛇毕业设计_【干货|python项目实例——贪吃蛇】- 环球网校
  4. hive 导出json格式 文件_hive中创建hive-json格式的表及查询
  5. pg函数同步数据到mysql_将数据从PostgreSQL复制到MySQL
  6. ApacheCN PythonWeb 译文集 20211028 更新
  7. Beetl学习总结(4)——Web集成
  8. 解决启用GTID binlog新安装完的MySQL提示无法登录
  9. 管家婆破解版7.1 软件下载
  10. usb转rs232驱动无效,已经电脑自动扫描检测硬件改动.设备无法运行错误代码10
  11. 监控导致设备cpu使用率高的原因
  12. wordpress登录美化css,wordpress后台login界面美化
  13. pxe无盘服务器教程,[教程]Synology+PXE挂载iSCSI网络无盘启动Win7(08.04更新)
  14. 长沙理工大学第十二届ACM大赛【9/12】
  15. excel的IRR函数中的预估值有什么用
  16. 列表、元组、字典、集合的基本使用和相关函数,基础的文件操作
  17. 智能名片小程序开发文档概要
  18. 高级气泡图——R语言简单实现
  19. 计算机控制实验室装置,新型自控原理/计算机控制多功能实验装置
  20. Maven 的下载安装和配置

热门文章

  1. c语言设备管理系统实训答辩,C语言设计(力学实验设备管理系统)1答辩.doc
  2. jdbc如果不关闭连接_JDBC基础整理
  3. flex 平铺布局_CSS3 Flex布局(伸缩布局盒模型)学习
  4. 列出mongodb里的所有表的名字,合并所有表到一个大表,用pandas
  5. html连接服务器文件夹,云服务器html链接到文件夹
  6. ++和--操作符分析
  7. wp转shp_【收藏】空间数据格式转换方法
  8. 使用显式Intent向下一个活动传递数据
  9. opencv 采用 matplot 显示图片
  10. 8行代码求解非线性方程