zhyiwww
用平实的笔,记录编程路上的点点滴滴………
posts - 536,comments - 394,trackbacks - 0
oracle环境的jdbc操作

[方法1]
先插入empty_blob(),在检索出刚插入的数据,更新blob
插入代码

  • //获得数据库连接   
  •     Connection con = ConnectionFactory.getConnection();   
  •     con.setAutoCommit(false);   
  •     Statement st = con.createStatement();   
  •     //插入一个空对象empty_blob()   
  •     st.executeUpdate("insert into TESTBLOB (ID, NAME, BLOBATTR) values (1, "thename", empty_blob())");   
  •     //锁定数据行进行更新,注意“for update”语句   
  •     ResultSet rs = st.executeQuery("select BLOBATTR from TESTBLOB where ID=1 for update");   
  •     if (rs.next())   
  •     {   
  •         //得到java.sql.Blob对象后强制转换为oracle.sql.BLOB   
  •         oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob("BLOBATTR");   
  •         OutputStream outStream = blob.getBinaryOutputStream();   
  •         //data是传入的byte数组,定义:byte[] data   
  •         outStream.write(data, 0, data.length);   
  •     }   
  •     outStream.flush();   
  •     outStream.close();   
  •     con.commit();   
  •     con.close();
  • 注:以上代码来自网上。

    [方法2]
    和1相同,但是,我们不用两次操作了,因为我们可以直接用byte[]来替代blob,如:
            PreparedStatement pstmt = null;

            StringBuffer sb = new StringBuffer(800);
            sb.append("INSERT INTO ARTICAL");
            sb.append("(");
           
            sb.append("ARTICAL_ID");
            sb.append(",");
            sb.append("CATEGORY_ID");
            sb.append(",");
            sb.append("ARTICAL_TITLE");
            sb.append(",");
            sb.append("ARTICAL_TAGS");
            sb.append(",");
            sb.append("ARTICAL_INTO_DATE");
            sb.append(",");
            sb.append("ARTICAL_FILE_TYPE");
            sb.append(",");
            sb.append("ARTICAL_CONTENT");

            sb.append(")");
            sb.append(" ");
            sb.append("values");
            sb.append("(");
           
            sb.append("?");
            sb.append(",");
            sb.append("?");
            sb.append(",");
            sb.append("?");
            sb.append(",");
            sb.append("?");
            sb.append(",");
            sb.append("?");
            sb.append(",");
            sb.append("?");
            sb.append(",");
    //        sb.append("empty_blob()");
            sb.append("?");
           
            sb.append(")");

            try {
                pstmt = createPreparedStatement(sb.toString());

                pstmt.setLong(1, artical.getArticalId());
                pstmt.setLong(2, artical.getCategoryId());
                pstmt.setString(3, artical.getTitle());
                pstmt.setString(4, artical.getTags());
                pstmt.setDate(5, new Date(System.currentTimeMillis()));
                pstmt.setString(6, artical.getFileType());
               
                // 此处直接用数组
                pstmt.setBytes(7, artical.getContent());
               
               
                pstmt.executeUpdate();

            } catch (SQLException e) {

            } finally {
                if (pstmt != null) {
                    pstmt.close();
                }
            }

    // Artical部分代码
        public byte[] getContent() {
            return content;
        }

    上面的方法可以一次就完成对数据库的插入操作,而不是上面的两步。
    但是,是有缺憾的。

    缺点:
    byte[]的长度只能是int的最大长度,所以,这也就决定了此blob对象的最大长度,如果,你的blob的长度超出了Integer.MAX_VALUE就会出问题的。

    1KB=2^10bytes
    1MB=2^20bytes
    1GB=2^30bytes
    Integer的最大值在java中是2^32
    所以,此blob的最大容量是
    2^2*1GB=4GB
    这对于一般的应用足矣。






    |----------------------------------------------------------------------------------------|
                               版权声明  版权所有 @zhyiwww
                引用请注明来源 http://www.blogjava.net/zhyiwww   
    |----------------------------------------------------------------------------------------|
    posted on 2010-03-03 16:46 zhyiwww 阅读(2582) 评论(1)  编辑  收藏 所属分类: oracle

    FeedBack:
    # re: blob的插入操作方法优化[未登录]
    2010-03-08 18:13 | qq
    int i = 2147483647;//int数据的边界值
    byte[] b = new byte[i+1];//不会报错啊
    为什么说byte[]的长度只能是int的最大长度呢?
    当然确实遇到过类似超出Integer.MAX_VALUE的异常,没有没搞清楚原理
    请高手指点!3Q  回复  更多评论
      

    只有注册用户登录后才能发表评论。


    网站导航: