問題描述
我正在嘗試將圖像插入數據庫并使用自動增量 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:未實現或不合理的轉換請求錯誤的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!