2

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?

4 Answers 4

4

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;
            }});
Sign up to request clarification or add additional context in comments.

Comments

3

You can try retrieving the blob from database as below.

String retrieveBlobAsString = null;
Blob b = row.get("REQUEST_MESSAGE");//cast with (Blob) if required. Blob from resultSet as rs.getBlob(index). 
InputStream bis = b.getBinaryStream();
ObjectInputStream ois = new ObjectInputStream(bis);
retrieveBlobAsString = (String) ois.readObject();

3 Comments

I got the below exception when I tried to cast with a Blob - Caused by: java.lang.ClassCastException: [B cannot be cast to java.sql.Blob
I think we only need to cast when we use an oracle data type BLOB. But since we are using Blob from java.sql package I don't think a cast is required. May be can you try removing the cast and see if it works?
Thanks Aditya. This statement doesn't work = Blob b = row.get("REQUEST_MESSAGE"); and throws the same error. If I am not casting , I need someway to convert the object into InputStream.
1

Another approach is by using java.sql.ResultSet getBytes() to convert BLOB column to String object,

List<ModelClass> hulaList = jdbcTemplate.query(sql,
    new RowMapper<ModelClass>() {
        @Override
        public ModelClass mapRow(ResultSet rs, int rowNum) throws SQLException {
            ModelClass model = new ModelClass();
            model.setOperationName(rs.getString("OPERATION_NAME"));
            byte[] byteArr = rs.getBytes("REQUEST_MESSAGE");
            model.setRequestMessage(new String(byteArr));
            return model;
        }
});

Comments

1

Java 8+ syntax:

List<MyObject> results = jdbcTemplate.query(
    "SELECT x,y FROM ... ",
    (rs, n) -> {
        MyObject o = new MyObject();
        o.setX(rs.getString("x"));
        o.setY(rs.getBytes("y")); // <-- a BLOB
        return o;
    }
);

Or if you query only a BLOB:

List<byte[]> b = jdbc.query("SELECT y FROM ...", (rs, n) -> rs.getBytes(1));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.