We help IT Professionals succeed at work.

How to store and get an image in Orcale

Morfar3000
Morfar3000 asked
on
Medium Priority
238 Views
Last Modified: 2012-08-14
Could anybody give me a simple example about how to store a picture (fx jpg) in Oracle using Long Raw, and how to recieve it back. I'm using JSP. I have tried to store a picture (jpg), but i'm not sure that it works, because i'm not able to recieve it. here is the code for store-image:

<%@page language="java" import="java.sql.*, java.io.*, java.util.*"  %>

<%    
     
Connection conn = "connection is made here"
                   
File fil = new File("C:/testpicture.jpg");
FileInputStream fis = new FileInputStream(fil);
     
try {
               
  PreparedStatement ps = conn.prepareStatement("insert into Test values(?,?)");
                   
  ps.setString(1, "the name");
  ps.setBinaryStream(2, fis, 19000);
  ps.executeUpdate();
         
  ps.close();
  conn.close();
  fis.close();
         
  out.println("Success");

}      
     
catch(SQLException e ) {
  out.println(e);
}
         
%>

It seems to work, but i'm not sure. how do i recieve it back.

Comment
Watch Question

Hi Morfar
Are you sure that u want to save it in long raw
if it is the case
In your insert statement Please use this function before inserting in the database as raw
insert into tables values(hextoraw(?),?)

Shyam

Commented:
Hey why dont U use BLOB i will give U sample code.
 first read the file into byte array form.

try this code

public void insertFile(Connection conn ,int idocId, byte[] bydata1)
         {
         try {

Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@XXXserver:1521:database", "scott", "tiger");
}
conn.setAutoCommit(false);
stmt = conn.createStatement();
stmt.execute("INSERT into " + SDocData +" values ('"
              + idocId + "', EMPTY_BLOB()) ");
rs = stmt.executeQuery("select * from "+ SDocData +
              " where doc_id = '" + idocId + "' for update of file_data");


while(rs.next()) {
blob = (oracle.sql.BLOB)rs.getObject(2);
         }
System.out.println(" Blob length before insert "
                   + blob.length());
java.io.OutputStream os = ((oracle.sql.BLOB)blob).getBinaryOutputStream();

os.write(bydata1);
os.flush();
os.close();

System.out.println("Blob length in the database " +
                   blob.length());
OraclePreparedStatement ops =          (OraclePreparedStatement)conn.prepareStatement
                   ("update Doc_Data set file_data=? where  " +" doc_id = '" + idocId + "'");
ops.setBLOB(1, blob);
ops.execute();
} catch (IOException e) {
e.printStackTrace();
    +e.getMessage());
                        }
                        throw new LibraryDAOException(
                             useme.clc.library.exception.
                             ExceptionClass.RecordNotInsertedException);
                   }

              } finally {
                   // finally clause  ensures closing of connection
                   //closeDBConnection(conn);
              }
         }

}


and read the file back in the same way


alll the best

Author

Commented:
i'm using blob instead.

thx for the answer!!!!

Explore More ContentExplore courses, solutions, and other research materials related to this topic.