Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8787651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:56:37+00:00 2026-06-13T21:56:37+00:00

Aimed at preventing SQL injection attacks, all the SQL Statement code in my project

  • 0

Aimed at preventing SQL injection attacks, all the SQL Statement code in my project should transformed to Parameterized Query. But I got a problem when the query condition includes a ‘IN’ case. Like this (Using DB2 database):

String employeeId = 'D2309';
String name = "%brady%";

List<Integer> userRights = new ArrayList<Integer>();
userRights.add(1);
userRights.add(2);
userRights.add(3);

String sql = "SELECT * FROM T_EMPLOYEE WHERE EMPLOYEE_ID = ? AND NAME LIKE ? 
AND RIGHT IN (?)";

jdbcTemplate.query(sql, new Object[] {employeeId, name, userRights}, new 
EmployeeRowMapper());

The above code runs failed with the exception:

org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad 
SQL grammar [SELECT * FROM T_EMPLOYEE WHERE EMPLOYEE_ID = ? AND NAME LIKE ? AND 
RIGHT IN (?)]; nested exception is com.ibm.db2.jcc.am.io: [jcc][1091][10824]
[3.57.82] .... ERRORCODE=-4461, SQLSTATE=42815

The question here is that does not JdbcTemplate support Parameterized Query for IN case? and I know this work can be done by NamedParameterJdbcTemplate, and whether only NamedParameterJdbcTemplate can do IN case query?

Thanks a lot.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T21:56:38+00:00Added an answer on June 13, 2026 at 9:56 pm

    As I already mentioned in the comments, I’m not happy with this solution as it dynamically generates a number of SQL statements. Given the number of userRights is between 1 and n, it requires up to n prepared statements in the cache.

    The below should work (I did not try it).

    String employeeId = 'D2309';
    String name = "%brady%";
    
    List<Integer> userRights = new ArrayList<Integer>();
    userRights.add(1);
    userRights.add(2);
    userRights.add(3);
    
    // build the input string
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < userRights.size; i++) {
        sb.append("?");
        if (i < userRights.size() - 1) {
            sb.append(", ");
        }
    }
    
    // build the SQL
    String sql = "SELECT * FROM T_EMPLOYEE WHERE EMPLOYEE_ID = ?" +
        " AND NAME LIKE ?" +
        " AND RIGHT IN (" + sb.toString() + ")";
    
    // init the object array
    // size is employeeId + name + right
    Object[] param = new Object[2 + userRights.size()];
    
    // fill it
    param[0] = employeeId;
    param[1] = name;
    
    for (int i = 0; i < userRights.size(); i++) {
        param[i + 2] = userRights.get(i);
    }
    
    jdbcTemplate.query(sql, param, new EmployeeRowMapper());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All this guildes and books are aimed at developing website, but what if I
I have this php code that is aimed for importing all rows and columns
This question is kinda aimed towards PHP but it probably applies to other languages
Array.Copy and Buffer.BlockCopy both do the same thing, but BlockCopy is aimed at fast
I'm working on a project aimed at system administration for a linux installation. I
I am working on a project aimed to track eye pupil . For this
I have written a chunk of code that is aimed at reordering pages. <form
I'm working on a project aimed to analyze biometric data collected from various terminals.
I've been looking for a code coverage viewer aimed at inspecting live Java applications,
I'm currently working on a holiday project aimed at simulating biological body clocks. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.