如何在图像文件夹的postgres表中添加图像

问题描述 投票:0回答:1

我在postgres有一个客户表,我想插入客户数据的图像。我在一个文件夹中有客户的图像。我用他们的customerid命名了所有图像名称。

现在我想用照片列中的各自图像更新客户表,该列是字节数据类型。尽管将图像存储在数据库中是个坏主意,但我的图像尺寸很小,我需要这样做。

有人可以帮帮我吗?

postgresql
1个回答
1
投票

我将从这里开始了解用Java处理文件。 How to read all files in a folder from Java?

然后在postgresql.org上查看有关此主题的文档: https://jdbc.postgresql.org/documentation/80/binary-data.html

对于像这样的数据库表:

CREATE TABLE images (imgname text, img bytea);

要插入图像,您可以使用:

File file = new File("myimage.gif");
FileInputStream fis = new FileInputStream(file);
PreparedStatement ps = conn.prepareStatement("INSERT INTO images VALUES (?, ?)");
ps.setString(1, file.getName());
ps.setBinaryStream(2, fis, (int)file.length());
ps.executeUpdate();
ps.close();
fis.close();
© www.soinside.com 2019 - 2024. All rights reserved.