Pallavi Choudhary
Pallavi Choudhary

Reputation: 103

store image in oracle database-- error-- ORA-01460: unimplemented or unreasonable conversion requested

I am working on an application in JAVA where I have to upload a image(scanned copy) and then save in database and I have coded as follows:---

public void insertImage() throws IOException
{
    System.out.println("In DAO");
    Connection con=null;
    PreparedStatement ps=null;
    try{  
        Class.forName("oracle.jdbc.driver.OracleDriver");
        System.out.println("Loaded Driver");
        con = DriverManager.getConnection(
                "jdbc:oracle:thin:@172.26.132.40:1521:orclilp",
                "aja20core", "aja20core");
    System.out.println("Connection established");
    ps=con.prepareStatement("insert into demo values(?,?)"); 
    ps.setString(1,"WS1");  
    FileInputStream fin=new FileInputStream("C:\\Users\\977924\\Desktop\\snapshots\\WS1.png");  
    ps.setBinaryStream(2,fin,fin.available()); 
    System.out.println("query to be fired");
    int i=ps.executeUpdate();
    System.out.println("query fired");
    System.out.println(i+" records affected");  
    }catch (SQLException e) {
        // TODO: handle exception
        e.printStackTrace();
    }
    catch (ClassNotFoundException ce) {
        // TODO: handle exception
        ce.printStackTrace();
    }
    catch (FileNotFoundException fe) {
        // TODO: handle exception
        fe.printStackTrace();
    }

    finally{
        try{
            if (con!= null && con.isClosed() == false)
            {
                con.close();
            }
            if(ps!=null)
                ps.close();
        }
         catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         }

    }
}

It is giving error as follows:== java.sql.SQLException: ORA-01460: unimplemented or unreasonable conversion requested

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.parseExecuteFetch(TTC7Protocol.java:1093)
at oracle.jdbc.driver.OracleStatement.executeNonQuery(OracleStatement.java:2047)
at oracle.jdbc.driver.OracleStatement.doExecuteOther(OracleStatement.java:1940)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:2709)
at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:589)
at com.DemoDAO.insertImage(DemoDAO.java:29)
at com.Controller.doPost(Controller.java:43)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:306)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:541)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:383)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:288)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)

I have taken the code from http://www.javatpoint.com/storing-image-in-oracle-database.

Upvotes: 0

Views: 931

Answers (1)

Amit Bhati
Amit Bhati

Reputation: 5659

As a good practise, you shouldn't store images in Database instead images should be stored at some location. And that location path should be stored against that image name in database.

import java.sql.*;  
import java.io.*;  
public class RetrieveImageFile {  
public static void main(String[] args) {  
try{  
//Registering the driver class for Oracle database
Class.forName("oracle.jdbc.driver.OracleDriver");

//Creating the connection object 
Connection con=DriverManager.getConnection(  
"jdbc:oracle:thin:@localhost:1521:DB","USER","PSWD");  

 //Select query to read the images from db which are stored as blob there
 PreparedStatement ps=con.prepareStatement("select IMAGE from table");  

 // Resultset obtained after executing the query
 ResultSet rs=ps.executeQuery();  
 if(rs.next()){//now on 1st row  

 // If your image column name is IMAGE then you can use   
 // Blob    b=rs.getBlob("IMAGE");         

 //Retrieving the first image from resultset which is in blob format
 Blob b=rs.getBlob(1); // Using column index of Image column

 // Now converting the Blob object into bytes as FileOuputStream don't    understand BLOB
 //Below 1 is the first byte of the BLOB and b.length() will give the next consecutive bytes which need to be copied from blob 
 byte byteArray[]=b.getBytes(1,(int)b.length()); 



 // The byte Array is then passed into the FileOutputStream
 FileOutputStream fout=new FileOutputStream("d:\\image.jpg");  

 //Writing the image
 fout.write(byteArray);  

 fout.close();  
 }
 System.out.println("Image loaded successfully");  

 con.close();  
 }catch (Exception e) {e.printStackTrace();  }  
 }  
 }  

Upvotes: 1

Related Questions