trying to insert image into database getting ORA-0

2020-04-20 14:24发布

I'm trying to insert an image into database and with auto increment photo_id.

my table columns are:

PHOTO_ID  (primary key),
USERNAME  (fkey),
PICTURE,
PICTURE_TITLE,
LIKES 

my code is

public class UploadPhotoDao {
    public int insertPhoto(UploadPhotoBean uploadBean){
        Connection con = null;
        PreparedStatement ps = null;

        int index = 0;
        final String query = "INSERT INTO PHOTOS_DETAILS (PHOTO_ID,USERNAME,PICTURE,PICTURE_TITLE,LIKES) VALUES (PHOTO_ID_SEQUENCE.nextval,?,?,?,?)";
            try {
                con = ConnectionUtil.getConnection();
                ps = con.prepareStatement(query);


                ps.setString(1, uploadBean.getUsername());
                File file =new File(uploadBean.getPicture());
                FileInputStream fis = new FileInputStream(file);
                ps.setBinaryStream(2, fis, fis.available());
                ps.setString(3, uploadBean.getPictureTitle());
                ps.setInt(4, new Integer(0));
                index = ps.executeUpdate();

            }
            catch (SQLException e) {
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
            finally {
                    ConnectionUtil.closeQuitly(ps);
                    ConnectionUtil.closeQuitly(con);
            }
            return index;
    }
}

I'm getting the error:

java.sql.SQLException: ORA-01460: unimplemented or unreasonable conversion requested

1条回答
趁早两清
2楼-- · 2020-04-20 14:35

InputStream.available() does not return the size of the stream (which is clearly documented in the Javadocs).

You will need to query the size of the file, not the "available bytes" in the inputstream:

ps.setBinaryStream(2, fis, (int)file.length());

depending on the version of your JDBC driver you can also use

ps.setBinaryStream(2, fis, file.length());

But setBinaryStream(int, InputStream, long) is defined in JDBC 4.0 and thus not implemented by all driver versions. setBinaryStream(int, InputStream, int) is JDBC 2.0 (if I'm not mistaken) and should be available in all versions.

查看更多
登录 后发表回答