Get BLOB from database, how to put them back

左心房为你撑大大i 提交于 2019-12-12 04:43:00

问题


I got the BLOB data from the database and use the below code to put it into a string(rs is the ResultSet, file is a Map), is there a way to put this string value back to the BLOB columns?

                    Blob blob = rs.getBlob("FILE_DATA");
                    if(blob != null) {
                        byte[] bdata = blob.getBytes(1, (int) blob.length());
                        String data = new String(bdata);
                        file.put("FILE_DATA", data);
                    }

I have tried some suggestions like this one, but they don't work well.

            Blob blbVal = getConnection().createBlob();
            byte[] fileData = value.toString().getBytes();
            blbVal.setBytes(1, fileData);
            cs.setBlob(index, blbVal);

回答1:


I found out that the way I put the BLOB into the String is also not correct, if we don't encode the BLOB data, it will be lost.

Here I found another post which solved this issue perfectly. Thanks for BalusC's answer How to represent an image from database in JSON



来源:https://stackoverflow.com/questions/21268723/get-blob-from-database-how-to-put-them-back

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!