Using NamedParameterJdbcTemplate, how to retrieve record sets from sql server that match a list of regex patterns

StackOverflow https://stackoverflow.com/questions/17604175

Domanda

I'm new to spring framework. I'm trying to retrieve record sets from a SQL server 2005 database table using NamedParameterJdbcTemplate that matches multiple patterns(patterns are stored in an ArrayList) .

The below code retrieves record sets matching a set of values:

List<String> valuesForBuildingPatterns1 = getValuesForBuildingPatterns1();
List<String> valuesForBuildingPatterns2 = getValuesForBuildingPatterns2();
String sqlQuery = "SELECT * FROM sqlServerTable col1 IN (:pattern1) AND col2 IN (:pattern2)";
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("pattern1", valuesForBuildingPatterns1);
params.addValue("pattern2", valuesForBuildingPatterns2);
resultset = namedParameterJdbcTemplate.query(sqlQuery, params, new MyRowMapper());

Problem: Instead of searching for a list of string values, I want to search for a list of regex patterns (like %xyz%). But I'm unable to use both the "IN" clause and "LIKE" operator in the SQL query as shown below:

List<String> pattern1 = new ArrayList<String>();
List<String> pattern2 = new ArrayList<String>();

for(String str : valuesForBuildingPatterns1)
pattern1.add("%"+str+"%"); //If str="xyz", it adds "%xyz%" to the new list
for(String str : valuesForBuildingPatterns2)
pattern2.add("%"+str+"%");

String sqlQuery = "SELECT * FROM sqlServerTable col1 IN LIKE (:pattern1) AND col2 IN (:pattern2)";
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("pattern1", pattern1);
params.addValue("pattern2", pattern2);
resultset = namedParameterJdbcTemplate.query(sqlQuery, params, new MyRowMapper());

I also tried looping through the lists and buliding the SQL query with "LIKE" operator alone without the "IN" clause.However the query() method doesn't accept this. Besides, building the SQL query like this beats the main purpose of using NamedParameterJdbcTemplate, I guess.

for(String str1 : valuesForBuildingPatterns1)
for(String str2 : valuesForBuildingPatterns2)
{
String sqlQuery = "SELECT * FROM sqlServerTable col1 LIKE " + str1 + " AND col2 LIKE " + str2";
resultset.add(namedParameterJdbcTemplate.query(sqlQuery,null,new MyRowMapper()));
}

Can you please guide me on solving this problem? Please tell if more information is needed and kindly pardon my ignorance as don't know much about the framework. Thanks a lot. Your help is greatly appreciated.

PS: The project is being developed using an internal tool developed by the organization that I'm working for. This tool is built upon Spring framework and hence supports all services provided by spring framework.

È stato utile?

Soluzione

I figured out a way to achieve it. Thanks guys.

StringBuilder sb = new StringBuilder();     
for(String str1 : valuesForBuildingPatterns1)
for(String str2 : valuesForBuildingPatterns2)
sb.append(" col1 LIKE '%" + str1 + "%' AND col2 LIKE '%" + str2 + "%' OR");

sb.delete(sb.length()-2, sb.length());

String sqlQuery = "SELECT * FROM sqlServerTable WHERE" + sb.toString();
resultset = namedParameterJdbcTemplate.query(sqlQuery, params, new MyRowMapper());
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top