如何确定JAVA中jdbcTemplate的SQL查询的类型? [英] How to determine type of the sql query for jdbcTemplate in JAVA?

查看:204
本文介绍了如何确定JAVA中jdbcTemplate的SQL查询的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的要求是我想确定sql查询的类型,以便可以相应地选择jdbcTemplate方法.

My requirement is I want to determine type of sql query so that I can choose jdbcTemplate method accordingly.

假设我的sql查询的类型为insert或update,那么我会选择

Suppose my sql query is of type insert or update then I will choose

String sql ="insert/update sql query";
jdbcTemplate.update(sql); 

jdbcTemplate方法,如果选择了我的sql查询的类型,那么我将选择

jdbcTemplate method and if type of my sql query is select then I will choose

String sql ="select sql query";
jdbcTemplate.query(sql);

jdbcTemplate方法.

jdbcTemplate method.

如何在Java中有效确定sql查询的类型?

How to determine type of sql query in java effectively?

哪个jdbcTemplate方法选择我的查询是否同时包含update和select sql语句?例如

Which jdbcTemplate method to choose if my query contains both update and select sql statements? e.g.

更新表设置column_name ="abc",其中column_name在(选择 表中的column_name,其中column_name ="xyz");

update table set column_name="abc" where column_name in (select column_name from table where column_name ="xyz");

更新:实际上,我从Web表单接受我的应用程序用户的sql查询,因此这是出现实际问题的原因,因为用户可以通过表单和表单发布任何类型的sql查询我想根据查询类型选择特定的jdbcTemplate方法.

Update: In reality I am accepting sql queries from the users of my application from the web form so that is the reason where actual problem arises because user can post any type of sql query through form and I want to choose specific jdbcTemplate method depending upon type of query.

推荐答案

SELECT子查询与最终结果无关.因此,该命令的第一个动词表示结果(int updateCount与ResultSet).

SELECT subqueries are irrelevant for the final result. So the command, the first verb is indicative of the result (int updateCount vs. ResultSet).

boolean isSqlSelect = sql.toUpperCase().startsWith("SELECT")
                   || sql.toUpperCase().startsWith("WITH");

这篇关于如何确定JAVA中jdbcTemplate的SQL查询的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆