BLOB是特殊的类型,需要自己重新写代码读的。不能用缺省的STRING读法。

解决方案 »

  1.   

    Clob testClob = resultSet.getClob(i);              oracle.sql.CLOB newClob=(oracle.sql.CLOB)testClob;              Reader reader=newClob.getCharacterStream();              int length = newClob.getBufferSize();              char[] cbuffer = new char[length];              StringBuffer sbuffer = new StringBuffer();              int index,type;              int nread = 0;              while((nread = reader.read(cbuffer))!=-1){                for(index =0;index<length;index++){                  type = Character.getType(cbuffer[index]);                  if(!(type==1 || type==2 || type==9 || type==26)) break;                }
                    sbuffer.append(cbuffer,0,index);              }              newRow.addElement(sbuffer);以前写的,反正能用。看看吧
      

  2.   

    我读的就是string的数据流啊,用setBinaryStream方法啊,楼上的有具体代码吗?最好是string数据的插入,读出
      

  3.   

    以前用过的,汉字没有问题,楼主可以试下。
    import java.sql.*;
    import java.io.*;
    import java.util.*;
    import oracle.sql.BLOB;
    import oracle.sql.*;public class LOBUtil {
        public LOBUtil(){ 
        
        }    /**
         * String2LOB store String to LOB
         * @param data String need to be stored.
         * @param table_name Table name
         * @param lob_field_name LOB Field
         * @param key_field Primary Key Field
         * @param key_value Primary Key value
         */
        public boolean String2LOB(Connection conn,
                                  String data,
                                  String table_name,
                                  String lob_field_name,
                                  String key_field,
                                  String key_value)throws DBAccessException{
            try {
                
                boolean commit=conn.getAutoCommit();
              
                BLOB lob = (BLOB)getBLOB(conn,table_name,lob_field_name,key_field,key_value);
               
                OutputStream os = lob.getBinaryOutputStream();
               
                java.io.BufferedOutputStream bos = new java.io.BufferedOutputStream(os);
               
                bos.write(data.getBytes());//Stored String data to BLOB field
              
                bos.flush();
               
                bos.close();
               
                os.close();
               
                conn.commit();
                
         conn.setAutoCommit(commit);
               
                return true;
            } catch(SQLException sqle){
                
                throw new DBAccessException(sqle);
            }catch (Exception err) {
                throw new DBAccessException(err);
                
            }
        }     private static void log(String msg){
            System.out.println(msg);
        }    private Blob getBLOB(Connection conn,
                             String table_name,
                             String lob_field_name,
                             String key_field,
                             String key_value)throws DBAccessException{
            Blob lob = null;
            try {
               
                String sql = "select " + lob_field_name + " from " + table_name + " where " + key_field + " = '" + key_value + "' for update ";
               
                conn.setAutoCommit(false);
               
                Statement pstmt = conn.createStatement();
               
                java.sql.ResultSet rs = pstmt.executeQuery(sql);
               
                if (rs.next()) {
                    //lob = ((OracleResultSet)rs).getBLOB(1);
               
                    lob = rs.getBlob(1);
               
               
                    return lob;
                } else {
                    pstmt.close();
                    throw new DBAccessException("LOB field can not be found. Please check it again.");
                }        } catch (Exception err) {            
                try{
                    conn.rollback();
                }catch(SQLException sqle){
                   
                    throw new DBAccessException(sqle);
                }
                throw new DBAccessException(err.getMessage());
            }    }    /**
         * LOB2String get String data from LOB
         * @param conn @see java.sql.Connection Object
         * @param table_name Table name
         * @param lob_field_name LOB Field
         * @param key_field Primary Key Field
         * @param key_value Primary Key value
         */
        public String LOB2String(
                                Connection conn,
                                String table_name,
                                String lob_field_name,
                                String key_field,
                                String key_value) throws DBAccessException{
            try {
                BLOB p_BLOB = (BLOB)getBLOB(conn,table_name,lob_field_name,key_field,key_value);
                // Open a stream to read BLOB data
                InputStream stream = p_BLOB.getBinaryStream();            // Keep BLOB Datas
                java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
                java.io.OutputStream os = new java.io.BufferedOutputStream(bos);            // Read from the BLOB stream and write to the stringbuffer
                int nchars = 0; // Number of chanracters read
                byte[] l_buffer = new byte[1024];  //  Buffer holding characters being transferred
                while ((nchars = stream.read(l_buffer)) != -1) // Read from BLOB
                    os.write(l_buffer,0,nchars);
                os.flush();
                os.close();
                String result = new String(bos.toByteArray());
                stream.close();  // Close the BLOB input stream
                bos.close();
                return result;
            } catch (Exception ex) { // Trap SQL and IO errors
                try{
                    conn.rollback();
                }catch(SQLException sqle){
                }
                return null;
            } 
        }    
    }
      

  4.   

    if (rs.next()) {
                    lob = ((OracleResultSet)rs).getBLOB(1);or
                    lob = rs.getBlob(1);
    楼主可以试下,跟连接池或jdbc版本有关系。
      

  5.   

    为什么我一用chunk方法,结果得到的长度都是8132呢??
    非常着急,有没有高手现身啊????