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


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

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 **********/

相关内容