Search code examples
javaspringspring-bootspring-jdbcjdbctemplate

How to read Blob data into String Object using Spring JDBCTemplate


I am trying to use Spring JDBCTemplate to read blob data from a table.

List<Map<String, Object>> rows = jdbcTemplate.queryForList(sql);

for(Map<String, Object> row:rows){
    row.get("OPERATION_NAME");
    row.get("REQUEST_MESSAGE"); // this is blob
}

How can I read blob into a Java String object?


Solution

  • This seemed to work fine -

    LobHandler lobHandler = new DefaultLobHandler();
    List<FrontendData> frontEndDataList = jdbcTemplate.query(getResponseQuery(sessionId), new RowMapper() {
                @Override
                public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
                    // TODO Auto-generated method stub
    
                    FrontendData frontEndData = new FrontendData();
                    String operationName = rs.getString("OPERATION_NAME");
                    frontEndData.setApiName(operationName);
                    byte[] requestData = lobHandler.getBlobAsBytes(rs,"RESPONSE_MESSAGE");
                    frontEndData.setResponse(new String(requestData));
    
    
                    return frontEndData;
                }});