ORACLE存大文件出现 错误:读取违反顺序

来源:互联网 发布:json对象解析不出来 编辑:程序博客网 时间:2024/05/16 10:13
 
package common;

import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import java.sql.Types;

public class StoreFile extends Thread {
   
private Connection con=null;
   
private String filename=null;
    
public StoreFile(Connection c,String name){
           
this.con=c;
           
this.filename=name;
        
    }

   
    
public void run(){
        loadFile(con,filename);
        
    }

    
private boolean loadFile(Connection con,String filename){
      
      


        PreparedStatement pstmt
=null;
        
try{
            con.setAutoCommit(
false); 

            File file
=new File(filename);
            FileInputStream fin
=new FileInputStream(file);
            
long filesize=file.length();
         System.out.println(
" fin.available()="+fin.available());
         
long key=getKEYfromSEQ(con);
         System.out.println(
"存入文件:"+filename+" 长度:"+filesize+" 取得主键值:"+key);
          String sql
="insert into filelist (n_file_id,n_file_length,vc_file_name," +
                    
"b_file,c_file)values("+key+",?,?,empty_blob(),empty_clob())";
          
//  String sql="insert into filelist (n_file_id,n_file_length,n_file_name) values (filelist_seq.nextval,?,?)";
            
            pstmt
=con.prepareStatement(sql);
            pstmt.setLong(
1,filesize);
            pstmt.setString(
2,filename);
         
//   pstmt.setObject(3,file,Types.BINARY);
         
//  pstmt.setBinaryStream(3,fin,(int)filesize);
         
// pstmt.setBinaryStream(4,fin,(int)filesize);
            pstmt.execute();
            pstmt.close();
            
//现在~再把它查询出来,填充二进制数据流,UPDATE
           String update="select b_file from filelist where n_file_id="+key+" for update"
            pstmt
=con.prepareStatement(update);
            ResultSet set
=pstmt.executeQuery();
            Blob blob
=null;
         
//   if(set.next()) blob=set.getBlob(1);
//           OutputStream out=blob.setBinaryStream(0);
            
            
        
//    stmt.
            
            
        }
catch(Exception e){
            e.printStackTrace();
        }
finally{
            
try {
                pstmt.close();
                con.close();
            
            }
 catch (SQLException e) {
                
// TODO Auto-generated catch block
                e.printStackTrace();
            }

            
        }

        
        
        
        
        
return true;
    }


  
private long getKEYfromSEQ(Connection con){
      Statement stmt
=null;
      
try {
        stmt
=con.createStatement();
        ResultSet set
=stmt.executeQuery("select filelist_seq.nextval from dual");
        
if(set.next())return set.getLong(1);
        
    }
 catch (SQLException e) {
        
// TODO Auto-generated catch block
        e.printStackTrace();
    }
finally{
        
try {
            stmt.close();
        }
 catch (SQLException e) {
            
// TODO Auto-generated catch block
            e.printStackTrace();
        }

        
    }

        
return -1;
  }

  
}

 

情况:

当我一执行:

 String update="select b_file from filelist where n_file_id="+key+" for update";

就报错,错误描述如下:

 

java.sql.SQLException: ORA-01002: 读取违反顺序

    at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:
134)
    at oracle.jdbc.ttc7.TTIoer.processError(TTIoer.java:
289)
    at oracle.jdbc.ttc7.Oall7.receive(Oall7.java:
573)
    at oracle.jdbc.ttc7.TTC7Protocol.doOall7(TTC7Protocol.java:
1891)
    at oracle.jdbc.ttc7.TTC7Protocol.fetch(TTC7Protocol.java:
1198)
    at oracle.jdbc.driver.OracleStatement.doExecuteQuery(OracleStatement.java:
2400)
    at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:
2672)
    at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:
589)
    at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:
527)
    at common.StoreFile.loadFile(StoreFile.java:
54)
    at common.StoreFile.run(StoreFile.java:
25)

 

解决办法:

Connection一定要先con.setAutoCommit(false);

至于为什么?我现在不知道~~~~~~

知道的请回答 ^_^

 

下面是抄来的~~

jdbc中如何处理Oracle BLOB字段

为什么我要写这篇文章?

在前段时间我所在的项目中,就碰到了这个问题,我花了2天的时间才将BLOB的问题搞定。我也尝试过网上所介绍的各种方法,那些方法所

使用的原理都一致,但都写得不完整,我也按照网上介绍的方法做了,但都因为其中一些没有提到的小的细节而失败。希望看到这篇文章的人

都不再走弯路。

一般人会走哪些弯路?
1.使用jdk中的方法进行传输。在ResultSet 中有getBlob()方法,在PreparedStatement中有setBlob()方法,所以大多数人都会尝试setBlob

(),getBlob() 进行读写,或者两个数据库之间BLOB的传输。这种方法实际上是行不通的,据网上的一些资料介绍,说sun官方的文档有些方法

都是错误的。
2.使用ResultSet.getBinaryStream 和PreparedStatement.setBinaryStream对BLOB进行读写或两个数据库间的传输。这种方法我自己尝试过,

发现,如果BLOB中存储的是文本文件的话,就没问题,如果是二进制文件,传输就会有问题。

根据自己的经验,以及查阅了Oracle的官方文档,都是使用如下处理方法:
1.新建记录,插入BLOB数据
 1.1首先新建记录的时候,使用oracle的函数插入一个空的BLOB,假设字段A是BLOB类型的:
  insert xxxtable(A,B,C) values(empty_blob(),'xxx','yyyy')
 1.2后面再查询刚才插入的记录,然后更新BLOB,在查询前,注意设置Connection的一个属性:
  conn.setAutoCommit(false);如果缺少这一步,可能导致fetch out of sequence等异常.
 1.3 查询刚才插入的记录,后面要加“ for update ”,如下:
  select A from xxxtable where xxx=999 for update ,如果缺少for update,可能出现row containing the LOB value is not locked

的异常
 1.4 从查询到的 BLOB字段中,获取blob并进行更新,代码如下:
  BLOB blob = (BLOB) rs.getBlob("A");
  OutputStream os = blob.getBinaryOutputStream();
  BufferedOutputStream output = new BufferedOutputStream(os);

  后面再使用output.write方法将需要写入的内容写到output中就可以了。例如我们将一个文件写入这个字段中:
  BufferedInputStream input = new BufferedInputStream(new File("c://hpWave.log").toURL().openStream());
  byte[] buff = new byte[2048];  //用做文件写入的缓冲
  int bytesRead;
  while(-1 != (bytesRead = input.read(buff, 0, buff.length))) {
   output.write(buff, 0, bytesRead);
   System.out.println(bytesRead);
  }
  上面的代码就是从input里2k地读取,然后写入到output中。
 1.5上面执行完毕后,记得关闭output,input,以及关闭查询到的ResultSet
 1.6最后执行conn.commit();将更新的内容提交,以及执行conn.setAutoCommit(true); 改回Connction的属性
2.修改记录,方法与上面的方法类似,
 2.1首先更新BLOB以外的其他字段
 2.2 使用1.3中类似的方法获取记录
 2.3 修改的过程中,注意以下:a 需要更新的记录中,BLOB有可能为NULL,这样在执行blob.getBinaryOutputStream()获取的值可能为

null,那么就关闭刚才select的记录,再执行一次update xxxtable set A = empty_blob() where xxx, 这样就先写入了一个空的BLOB(不是null),然后再

使用1.3,1.4中的方法执行更新记录.b 注意别忘了先执行setAutoCommit(false),以及"for update",以及后面的conn.commit();等。
3.读取BLOB字段中的数据.
 3.1 读取记录不需要setAutoCommit(),以及 select ....for update.
 3.2 使用普通的select 方法查询出记录
 3.3 从ResultSet中获取BLOB并读取,如下:
  BLOB b_to = (BLOB) rs.getBlob("A");
  InputStream is = b_from.getBinaryStream();
  BufferedInputStream input = new BufferedInputStream(is);
  byte[] buff = new byte[2048];
  while(-1 != (bytesRead = input.read(buff, 0, buff.length))) {
   //在这里执行写入,如写入到文件的BufferedOutputStream里
   System.out.println(bytesRead);
  }
  通过循环取出blob中的数据,写到buff里,再将buff的内容写入到需要的地方
4.两个数据库间blob字段的传输
类似上面1和3的方法,一边获取BufferedOutputStream,另外一边获取BufferedInputStream,然后读出写入,需要注意的是写入所用的

Connection要执行conn.setAutoCommit(false);以及获取记录时添加“ for update ”以及最后的commit();

总结以上方法,其根本就是先创建空的BLOB,再获取其BufferedOutputStream进行写入,或获取BufferedInputStream进行读取

原创粉丝点击