How to determine type of the sql query for jdbcTemplate in JAVA?

Pawan Patil :

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

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

String sql ="insert/update sql query";
jdbcTemplate.update(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 method.

How to determine type of sql query in java effectively?

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

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

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.

Joop Eggen :

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");

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=128100&siteId=1