0

I have a table with few fields out of which 2 are of Varchar and Blob type. When I'm retrieving them using queryForMap, I'm getting a map instance with two keys(name of the column). I'm able to cast varchar to String simply but getting ClassCast Exception doing the same with Object.

file = new File(System.currentTimeMillis() + (String) map.get("SAMPLE_DOC_FILE_NAME"));
blob = (Blob) map.get("SAMPLE_DOC");

My DAO layer method is:

public Map<String, Object> getSampleDoc1(String docName) throws SQLException {

    String query = "select form.sample_doc_file_name as SAMPLE_DOC_FILE_NAME, form.sample_document as SAMPLE_DOC from forms form where form.document_name=?";
    return localJdbcTemplateObject.queryForMap(query, docName);
}

Exception - java.lang.ClassCastException: [B cannot be cast to java.sql.Blob

What can I do to get back this object as Blob?

4
  • you can use "byte[]" data type for image, it will cast to BLOB type in database Commented Nov 19, 2015 at 6:37
  • 1
    System.out.println(yourBlob.getClass().getName()); on this object, then use this class. Commented Nov 19, 2015 at 6:55
  • @Ramaraj, I'm having problem after retrieval. Commented Nov 19, 2015 at 7:16
  • @KrzysztofCichocki, can you please explain a bit? Commented Nov 19, 2015 at 7:17

2 Answers 2

3

A Blob is just a wrapper for a (possibly large) byte[]. What you're getting back from Spring here is the interesting raw data, the byte[] (or B[ in the exception's notation). So, just use that instead, it'll be much easier to work with:

byte[] blob = (byte[]) map.get("SAMPLE_DOC");
Sign up to request clarification or add additional context in comments.

Comments

0

Please check what class it has as this:

System.out.println(map.get("SAMPLE_DOC").getClass().getName());

then cast to this type, then you can use the API of this type to do something with it.

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.