首页 > 数据库 >postgresql数据库插入和读取图片

postgresql数据库插入和读取图片

时间:2022-12-14 22:34:34浏览次数:50  
标签:preparedStatement postgresql 读取 img 数据库 connection test new image

postgresql插入和读取图片

postgresql存储图片需要二进制类型bytea,创建一张测试表:

postgres=# create table test_image(img bytea);
CREATE TABLE

使用jdbc插入1.jpg

try {
    connection = JDBCUtil.getConnection();
    connection.setAutoCommit(false);
    String sql = "INSERT INTO test_image(img) VALUES(?)";
    preparedStatement = connection.prepareStatement(sql);
    InputStream inputStream = new FileInputStream(img_name);
    preparedStatement.setBinaryStream(1,inputStream);
    preparedStatement.executeUpdate();
    connection.commit();
} catch (SQLException | FileNotFoundException e) {
    throw new RuntimeException(e);
} finally {
    JDBCUtil.release(connection);
}

查看字段长度

postgres=# select length(img) from test_image;
 length
--------
  50726
(1 row)

读取图片

try {
    connection = JDBCUtil.getConnection();
    String sql = "SELECT img from test_image";
    preparedStatement = connection.prepareStatement(sql);
    resultSet = preparedStatement.executeQuery();
    while (resultSet.next()) {
        InputStream in = resultSet.getBinaryStream(1);
        byte[] buffer = new byte[1024*1024];
        OutputStream out = new FileOutputStream("new_1.jpg");
        for (int i; (i = in.read(buffer)) > 0;)
        {
            out.write(buffer, 0, i);
            out.flush();
        }
        out.close();
        in.close();
    }
} catch (Exception e) {
    e.printStackTrace();
} finally {
    JDBCUtil.release(connection);
}

标签:preparedStatement,postgresql,读取,img,数据库,connection,test,new,image
From: https://www.cnblogs.com/jl1771/p/16983837.html

相关文章

  • 架构设计(二):数据库复制
    架构设计(二):数据库复制作者:Grey原文地址:博客园:架构设计(二):数据库复制CSDN:架构设计(二):数据库复制在架构设计(一):从单服务器模式到负载均衡设计中提到了数据库类型的选择,针......
  • #yyds干货盘点#node.js链接MongoDB数据库
    在这之前确保当前环境下安装了mongodb的模块,且mongodb数据库已经启动,安装mongodb模块到当前目录可以通过yarnaddmongodb我此时安装的是v4.1.4版本的mongodb常见的连接方法......
  • spring boot 实现Mysql数据脚本导出和数据库脚本的导入
    前言在开发过程中这样一个需求,有些数据需要从数据库导出,然后导入到另外的数据库中。数据导出@SneakyThrowspublicStringexport(){//获取数据库连接对象......
  • Zabbix 6 系列学习 07:包安装(时序数据库篇)
    其实出这期文章的目的就是展示官方为了解决传统关系型数据库在应对海量监控数据的能力不足的方案。本文环境系统:AlmaLinux8.7数据库:PostgreSQL14TimescalePHP数据库部分Al......
  • windows 按照mongodb数据库
    参考博客,不要选择c盘安装,我下载的msi文件直接安装的,https://www.cnblogs.com/nastu/p/16271881.htmlhttps://www.runoob.com/mongodb/mongodb-window-install.html......
  • PostgreSQL 常用操作记录
    常用命令行命令1,连接数据库#需要输入密码psql-hhost-Udbuser-ddbname免密登录方法:方法一:设定环境变量PGPASSWORD​方法二:配置.pgpass​touch~/......
  • .Net App.Config 读取
    经常能在.Net项目中看到App.Config/Web.Config,一直没有了解过.Net自带的对配置文件的读写操作,常规的操作类在System.Configuration.dll中,比较重要的类为Configura......
  • 数据库自动备份
    本文是通过使用Bat文件去执行SQL的方法使数据库进行备份,bat文件被windows的任务定时调取1.执行备份数据库文件的sql文件内容,并命名为backup.sqlGODECLARE@backupTim......
  • 巨蟒python全栈开发数据库前端5:JavaScript1
     1.js介绍&变量&基础数据类型2.类型查询&运算符&if判断&for循环3.while循环&三元运算符4.函数5.今日总结 1.js介绍&变量&基础数据类型js介绍(1)什么是JavaScript&一些历史......
  • docker-compose + mysql8.x 主从数据库配置
    0.准备(略过docker的安装与镜像拉取)docker/docker-compose安装拉取mysql8.x 1.master和slave的mysql配置master:[mysqld]server-id=11118log-bin=mysql-bi......