阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Oracle处理Clob类型数据入库(String入库)

95次阅读
没有评论

共计 2580 个字符,预计需要花费 7 分钟才能阅读完成。

从网上查找一堆参考,要么语焉不详,要么不可行。自己鼓捣了一堆可以正常入库了。请看最后:

insert into CP_V_INFO” +
                    “(ID, “+
                    “PROJECT_ID, “+
                    ……
                    “V_INFO, “+
                    ……
                    “VERSION)values(?,?,?,?,?,?,?,?,?,?,” +
                    “?,?,EMPTY_CLOB(),?,?,?,?,?,?,?,” +
                    “?,?,?,?,?,?,?,?,?,?,” +
                    “?)”;
        this.cdcDao.executeSQL(sql, v7Info.getId(),
                v7Info.getProjectId(),……
            );
// 先插入该数据,在该字段用 EMPTY_CLOB() 插入。
// 然后 for update
String update_sql = “select V_INFO from CP_V_INFO where ID='”+v7Info.getId()+”‘ for update”;
            this.cdcDao.executeClobSQL(update_sql,”V_INFO”,v7Info.getvInfoStr());

 

 

 

/**
    *
    * @Method: executeClobSQL
    * @Description: 更新 Clob 字段
    * @param update_sql
    * @param column 字段名称  data  该字段数据
    * @return
    * @throws SQLException
    * @throws Exception
    * @author liuz
    * @date 2016-3-28
    */
    public void executeClobSQL(String update_sql,String column,String data) throws SQLException, Exception {
        if(((MyJdbcTemplate)this.getJdbcTemplate()).isNEED_ENCODE()){
            update_sql = new String(update_sql.getBytes(((MyJdbcTemplate)this.getJdbcTemplate()).getAPP_ENCODE()),((MyJdbcTemplate)this.getJdbcTemplate()).getDB_ENCODE());
        }
        PreparedStatement p = null;
        ResultSet rs = null;
        try {
            Connection conn = this.getConnection();
           
            conn.setAutoCommit(false);
            p = paserSQL(conn, update_sql);
            rs = conn.createStatement().executeQuery(update_sql);
            if (rs.next()) {
                /* 取出此 CLOB 对象 */
                Oracle.sql.CLOB clob = null;
                clob = (oracle.sql.CLOB) rs.getClob(column);
                /* 向 CLOB 对象中写入数据 */
                BufferedWriter out = new BufferedWriter(clob
                        .getCharacterOutputStream());
                    out.write(data);
                    out.flush();
                    out.close();
            }
            rs.close();
            conn.commit();
            conn.setAutoCommit(true);
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (p != null) {
                try {
                    p.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

 

千辛万苦倒腾入库之后,同事说了一句“干嘛要改底层代码?”。啪啪啪一通悦耳的键盘声之后,改回原来的方式。该字段用 String 正常插入。结果是:正常入库了。、、、、、、、、、、泪奔!!!!

 

String sql = “insert into CP_V_INFO” +
                    “(ID, “+
                    ……
                    “V_INFO, “+
                    ……
                    “VERSION)values(?,?,?,?,?,?,?,?,?,?,” +
                    “?,?,?,?,?,?,?,?,?,?,” +
                    “?,?,?,?,?,?,?,?,?,?,” +
                    “?)”;
        this.cdcDao.executeSQL(sql, v7Info.getId(),
                ……
                v7Info.getvInfo(),//String 类型
                ……
                v7Info.getVersion());
/* ******** end **********/

更多 Oracle 相关信息见 Oracle 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=12

本文永久更新链接地址 :http://www.linuxidc.com/Linux/2016-04/130637.htm

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-22发表,共计2580字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中