Quantcast
Viewing all articles
Browse latest Browse all 4

Using static final Strings for SQL Query in Spring Boot

This is regarding code quality and coding standards with respect to Spring Ecosystem. Here there are two methods:

  • isRecordExists is using a class level static constant String query. (Definitely suggestable for old way where we use prepared statements)
  • isRecordExists2 is using method level String query

Which method is better (isRecordExists or isRecordExists2) in context with Spring Boot?

@Repositorypublic class SpringRepository {     @Autowired    private NamedParameterJdbcTemplate namedParamJdbcTemplate;    private static final String QUERY = " select * from Dual where created_date=:date";    public void isRecordExists(Date date){       /**       *  using constant for sql Query String at class level       */         MapSqlParameterSource parameters = new MapSqlParameterSource();         parameters.addValue("date", date);         namedParameterJdbcTemplate.queryForObject(QUERY, parameters, Integer.class);    }   public void isRecordExists2(Date date){         String sqlQuery=" select * from Dual where created_date=:date";       /**       *  using  sql Query at method level       */         MapSqlParameterSource parameters = new MapSqlParameterSource();         parameters.addValue("date", date);         namedParameterJdbcTemplate.queryForObject(sqlQuery, parameters, Integer.class);    }

Viewing all articles
Browse latest Browse all 4

Trending Articles