db2 存储BLOB格式的大对象
项目中需要将图片作为大对象存入到db2数据库中BLOB格式的字段中,直接存入byte[] 数组保存不成功。查找资料后,jdbc的方式确定要以输入流存入,代码如下:
/** * 存储图片 * * @param sqlString * @param bindArgs * @return */ public int extSql(byte[] b,Map m) { File file = FileHelper.newFile("img.png"); int rt = -1; try { conn(); ps=conn.prepareStatement("insert into tuxx (tu) values(?)"); FileHelper.writeFile(file, b, 0,b.length); InputStream inputStream= new java.io.FileInputStream(file); java.io.BufferedInputStream imageInput = new java.io.BufferedInputStream(inputStream); ps.setBinaryStream(1, imageInput,(int)file.length()); ps.executeUpdate(); conn.commit(); // 关闭流 bufferedInputStream.close() imageInput.close(); inputStream.close(); close(); } catch (Exception e) { ClientMain.println("d","db2操作失败!"+e.getMessage()); close(); } finally { // 如果try中异常,也要对数据库进行关闭 close(); } return rt; }