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

来源:互联网 发布:网络打字兼职可信吗 编辑:程序博客网 时间:2024/05/16 17:54

一、插入

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 blocke.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);}<span style="font-family: Arial, Helvetica, sans-serif;"> </span><span style="white-space:pre"> </span>//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字段的引用Civ.之后 ,对引用赋值 C.pubString(1,value);v.pstmt.setClob(C);赋值vi.conn.commit;//提交
二、查询

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

rs.getString("clobField");
可参考:点击打开链接


0 0
原创粉丝点击