/** 针对oracle Blob字段的插入操作 */ @Test public void testBigBlobWrite() { PreparedDBUtil dbUtil = new PreparedDBUtil(); TransactionManager tm = new TransactionManager(); try { // 启动事务 tm.begin(); // 先插入一条记录,blob字段初始化为empty_lob dbUtil.preparedInsert("insert into test(id,blobname) values(?,?)"); String id = DBUtil.getNextStringPrimaryKey("test"); dbUtil.setString(1, id); dbUtil.setBlob(2, BLOB.empty_lob()); // 先设置空的blob字段 dbUtil.executePrepared(); // 查找刚才的插入的记录,修改blob字段的值为一个文件 dbUtil = new PreparedDBUtil(); dbUtil.preparedSelect("select blobname from test where id = ?"); dbUtil.setString(1, id); dbUtil.executePrepared(); BLOB blob = (BLOB) dbUtil.getBlob(0, "blobname"); if (blob != null) { DBUtil.updateBLOB(blob, new java.io.File("d:/dominspector.rar")); } tm.commit(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); try { tm.rollback(); } catch (RollbackException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } finally { tm = null; dbUtil = null; } }
/** 第一种插入blob字段的方法,通用的模式 */ @Test public void testBlobWrite() { PreparedDBUtil dbUtil = new PreparedDBUtil(); try { dbUtil.preparedInsert("insert into test(id,blobname) values(?,?)"); dbUtil.setString(1, DBUtil.getNextStringPrimaryKey("test")); dbUtil.setBlob(2, new java.io.File("d:/dominspector.rar")); // 直接将文件存储到大字段中 dbUtil.executePrepared(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { } }