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

 

posted @ 2017-11-15 10:14  疯狂的蜗牛仔  阅读(1056)  评论(0编辑  收藏  举报