jdbc处理大字段

来源:互联网 发布:淘宝网中老年人女裤 编辑:程序博客网 时间:2024/04/29 12:36

方法一,PreparedStatement.setCharacterStream(int parameterIndex,Reader reader,int length)


但这么做的弊端是不允许批量提交的。


方法二,


1)先用connection从数据库得到一个clob对象,

public static Clob getClobTDB(Connection con) {
        Clob clob = null;
        try {
            CallableStatement cs = con
                    .prepareCall("begin dbms_lob.createtemporary(?,false);end;");
            cs.registerOutParameter(1, Types.CLOB);
            cs.execute();
            clob = cs.getClob(1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return clob;
    }


2)将要插入到数据的的大字段写入从数据获取的clob对象


public static void getClobObj(Object jclob, String str) {
        CLOB clob = (oracle.sql.CLOB) jclob;
        Writer outStream = null;
        try {
            outStream = clob.getCharacterOutputStream();
            char[] c = str.toCharArray();
            outStream.write(c, 0, c.length);
            outStream.flush();
            outStream.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


3)完成PreparedStatement.setClob(int i, Clob x)


Clob clob = XXXDao.getClobTDB(con);
XXXDao.getClobObj(clob, que.getClobCasual());

xxxPstm.setClob(9, clob);




OK,这样写的好处是,可以执行大字段批量插入

xxxPstm.addBatch();

xxxPstm.executeBatch();

con.commit();

原创粉丝点击