簡體   English   中英

如何在Spring JdbcTemplate中將Resultset對象轉換為String

[英]How to convert Resultset object to String in Spring JdbcTemplate

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;
    }

即使轉換為toString()之后,也會獲得相同的結果。 輸出為: property - org.postgresql.jdbc.PgResultSet@6fd70563, value - org.postgresql.jdbc.PgResultSet@6fd70563

而不是上面的對象格式,我想像下面的字符串格式。 property - First Name, Last Name value - Ram, Robert

如果希望方法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
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM