I need to fetch the data from a table which multiple filters and limit rows from java script datatable request
SELECT coalesce(parent_id,siteid) as siteid, address, state, status, plan,
remarks, FROM archive LEFT OUTER JOIN site_mappings ON
site_dn = mrbts AND siteid = child_site_id
In my code i have a implementation to append the filter in the query before executing the prepared statement.
filters here is List<String[]> filters having values of filters with the column name (UPPER(mrbts) like UPPER('%6105%'))... 6105 is the filter string and mrbts is the column name
private String createFilterWhereClause(List<String[]> filters) {
StringBuilder sb = new StringBuilder();
Iterator<String[]> filterParmItr = filters.iterator();
while (filterParmItr.hasNext()) {
String[] filterParm = filterParmItr.next();
sb.append("(")
.append(filterParm[ScFilterCriteria.FILTER_PARM_VAL])
.append(")");
if (filterParmItr.hasNext()) {
sb.append(" and ");
}
}
return sb.toString();
}
During execution ,it forms the sql query as below and executed in prepared statement.
SELECT coalesce(parent_id,siteid) as siteid, address, state, status, plan,
remarks, FROM archive LEFT OUTER JOIN site_mappings ON site_dn = mrbts AND
siteid = child_site_id where UPPER(mrbts) like UPPER('%4105%') and
((UPPER(technology) like UPPER('%LTE%')))
It has an SQL injection vulnarability. In order to solve that , i am trying to secure it by use prepared statement set string as below,
SELECT coalesce(parent_id,siteid) as siteid, address, state, status, plan,
remarks, FROM archive LEFT OUTER JOIN site_mappings ON site_dn = mrbts AND
siteid = child_site_id where ?
Using prepared statement ,
PreparedStatement ps = null;
Connection connection = null;
ps = connection.prepareStatement(sql);
String filters = createFilterWhereClause(filterClause);
ps.setString(1, filters );
Problem here in the sql query formed with single quotes after set string ,
SELECT coalesce(parent_id,siteid) as siteid, address, state, status, plan,
remarks, FROM archive LEFT OUTER JOIN site_mappings ON site_dn = mrbts AND
siteid = child_site_id where '((UPPER(mrbts) like UPPER(\'%6105%\')))';
How to remove the single quotes during set string and or any other approach to do this ? Could you someone help me.