试图插入图像到数据库中获取ORA-01460:未实现或不合理的转换请求错误 [英] trying to insert image into database getting ORA-01460: unimplemented or unreasonable conversion requested error

查看:1330
本文介绍了试图插入图像到数据库中获取ORA-01460:未实现或不合理的转换请求错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将图片插入数据库并自动递增photo_id。

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

我的表列包括:

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

我的代码是

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

我收到错误:


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


推荐答案

InputStream.available()执行 流的大小(在Javadoc中清楚地记录)。

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

取决于您的JDBC驱动程序的版本,您也可以使用

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

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

setBinaryStream(int,InputStream,long)在JDBC 4.0中定义,因此不是由所有驱动程序版本实现。 setBinaryStream(int,InputStream,int)是JDBC 2.0(如果我没有错误),应该在所有版本都可用。

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.

这篇关于试图插入图像到数据库中获取ORA-01460:未实现或不合理的转换请求错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆