上传照片并将其保存到 Oracle 数据库 10g



我编写了一段代码,可以帮助用户使用 JDBC 将图像保存在 oracle 数据库中。

PreparedStatement ps =con.prepareStatement("insert into employee values(?,?)");
ps.setString(1,name);
ps.setBlob(2,inputStream);

但是当我尝试运行代码时,它出现错误

java.lang.AbstractMethodError: Method oracle/jdbc/driver/T4CPreparedStatement.setBlob(ILjava/io/InputStream;(V is abstract

这是如何造成的,我该如何解决?

首先尝试将输入流转换为 byte[],并将其作为参数传递给 steBlob 方法

例如:

BufferedInputStream bis= new BufferedInputStream(inputStream); /// pass your inputStream here
        int size=0;
        byte[] bytes;
        Blob blob = con.createBlob(); // create blob using connection obj
        int length = 1;
        int readCount=0;
        System.out.println(img.getCanonicalPath());
        do {
            bytes = new byte[bis.available()];
            readCount = bis.read(bytes);
            blob.setBytes(length, bytes); ///populate chunks of data to the blob
            length=length+readCount;
            size= bis.available();
            //System.out.println(bis.read());
        }while ( size > 0 );
        PreparedStatement st = con.prepareStatement("INSERT INTO TABLE VALUES (?,?)");
        st.setString(1, id);
        st.setBlob(2, blob);
        st.execute();

最新更新