`
cxshun
  • 浏览: 720607 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

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我们测试后,效果为:

 

表的结构如下:

 

 

前面我们称这种方法是旧方法,那现在我们用新的方法试试,当然这个新方法需要用到新包,可以到oracle官网下载:http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-112010-090769.html下载ojdbc6.jar

新的代码如下:

PreparedStatement pstat = conn.prepareStatement("insert into test_clob(id,bigblob) values(3,?)");
            Blob blob = conn.createBlob();
            OutputStream out = blob.setBinaryStream(0);
            FileInputStream fis = new FileInputStream(new File("C:\\Users\\Administrator\\Desktop\\2000.gif"));
            int length = 0;
            byte[] bytes = new byte[4096];
            while((length = fis.read(bytes)) != -1) {
                out.write(bytes);
            }
            out.close();
            pstat.setBlob(1,blob);
            pstat.executeUpdate();

   CLOB字段也类似,大家可以自行尝试。

 

可以看到,现在的操作已经省去了开始的第一步插入空的clob或blob了,而最后我们所要加的一句就是setBlob或setClob把相应的参数设到相应的参数位上。当然这个要求我们用preparedStatement,以此来使用参数。

 

这个是JDBC新标准的做法,由于我一般情况下只用oracle,mysql暂时没测试,大家可以看看是不是也是这样。

  • 大小: 3.4 KB
  • 大小: 1.4 KB
  • 大小: 150.7 KB
3
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics