derby中的图像数据类型和BLOB数据类型

时间:2014-01-12 12:30:01

标签: java sql image blob derby

我想将图像插入到derby数据库

我遵循的步骤:

从文档中读取图像:

public Image[] ReadImg() throws IOException {
    Image[] img;
    String[] pname;
    int[] imageid;
    int l;
    XWPFDocument doc = new XWPFDocument(new FileInputStream("import.docx"));
    BufferedImage jpg = null;
    List<XWPFPictureData> pic = doc.getAllPictures();
    int length = pic.size();
    img = new Image[length];
    pname = new String[length];
    for (int i = 0; i < length; i++) {
        XWPFPictureData pict = pic.get(i);
        pname[i] = pict.getFileName();
    }
    imageid = new int[length];
    imageid = sorting(pname, length);
    for (int i = 0; i < length; i++) {
        XWPFPictureData pict = pic.get(imageid[i]);
        System.out.println(pict.getFileName());
        byte[] data = pict.getData();
        jpg = ImageIO.read(new ByteArrayInputStream(data));
        String[] prop = jpg.getPropertyNames();
        img[i] = jpg;
    }
    return img;
}

将图像插入德比数据库:

a)我用“BLOB”数据类型

创建了列图像
for(int k=0;k<img.length;k++)
        {
        PreparedStatement stmt2 = con.prepareStatement("insert into APP.IMAGES(IMAGES) values(?)");
        InputStream f=new FileInputStream(img[k]);
        stmt2.setBlob(1, f);
        stmt2.executeUpdate();
        stmt2.close();
        }

我不知道在步骤InputStream f = new FileInputStream(img [k]);

如何转换我的图像数据类型并将其放在这里?

你能帮我解决一下吗?

1 个答案:

答案 0 :(得分:3)

最后我得到了我的查询答案

ByteArrayOutputStream b=new ByteArrayOutputStream();
        ImageIO.write(img[k], "jpeg", b);
        byte[] imageInByte = b.toByteArray();
        b.close();
        InputStream fis = new ByteArrayInputStream(imageInByte);
        stmt2.setBlob(1, fis);
        stmt2.executeUpdate();