user1527483
user1527483

Reputation: 15

spring jdbc.query() suggestion

I need to execute the below query and get the list object. Do you guys know how best to retrieve in spring jdbc?

Here is the SQL Query:

SELECT app_name, error_code, error_message FROM error_message
WHERE ( 
    (app_name = ? AND error_code = ?) OR 
    (app_name = ? AND error_code = ?) OR 
    (app_name = ? AND error_code = ?) OR 
    (app_name = ? AND error_code = ?) OR 
    (app_name = ? AND error_code = ?) 
)

Upvotes: 0

Views: 122

Answers (1)

ahmetdursun
ahmetdursun

Reputation: 83

Here is my sample code:

String sql = yoursql;

Object args[] = new Object[3];
args[0] = yourparam1;          
args[1] = yourparam2;          
args[2] = yourparam3;

getJdbcTemplate().query(sql, args, yourRowMapper);

You should add following code for rowmapper.

private RowMapper yourRowMapper= new YourRowMapperClass ();

public final class YourRowMapperClass implements RowMapper {
   public Object mapRow(ResultSet rs, int rowNum) throws SQLException {

      YourResponse model = new YourResponse ();
      model.setresp1(rs.getDate(1));
      model.setresp2(rs.getDate(2));
      model.setresp3(rs.getString(3));

      return model;
    }
  }

Order is important for specifying the arguments. For example If we use searchType argument twice in our sql, we should define argument as below:

Object args[] = new Object[4];
args[0] = searchType;
args[1] = param1;
args[2] = searchType;
args[3] = param2;

If we customize the example for you, you can use the following code:

Object args[] = new Object[10];
args[0] = appname;
args[1] = errorcode;
args[2] = appname;
args[3] = errorcode;
args[4] = appname;
args[5] = errorcode;
args[6] = appname;
args[7] = errorcode;
args[8] = appname;
args[9] = errorcode;

Upvotes: 1

Related Questions