trying to insert image into database getting ORA-01460: unimplemented or unreasonable conversion requested error(试图将图像插入数据库得到 ORA-01460:未实现或不合理的转换请求错误)
问题描述
我正在尝试将图像插入数据库并使用自动增量 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.
这篇关于试图将图像插入数据库得到 ORA-01460:未实现或不合理的转换请求错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:试图将图像插入数据库得到 ORA-01460:未实现或不合
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01