Home » Developer & Programmer » JDeveloper, Java & XML » Urgent Questions for JDBC
Urgent Questions for JDBC [message #92710] Sun, 12 December 2004 17:46 Go to next message
li yueli
Messages: 1
Registered: December 2004
Junior Member
I have worked a week to solve a question. How to upload/download image from Oracle9i. I have tried a lot of methods and viewed many pages but I can't get the image.

When I ran the readBlob method, there was an error: java.lang.nullpointerException. I think it has not got the BLOB object. But how to get it? I don't understand. There is the method I used:

 public synchronized void addBlob(String fileName, int id)throws Exception
 {
      
            stmt.execute("insert into image_test(id) values(" + id + " )");
            System.out.println ("Have insert a row to image_test");
            PreparedStatement pstmt = conn.prepareStatement("update image_test set image = ? where id = ?");

            File binaryFile = new File(fileName);
            int fileLength = 0;
            fileLength = (int)binaryFile.length();           
            FileInputStream instream = new FileInputStream(binaryFile);
            byte[[]] buffer = new byte[[fileLength]];
            instream.read(buffer, 0, fileLength);
            ByteArrayInputStream byteInput = new ByteArrayInputStream(buffer);
            
   pstmt.setInt(2, id);
   pstmt.setBinaryStream(1, byteInput, fileLength);
   pstmt.execute();
   pstmt.close();
         }

And the method of readBlob to download the Blob object to a filename from DB.

     public synchronized void readBlob(String fileName, int id)throws Exception
   {
         
            rset = stmt.executeQuery("SELECT image FROM image_test where id = " + id);
                                    
            FileOutputStream file_out = new FileOutputStream(new File(fileName));
            BufferedOutputStream buf_out = new BufferedOutputStream(file_out);
            if (rset.next()){
             InputStream blob_in = rset.getBinaryStream(1);
             int bytesRead = 0;
             byte[[]] buffer = new byte[[4096]];           
             while ((bytesRead = blob_in.read(buffer, 0, buffer.length)) != -1){  // There, throw an Exception, nullPointerException,  when blob_in.read(..) ???
            buf_out.write(buffer, 0, bytesRead);
       }
       blob_in.close();       
      }
                        
            conn.commit();          
            file_out.close();
            buf_out.close();
        }

Have any exerpts had done this before or some solution?

Thank you very much!
Re: Urgent Questions for JDBC [message #92720 is a reply to message #92710] Mon, 20 December 2004 02:12 Go to previous messageGo to next message
De Leeuw Thierry
Messages: 1
Registered: December 2004
Junior Member
Are you sur the column is not NULL?
Re: Urgent Questions for JDBC [message #92728 is a reply to message #92720] Wed, 22 December 2004 23:47 Go to previous message
Jan Tanis
Messages: 4
Registered: December 2004
Junior Member
viewing (from servlet) :

OutputStream os = res.getOutputStream();
Long id = new Long(fileId);
BLOB blob = null;
PreparedStatement ps = conn.prepareStatement( "select ATT_NAME, ATT_CONTENT, ATT_MIMETYPE from ATTACHMENT where ATT_ID = ? " );
ps.setInt( 1, id.intValue() );
ResultSet rs = ps.executeQuery();
if( rs.next() ) {
fileName = rs.getString(1);
mimeType = rs.getString(3);
blob = ((OracleResultSet)rs).getBLOB( 2 );
}


int len = -1;
int chunk = blob.getChunkSize();
byte[[]] buffer = new byte[[chunk]];
InputStream is = blob.getBinaryStream();
while( ( len = is.read( buffer ) ) != -1 )
os.write( buffer);
os.flush();
os.close();

Its essential that while youre inserting the BLOB, you insert an EmptyBLOB() value. After the insert you update
this record with a blob. Here s how i did it:

String query = "SELECT ATT_CONTENT FROM ATTACHMENT WHERE ATT_ID = " + value[[0]].toString()/* + " FOR UPDATE"*/;
/*CallableStatement stmt = aConnection.prepareCall(query);
ResultSet lobDetails = stmt.executeQuery();*/
Statement stmt = aConnection.createStatement();
ResultSet lobDetails = stmt.executeQuery(query);

if(lobDetails.next()){
Blob mapBlob = lobDetails.getBlob(1);
OutputStream blobOutputStream =((oracle.sql.BLOB)mapBlob).getBinaryOutputStream();
byte[[]] buffer = new byte[[10* 1024]];
int nread = 0; // Number of bytes read
while( (nread= aPFileIn.read(buffer)) != -1 )
blobOutputStream.write(buffer, 0, nread); // Write to Blob

// Close both streams
aPFileIn.close();
blobOutputStream.close();}
aConnection.commit();
aConnection.setAutoCommit(true);

Good luck with it
Previous Topic: IndexOutOfBoundsException
Next Topic: ORA-17059 Cannot convert to internal representation
Goto Forum:
  


Current Time: Fri Apr 19 05:41:01 CDT 2024