试图将图像插入数据库得到 ORA-01460:未实现或不合理的转换请求错误

2022-01-09 00:00:00 image database insert java jsp

我正在尝试将图像插入数据库并使用自动增量 photo_id.

我的表格列是:

PHOTO_ID(主键),用户名 (fkey),图片,PICTURE_TITLE,喜欢

我的代码是

公共类 UploadPhotoDao {公共 int insertPhoto(UploadPhotoBean uploadBean){连接 con = null;PreparedStatement ps = null;整数索引 = 0;最终字符串查询=插入照片详细信息(PHOTO_ID,用户名,图片,图片标题,喜欢)值(PHOTO_ID_SEQUENCE.nextval,?,?,?,?)";尝试 {con = ConnectionUtil.getConnection();ps = con.prepareStatement(查询);ps.setString(1, uploadBean.getUsername());文件文件 =new File(uploadBean.getPicture());FileInputStream fis = new FileInputStream(file);ps.setBinaryStream(2, fis, fis.available());ps.setString(3, uploadBean.getPictureTitle());ps.setInt(4, 新整数(0));index = ps.executeUpdate();}捕捉(SQLException e){e.printStackTrace();} 捕捉(FileNotFoundException e){//TODO 自动生成的 catch 块e.printStackTrace();} 捕捉(IOException e){//TODO 自动生成的 catch 块e.printStackTrace();}最后 {ConnectionUtil.closeQuitly(ps);ConnectionUtil.closeQuitly(con);}回报指数;}}

我收到了错误:

<上一页>java.sql.SQLException: ORA-01460: 请求未实现或不合理的转换

解决方案

InputStream.available() 不返回的大小流(Javadocs 中有明确记录).

您需要查询文件的大小,而不是输入流中的可用字节":

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

根据您的 JDBC 驱动程序版本,您也可以使用

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

setBinaryStream(int, InputStream, long) 是在 JDBC 4.0 中定义的,因此并非所有驱动程序版本都实现.setBinaryStream(int, InputStream, int) 是 JDBC 2.0(如果我没记错的话),应该在所有版本中都可用.

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

解决方案

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.

相关文章