Question

I want to call this statement using spring JDBC how to do it

select* from cdc.fn_cdc_get_all_changes_dbo_Student(@from_lsn,@to_lsn,@row_filter_option);

should i use JDBCTemplate class or SimpleJdbcCall class .

I tried this but it did not worked :

jdbcTemplate.setResultsMapCaseInsensitive(true);
      SimpleJdbcCall mysimpleJdbcCall = new SimpleJdbcCall(jdbcTemplate);

      mysimpleJdbcCall.withSchemaName("cdc")
                  .withProcedureName("fn_cdc_get_all_changes_dbo_student").returningResultSet("students", 
                            new RowMapper<Student>()
                    {

                      @Override
                      public Student mapRow(ResultSet rs, int rowNum) throws SQLException
                      {
                        Student s = new Student();
                        s.setId(rs.getInt(5));
                        s.setName(rs.getString(6));
                        s.setAge(rs.getInt(7));
                        return s;
                      }
                    });

      MapSqlParameterSource paramSource =new MapSqlParameterSource();
     // paramSource.addValue("capture_instance", "dbo_student");
    paramSource.addValue("from_lsn", lsn1);
    paramSource.addValue("to_lsn", lsn2);
    paramSource.addValue("row_filter_option","all");
    paramSource.addValue("TABLE_RETURN_VALUE", null);
   /* ResultSet rs = mysimpleJdbcCall.executeFunction(ResultSet.class, paramSource);
    return null;*/
    Map m = mysimpleJdbcCall.execute(paramSource);
    List<Student> s=  (List)m.get("students");
    return s;
Was it helpful?

Solution

Instead of going through stored procedure way , table valued functions should have called directly the way we call a query.

I figured out this way:

String sql = "select* from cdc.fn_cdc_get_all_changes_" + tableName + "(:from_lsn ,     :tos_lsn , :row_filter_option)";

MapSqlParameterSource params =  new MapSqlParameterSource();
  params.addValue("from_lsn" , lsn1);
params.addValue("tos_lsn", lsn2);
params.addValue("row_filter_option", "all");

List<Student>s = namedParameterJdbcTemplate.query(sql, params, new RowMapper<Student>()
      {

        @Override
        public Student mapRow(ResultSet rs, int rowNo) throws SQLException
        {
          Student s = new Student();
          s.setId(rs.getInt(5));
          s.setName(rs.getString(6));
          s.setAge(rs.getInt(7));
          return s;
        }
      });
return s;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top