日期:2014-05-16  浏览次数:20477 次

JDBC对clob和blob的相关操作

这段时间经常接触到需要对clob和blob字段进行操作的情况,顺便总结一下。

很多情况下我们都需要对数据库中的clob和blob字段进行操作。比如在oracle下,按照我们以前的做法,一般如下:

1)用insert语句插入一个empty_clob()或者empty_blob(),这里是为了让数据库可以分配一个空的clob或blob;

2)接着我们需要从数据库中直接select from XXX for update,这里记住需要加update,至于不加update会有什么错误,大家可以试试,这里就不截图了。

3)然后我们通过rs.getClob(XXX)或者rs.getBlob(XXX),其中的XXX为字段名或索引,记住这里取到的clob和blob不是数据库相关的。即是java.sql.Clob和java.sql.Blob。

4)然后我们再通过相关的流设置来进行设值。

一共需要四步,麻烦不,其实说麻烦也不麻烦,但说简单也不简单,非要插入一个空的,然后才设值,有点奇怪,但也是没办法的,谁叫数据库不是我们自己开发的。

下面我们直接来看看一个小例子,实现一个上面的步骤,即“以前的做法”。

public class TestClob {
    public static void main(String[] args) {
        Connection conn = null;
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","3367446");
            //这里先插入一个空的clob,后面需要取出来进行更新
            PreparedStatement pstat = conn.prepareStatement("insert into test_clob(id,bigstring) values(1,empty_clob())");
            pstat.executeUpdate();
            pstat.close();

            conn.setAutoCommit(false);
            pstat = conn.prepareStatement("select bigstring from test_clob where id=1 for update");
            ResultSet rs = pstat.executeQuery();
            while(rs.next()) {
                Clob clob = rs.getClob("bigstring");
                //这里最重要,写入clob
                Writer writer = clob.setCharacterStream(0);
                BufferedWriter bw = new BufferedWriter(writer);
                bw.write("I'm the test string");
                //记得要关闭,或者flush,否则看不到效果
                bw.close();
                conn.commit();
            }
            conn.setAutoCommit(true);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

? 当我们执行后,看看效果:

?

这个例子应该都看得明白吧,blob也跟这个类似,不同的是BLOB的写入方式有点不同。

PreparedStatement pstat = conn.prepareStatement("insert into test_clob(id,bigblob) values(3,empty_blob())");
            pstat.executeUpdate();
            pstat.close();

            conn.setAutoCommit(false);
            pstat = conn.prepareStatement("select bigblob from test_clob where id=2 for update");
            ResultSet rs = pstat.executeQuery();
            while(rs.next()) {
                Blob blob = rs.getBlob("bigblob");
                //这里最重要,写入blob
                OutputStream out = blob.setBinaryStream(0);
                FileInputStream fis = new FileInputStream("C:\\Users\\Administrator\\Desktop\\img0.jpg");
                int length = 0;
                byte[] bytes = new byte[1024];
                while((length = fis.read(bytes)) != -1) {
                    out.write(bytes,0,length);
                }
                //记得要关闭,或者flush,否则看不到效果
                out.close();
                conn.commit();
            }
            conn.setAutoCommit(true);

?? 而clob我们测试后,效果为:

?

表的结构如下: