zoukankan      html  css  js  c++  java
  • JDBC批量插入blob数据

    图片从接口读取后是base64的字符串,所以转成byte数组进行保存.

    我们一般保存数据的话,都是基本数据,对于这些图片数据大部分会将图片保存成Blob,Clob等.

    Blob存储的是二进制对象数据(用plsql可以查看图片)

    Clob存储的是字符串对象数据(一般存储图片的base64)

    下面是将JDBC存储图片成blob的方法:

        /**
         * 数据批量插入
         * @param poolName
         * @param humanList
         * @param sql
         * @return
         */
        public int executeInsertSQL(String poolName,List<HumanBo> humanList, String sql) {
            Connection conn = null;
            PreparedStatement prep = null;
            int ret = 0;
            try {
                conn = ConnectionPoolManager.getInstance().getConnection(poolName);
                prep = conn.prepareStatement(sql);
                for(HumanBo humanBo : humanList){
                    prep.setString(1, humanBo.getId());
                    prep.setString(2, humanBo.getName());
                    prep.setString(3, humanBo.getIdNumber());
                    byte[] imageUrl = Base64.decode(humanBo.getHumanPic());
                    ByteArrayInputStream inputstr = new ByteArrayInputStream(imageUrl);
                    prep.setBinaryStream(4, inputstr, imageUrl.length);
                    prep.setString(5, humanBo.getFlag());
                    prep.setString(6, humanBo.getType());
                    prep.setDate(7, humanBo.getCreateTime());
                    prep.addBatch();
                }
                int[] result = prep.executeBatch();
                ret = result.length;
            } catch (Exception e) {
                logger.error("执行数据库操作出错, SQL语句:" + sql, e);
            } finally {
                closeConn(poolName, conn, prep, null);
            }
            return ret;
        }
  • 相关阅读:
    Django开发笔记一
    Netty+SpringBoot写一个基于Http协议的文件服务器
    SQL优化
    mysql 字符串数字转换
    git 常用的命令总结
    eclipse 使用Maven deploy命令部署构建到Nexus
    intellij idea远程debug调试resin4教程
    postman 请求种添加用户权限
    对List中每个对象元素按时间顺序排序
    List根据时间字符串排序
  • 原文地址:https://www.cnblogs.com/fxust/p/7837592.html
Copyright © 2011-2022 走看看