Spring jdbc template query returns empty results with multiple arguments

5k views Asked by At

Hi i have the following code.

public static final MY_QUERY = "Select distinct A.col1, A.col2 from tableA, tableB where A.col1 = ? and A.col2 = ? and b.id = A.id";

in my code i basically do this...

public List<MyClass> getData(final String input1, final String input2) {
    return this.jdbcTemplate.query(MyQuery, 
                    new Object[] { input1, input2}, 
                    new int[] {Types.VARCHAR, Types.CHAR}, new MyMapper());
}

When i run the results .. it returns empty list.

Now if i do this.

public static final MY_QUERY_2 = new StringBuilder(96)
.append("Select distinct A.col1, B.col2 from tableA, tableB where A.col1 =").append(input1)
.append(" and A.col2 = ").append(input2).append(" and b.id = A.id").toString();

and inside my DAO class

public List<MyClass> getData() {
    return this.jdbcTemplate.query(MY_QUERY_2, new MyMapper());
}

I get three results.

What is wrong with the first implementation.

Note:If i use only one argument than it works, but if i use 2, than it doesn't.

1

There are 1 answers

0
Chun ping Wang On BEST ANSWER

I found the problem.

if i use

A.Col2 = 'Input2' 

than trim is being called.

I solved it by using

trim(A.Col2) = ?

to get rid of whitespace.