java - 将图像存储在oracle数据库中--错误--ORA-01460 : unimplemented or unreasonable conversion requested

标签 java oracle

我正在 JAVA 中开发一个应用程序,我必须上传图像(扫描副本),然后保存在数据库中,我的编码如下:---

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();
         }

    }
}

报错如下:== java.sql.SQLException:ORA-01460:请求的转换未实现或不合理

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)

我从 http://www.javatpoint.com/storing-image-in-oracle-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();  }  
 }  
 }  

关于java - 将图像存储在oracle数据库中--错误--ORA-01460 : unimplemented or unreasonable conversion requested,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31801105/

相关文章:

java - 标准(非灵活)Google App Engine 是否支持 Google Cloud Bigtable?

java - 监听 Firebase Firestore 数据库中的元数据更改

c++ - Oracle OCCI - 按名称而不是索引获取列

java - csv文件到android中的JSONArray

java - 使用接口(interface)导入最终字段(正确/错误)

Java-游戏 : Multiple instances

oracle - 当我运行带有参数的查询或命令时,为什么 Dapper 会抛出 OracleException?

mysql - 随时间变化的 Oracle Select

mysql - 主索引和二级索引到底有什么区别?

mysql - SQL查询最后连续相同的数据