如何在Spring JdbcTemplate中将Resultset对象转换为String

时间:2018-11-21 16:17:30

标签: java spring

<head>
    <script type="text/javascript" src="test.js"></script>
</head>

即使转换为toString()之后,也会得到相同的结果。 输出为: I am getting the below output after executing the query. I am getting in Object format, but i need in String format. I want to get list of the ResultSet. private static final String GET_PROPERTY_VALUE = "select property, value \r\n" + " from metadata \r\n" + " where id = ?"; public List<String> getTransformationValue(Long documentId) throws ABCApplicationException{ List<String> names = null; try { names = jdbcTemplate.query(GET_PROPERTY_VALUE, new Object[] { documentId }, new RowMapper() { public Object mapRow(ResultSet resultSet, int rowNum) throws SQLException { resultSet.getString(1); resultSet.getString(2); return resultSet.toString(); } }); } catch (DataAccessException dae) { logger.error(dae.toString()); throw new ABCApplicationException(dae).setParameter("SQL", GET_PROPERTY_VALUE); } return names; }

我想要的不是上面的Object格式,而是下面的String格式。 property - org.postgresql.jdbc.PgResultSet@6fd70563, value - org.postgresql.jdbc.PgResultSet@6fd70563

1 个答案:

答案 0 :(得分:0)

如果期望方法jdbcTemplate.query返回字符串列表,建议您使用SingleColumnRowMapper而不是简单的RowMapper。像这样:

new SingleColumnRowMapper<String>(String.class)
{
    public String mapRow(ResultSet resultSet, int rowNum) throws SQLException
    {
        return resultSet.getString(1); // Chose here your desired reading formula
    }
}