我正在研究Spring框架,下面是SQL查詢來執行並獲取結果。RowMapper/ResultsetExtractor春季
String sql = "SELECT DESCRIPTION FROM ATWData WHERE SNO = '980098' ";
在Spring框架我們的RowMapper和ResultSetExtractor類回撥接口,哪一個是最好用來執行上述SQL查詢並獲取單個記錄。下面的代碼使用RowMapper和ResultSetExtractor獲取結果。
代碼使用ResultSetExtractor類:
try
{
description = jdbcTemplate.query(sql,
new ResultSetExtractor<String>()
{
public String extractData(final ResultSet rs) throws SQLException
{
if (rs.next())
{
String description = null;
description = rs.getString("DESCRIPTION");
return description;
}
return null;
}
});
}
catch (final DataAccessException e)
{
LOG.error("Error " + e);
}
下面的代碼使用的RowMapper得到結果:
try
{
description = jdbcTemplate2.query(sql,
new RowMapper<String>()
{
public String mapRow(final ResultSet rs, final int rowNum) throws SQLException
{
String description = (String)rs.getString("DESCRIPTION"));
return description;
}
});
}
catch (final DataAccessException e)
{
LOG.error("Error " + e);
}
我看了一下這些方法,但文檔困惑,選擇一個在我現有方案。請建議哪個代碼(RowMapper/ResultSetExtractor)最適合執行上面給出的簡單SQL查詢。
我懷疑'extractData()'返回'LocalDate'將允許代碼編譯。 – Andreas