试图插入图像到数据库中获取ORA-01460:未实现或不合理的转换请求错误 [英] trying to insert image into database getting ORA-01460: unimplemented or unreasonable conversion requested error
问题描述
我正在尝试将图片插入数据库并自动递增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屋!