从数据库获取BLOB,如何将它们放回原处 [英] Get BLOB from database, how to put them back

查看:78
本文介绍了从数据库获取BLOB,如何将它们放回原处的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从数据库中获取了BLOB数据,并使用以下代码将其放入字符串中(rs是ResultSet,文件是Map),有没有办法将此字符串值放回到BLOB列中? /p>

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

推荐答案

我发现将BLOB放入String的方式也不正确,如果不对BLOB数据进行编码,它将丢失.

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.

在这里,我找到了另一个帖子,可以完美解决此问题.感谢BalusC的回答 如何用JSON表示数据库中的图像

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

这篇关于从数据库获取BLOB,如何将它们放回原处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆