java clob类型怎么插入数据库_java操作数据库中clob类型 之插入和查询

一、插入

1)对于clob字段,首先插入empty_clob();

String sql = " insert into batchintfloadlog (centercode,filename,policydate,starttime,endtime,resultinfo) values ('"

+ this.ywBranchCode.substring(0, 4).concat("0000") + "','"

+ this.dataType + "',date '"

+ this.policyDate + "',"

+ "'"+ this.loadTime + "','"+ endtime +"',"

+ "empty_clob()";

ExeSQL eSQL = new ExeSQL(conn1);

eSQL.execUpdateSQL(sql);

//提前提交,防止插入日志发生异常时,日志表中无任何记录

//若发现resultinfo字段为null.则表明提交之后,插入日志方法insertIntfLoadLog()发生异常

//当然此处也可以不提交(业务需要)

try {

conn1.commit();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

2)使用select...forupdate语句查询(目的是查询并锁定当前数据行以便更新)

String sql2 = "select resultinfo from batchintfloadlog a where " +

"a.centercode='"+ this.ywBranchCode.substring(0, 4).concat("0000") + "' " +

"and a.filename='"+ this.dataType + "' " +

"and a.policydate= date'"+ this.policyDate +"' " +

"and a.starttime='"+ this.loadTime +"' and a.endtime='"+ endtime +"'" +

"for update";

try {

pstmt = conn1.prepareStatement(sql2);

rs = pstmt.executeQuery();

pstmt = null;

if(rs.next()){

Object obj = rs.getObject(1);

if("oracle.sql.CLOB".equals(obj.getClass().getName())){

clob = (CLOB) rs.getClob(1);

}

//else if("weblogic.jdbc.wrapper.Clob_oracle_sql_CLOB".equals(obj.getClass().getName())){

// Method method = obj.getClass().getMethod("getVendorObj",new Class[]{});

// clob = (CLOB) method.invoke(obj);

//}

//其中1表示的是sql3中的第几个问号

clob.putString(1, eMessage);

String sql3 = "update batchintfloadlog a set a.resultinfo=? where " +

"a.centercode='"+ this.ywBranchCode.substring(0, 4).concat("0000") + "' " +

"and a.filename='"+ this.dataType + "' " +

"and a.policydate= date'"+ this.policyDate +"' " +

"and a.starttime='"+ this.loadTime +"' and a.endtime='"+ endtime +"'";

pstmt = conn1.prepareStatement(sql3);

pstmt.setClob(1, clob);

pstmt.executeUpdate();

//提交

conn1.commit();

closeMethod(rs, pstmt,conn1);

}

} catch (Exception e) {

try {

conn1.rollback();

} catch (Exception e1) {

e1.printStackTrace();

}

closeMethod(rs, pstmt,conn1);

e.printStackTrace();

}

总结: i.先把clob字段类型给空出来,取而代之的是empty_clob(),插入数据库中

ii.使用select...for update把刚插入的数据给查出来,for update目的锁定当前数据行

iii.使用getObject(index)方法获得clob字段的引用C

iv.之后 ,对引用赋值 C.pubString(1,value);

v.pstmt.setClob(C);赋值

vi.conn.commit;//提交

二、查询

相对来说查询比较简单,就直接getString即可

rs.getString("clobField");可参考:

点击打开链接

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值